Title: Reading ZipFile not thread-safe
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.8, Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Thomas, cuibonobo, eric.smith, malin, serhiy.storchaka
Priority: normal Keywords:

Created on 2020-11-16 11:07 by Thomas, last changed 2021-01-25 22:47 by cuibonobo.

File name Uploaded Description Edit Thomas, 2020-11-16 11:07 Test case and traceback
Messages (5)
msg381090 - (view) Author: Thomas (Thomas) * Date: 2020-11-16 11:07
According to bpo-14099, reading multiple ZipExtFiles should be thread-safe, but it is not.

I created a small example where two threads try to read files from the same ZipFile simultaneously, which crashes with a Bad CRC-32 error. This is especially surprising since all files in the ZipFile only contain 0-bytes and have the same CRC.

My use case is a ZipFile with 82000 files. Creating multiple ZipFiles from the same "physical" zip file is not a satisfactory workaround because it takes several seconds each time. Instead, I open it only once and clone it for each thread:

with zipfile.ZipFile("/tmp/", "w") as dummy:

def clone_zipfile(z):
    z_cloned = zipfile.ZipFile("/tmp/")
    z_cloned.NameToInfo = z.NameToInfo
    z_cloned.fp = open(, "rb")
    return z_cloned

This is a much better solution for my use case than locking. I am using multiple threads because I want to finish my task faster, but locking defeats that purpose.

However, this cloning is somewhat of a dirty hack and will break when the file is not a real file but rather a file-like object.

Unfortunately, I do not have a solution for the general case.
msg381126 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-11-16 17:14
I'm changing from "crash" to "behavior". We use "crash" for a segfault or equivalent. I realize that most people are unlikely to know this, but we consider "crash" to be more alarming, so I want to make sure it's correct.

Also: when this happens, is it always for file 127, or does it change on each run?
msg381139 - (view) Author: Thomas (Thomas) * Date: 2020-11-16 18:40
I have not observed any segfaults yet. Only zipfile.BadZipFile exceptions so far.

The exact file at which it crashes is fairly random. It even crashes if all threads try to read the same file multiple times.

I think the root cause of the problem is that the reads of zef_file in are not locked properly.

The underlying file object is shared between all ZipExtFiles. Every time a thread makes a call to, a new lock is created in _SharedFile, but that lock only protects against multiple threads reading the same ZipExtFile. Multiple threads reading different ZipExtFiles with the same underlying file object will cause trouble. The locks do nothing in this scenario because they are individual to each thread and not shared.
msg381205 - (view) Author: Thomas (Thomas) * Date: 2020-11-17 05:08
Scratch what I said in the previous message. I thought that the lock was created in _SharedFile and did not notice that it was passed as a parameter.
msg381212 - (view) Author: Thomas (Thomas) * Date: 2020-11-17 07:17
I have simplified the test case a bit more:

import multiprocessing.pool, zipfile

# Create a ZipFile with two files and same content
with zipfile.ZipFile("", "w", zipfile.ZIP_STORED) as z:
    z.writestr("file1", b"0"*10000)
    z.writestr("file2", b"0"*10000)

# Read file1  with two threads at once
with zipfile.ZipFile("", "r") as z:
    pool = multiprocessing.pool.ThreadPool(2)
    while True:, ["file1", "file1"])

Two files are sufficient to cause the error. It does not matter which files are read or which content they have.

I also narrowed down the point of failure a bit. After

in ( ), the following assertion should hold:

assert(self._file.tell() == self._pos)

The issue occurs when seeking to position 35 (size of header + length of name). Most of the time, self._file.tell() will then be 35 as expected, but sometimes it is 8227 instead, i.e. 35 + 8192.

I am not sure how this can happen since the file object should be locked.
Date User Action Args
2021-01-25 22:47:46cuibonobosetnosy: + cuibonobo
2020-11-17 08:28:25serhiy.storchakasetnosy: + serhiy.storchaka
2020-11-17 07:17:55Thomassetmessages: + msg381212
2020-11-17 05:08:06Thomassetmessages: + msg381205
2020-11-16 18:40:07Thomassetmessages: + msg381139
2020-11-16 17:14:36eric.smithsettype: crash -> behavior

messages: + msg381126
nosy: + eric.smith
2020-11-16 11:25:09malinsetnosy: + malin
2020-11-16 11:10:08Thomassettype: crash
components: + Library (Lib)
2020-11-16 11:07:51Thomascreate