Fix ZipFile resource leak in open_maybe_zipped#70012
Open
magic-peach wants to merge 1 commit into
Open
Conversation
The open_maybe_zipped function created a ZipFile object inline and called .open(filename) on it, but the ZipFile reference was lost after the return. The TextIOWrapper wrapped the inner file object, but no one retained a reference to the outer ZipFile, so it could never be explicitly closed. Added a _ZipFileWrapper class that closes both the inner file and the ZipFile when closed, ensuring proper cleanup of the archive file descriptor. Signed-off-by: Akanksha Trehun <akankshatrehun@gmail.com>
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.
Summary
The
open_maybe_zippedfunction (airflow-core/src/airflow/utils/file.py:72) creates aZipFileobject inline and calls.open(filename)on it:The
ZipFilereference is lost after the return. TheTextIOWrapperwraps the inner file object from.open(), but no one retains a reference to the outerZipFile, so it can never be explicitly closed. TheZipFile(which holds an open file descriptor to the archive) will only be cleaned up when the garbage collector eventually runs, which is non-deterministic.Each call to this function with a zipped path leaks one file handle.
Fix: Added a
_ZipFileWrappersubclass ofTextIOWrapperthat closes both the inner file and theZipFilewhen closed, ensuring proper cleanup of the archive file descriptor.Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (big-pickle) following the guidelines