Skip to content

gh-159422: Always close GzipFile in __del__ even if the warning fails - #159423

Open
brittanyrey wants to merge 3 commits into
python:mainfrom
brittanyrey:b-gzip-del-close
Open

brittanyrey wants to merge 3 commits into
python:mainfrom
brittanyrey:b-gzip-del-close

Conversation

@brittanyrey

@brittanyrey brittanyrey commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

BUG: GzipFile.del emits a ResourceWarning before closing the file. If emitting it raises an exception, close() is skipped and the gzip file left truncated and incomplete.

FIX: Close the file in a finally block to ensure it always runs.

NEW TEST: The new test is load bearing, without the fix it fails with EOFError: Compressed file ended before the end-of-stream marker was reached.

GzipFile.__del__ emits a ResourceWarning before closing the file. If
emitting it raises, close() is skipped and the gzip trailer is never
written, truncating the output. This happens at interpreter shutdown,
when the function-level `import warnings` fails, and with
-W error::ResourceWarning. Close the file in a finally block.
aisk
aisk previously approved these changes Oct 14, 2026
@aisk
aisk self-requested a review October 14, 2026 16:51
@aisk
aisk dismissed their stale review October 14, 2026 16:52

Just found the newly added test fails on the free-threaded build

Comment thread Lib/test/test_gzip.py Outdated
The order of __del__ calls at interpreter shutdown is not deterministic
on the free-threaded build, so make the warning raise directly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants