Skip to content

Commit 4352ca2

Browse files
bpo-42369: Fix thread safety of zipfile._SharedFile.tell (GH-26974)
The `_SharedFile` tracks its own virtual position into the file as `self._pos` and updates it after reading or seeking. `tell()` should return this position instead of calling into the underlying file object, since if multiple `_SharedFile` instances are being used concurrently on the same file, another one may have moved the real file position. Additionally, calling into the underlying `tell` may expose thread safety issues in the underlying file object because it was called without taking the lock. (cherry picked from commit e730ae7) Co-authored-by: Kevin Mehall <[email protected]>
1 parent 87b3e20 commit 4352ca2

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

Lib/zipfile.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,9 @@ def __init__(self, file, pos, close, lock, writing):
721721
self._lock = lock
722722
self._writing = writing
723723
self.seekable = file.seekable
724-
self.tell = file.tell
724+
725+
def tell(self):
726+
return self._pos
725727

726728
def seek(self, offset, whence=0):
727729
with self._lock:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix thread safety of :meth:`zipfile._SharedFile.tell` to avoid a "zipfile.BadZipFile: Bad CRC-32 for file" exception when reading a :class:`ZipFile` from multiple threads.

0 commit comments

Comments
 (0)