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 e730ae7eff)

Co-authored-by: Kevin Mehall <km@kevinmehall.net>
This commit is contained in:
Miss Islington (bot) 2022-03-20 07:51:11 -07:00 committed by GitHub
parent 87b3e202d4
commit 4352ca234e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 1 deletions

View file

@ -721,7 +721,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.