[Python-checkins] bpo-42369: Fix thread safety of zipfile._SharedFile.tell (GH-26974)

miss-islington webhook-mailer at python.org
Sun Mar 20 10:54:39 EDT 2022


https://github.com/python/cpython/commit/4aa8b802513340d12a6ffea3d5e2228ac6c7d5b8
commit: 4aa8b802513340d12a6ffea3d5e2228ac6c7d5b8
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-03-20T07:54:19-07:00
summary:

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

Co-authored-by: Kevin Mehall <km at kevinmehall.net>

files:
A Misc/NEWS.d/next/Library/2022-03-19-19-56-04.bpo-42369.Ok828t.rst
M Lib/zipfile.py

diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index f72c6a4e0c253..1e942a503e8ee 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -720,7 +720,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:
diff --git a/Misc/NEWS.d/next/Library/2022-03-19-19-56-04.bpo-42369.Ok828t.rst b/Misc/NEWS.d/next/Library/2022-03-19-19-56-04.bpo-42369.Ok828t.rst
new file mode 100644
index 0000000000000..86dc3a0b81b9c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-03-19-19-56-04.bpo-42369.Ok828t.rst
@@ -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.



More information about the Python-checkins mailing list