Repository navigation
Conversation
Popen does not close stdout after wait(), so the pipe was only released when the hook object was garbage collected. If reading the output raised, the pipe was never closed. Unclosed pipes surface as ResourceWarning in unrelated code, which fails tests that turn warnings into errors. Close stdout in a finally block instead of using `with Popen(...)`. Popen.__exit__ calls wait() for any exception other than KeyboardInterrupt, which would block the execution_timeout path before on_kill() can send SIGTERM to the child.
Dev-iL
force-pushed
the
2610/popen_leak
branch
from
October 16, 2026 05:55
9a26213 to
c4a6c06
Compare
This branch has not been deployed
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.
SubprocessHook.run_commandnever closed the stdout pipe of thePopenit creates.Popendoes not close stdout afterwait(), so the pipe stays open until the hook object is released, and it also stays open when reading the output raises. This change closes the pipe in afinallyblock, so it is closed on both the normal and the error path.Why
try/finallyand notwith Popen(...):Popen.__exit__callswait()for any exception other thanKeyboardInterrupt. On anexecution_timeout,AirflowTaskTimeout(aBaseException) is raised inside the read loop, andon_kill()runs only after the stack unwinds (task-sdk/src/airflow/sdk/execution_time/task_runner.py). Withwith Popen(...), the unwind would block until the child exits, soon_kill()would never send SIGTERM to a long-running command.Evidence
ResourceWarnings; after the fix, +0 and 0.-W error::ResourceWarning. Before the fix, 6PytestUnraisableExceptionWarning: ... unclosed file <_io.BufferedReader name=N>; after the fix, 0.providers/standard/tests/unitsuite on the fixed tree (run before the final test-only mock change): 1018 passed, 146 skipped, 0 failed. The hook test module was re-run after that change: 12 passed.The leaked objects match the
unclosed file <_io.BufferedReader name=N>warnings reported bytest_no_warnings(providers/standard/tests/unit/standard/decorators/test_python.py), which runs withfilterwarnings("error"). This PR does not claim to fix that test's flakiness: the CI failure was not reproduced locally.Reproducing the descriptor leak
Save the script below as
dev/fd_leak_experiment.py, then run it against the base and the fixed hook from the repository root:git show upstream/main:providers/standard/src/airflow/providers/standard/hooks/subprocess.py > dev/subprocess_hook_base.py breeze run python dev/fd_leak_experiment.py dev/subprocess_hook_base.py 200 breeze run python dev/fd_leak_experiment.py providers/standard/src/airflow/providers/standard/hooks/subprocess.py 200 rm dev/subprocess_hook_base.py dev/fd_leak_experiment.pyWas generative AI tooling used to co-author this PR?
Generated-by: Claude Code following the guidelines
{pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.