gh-154842: Reject repack() while a reading handle is open - #154843
Merged
serhiy-storchaka merged 2 commits intoAug 16, 2026
Conversation
ZipFile.repack() moves member data, but a ZipExtFile from an earlier open() keeps its own absolute position, so it silently returned data from the wrong place and a full read failed with a misleading CRC error. Raise ValueError while _fileRefCnt shows an open reading handle, as the writing-handle case already does.
Documentation build overview
66 files changed ·
|
| raise ValueError( | ||
| "Can't write to ZIP archive while an open writing handle exists" | ||
| ) | ||
| if self._fileRefCnt > 1: |
Member
There was a problem hiding this comment.
Would not it be better to move all two (or even three) checks into the with self._lock block?
And maybe harmonize or consolidate the two last errors? Something like "Can't repack ZIP archive while an open handle exists".
Contributor
Author
There was a problem hiding this comment.
Both done, thanks.
The fp/_writing/_fileRefCnt checks guarded state that repack() then mutates inside `with self._lock`, so the check and the `_writing = True` it gates were not atomic. Move them into the lock. The writing-handle and reading-handle errors describe the same condition, so state it once.
fedonman
force-pushed
the
zipfile-repack-open-readers
branch
from
August 16, 2026 16:51
5d6c2cc to
bfe8238
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ZipFile.repack()moves member data to lower offsets, but aZipExtFilefrom an earlieropen()keeps its own absolute position, so it silently returned data from the wrong place and a full read failed withBadZipFile: Bad CRC-32. RaiseValueErrorwhile an open reading handle exists, as the writing-handle case already does.The test fails without the guard.
repack()is new in 3.16, so no NEWS entry.