Skip to content

Fix ZipFile resource leak in open_maybe_zipped#70012

Open
magic-peach wants to merge 1 commit into
apache:mainfrom
magic-peach:fix/zipfile-resource-leak-in-open-maybe-zipped
Open

Fix ZipFile resource leak in open_maybe_zipped#70012
magic-peach wants to merge 1 commit into
apache:mainfrom
magic-peach:fix/zipfile-resource-leak-in-open-maybe-zipped

Conversation

@magic-peach

Copy link
Copy Markdown

Summary

The open_maybe_zipped function (airflow-core/src/airflow/utils/file.py:72) creates a ZipFile object inline and calls .open(filename) on it:

return TextIOWrapper(zipfile.ZipFile(archive, mode=mode).open(filename))

The ZipFile reference is lost after the return. The TextIOWrapper wraps the inner file object from .open(), but no one retains a reference to the outer ZipFile, so it can never be explicitly closed. The ZipFile (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 _ZipFileWrapper subclass of TextIOWrapper that closes both the inner file and the ZipFile when closed, ensuring proper cleanup of the archive file descriptor.


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (big-pickle)

Generated-by: Claude Code (big-pickle) following the guidelines

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>
@potiuk potiuk added the ready for maintainer review Set after triaging when all criteria pass. label Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready for maintainer review Set after triaging when all criteria pass.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants