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.
This commit is contained in:
Kevin Mehall 2022-03-20 08:26:09 -06:00 committed by GitHub
parent 3af68fc77c
commit e730ae7eff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 1 deletions

View File

@ -747,7 +747,9 @@ class _SharedFile:
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:

View File

@ -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.