Skip to content

Commit

Permalink
bpo-42369: Fix thread safety of zipfile._SharedFile.tell (pythonGH-26974
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
kevinmehall authored Mar 20, 2022
1 parent 3af68fc commit e730ae7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Lib/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,9 @@ def __init__(self, file, pos, close, lock, writing):
self._lock = lock
self._writing = writing
self.seekable = file.seekable
self.tell = file.tell

def tell(self):
return self._pos

def seek(self, offset, whence=0):
with self._lock:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +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 comments on commit e730ae7

Please sign in to comment.