Repository navigation
fix(zipapp): remove the extraction directory when killed while unzipping - #4520
Conversation
The shell zip launchers (`py_zipapp_binary` and `--build_python_zip` with `bootstrap_impl=script`) leak a partly extracted temporary directory if they are killed while still unzipping. bazel-contrib#4161 added a detached watcher that removes the directory once the launcher PID exits, but the watcher only starts just before `exec`, after extraction has finished. Before that: * `zip_shell_template.sh` creates the directory with `mktemp -d`, runs `unzip`, and only then sets the `EXIT` trap. A `SIGHUP` (e.g. closing the terminal), `SIGTERM` or `SIGKILL` during `unzip` leaves the directory behind. * `stage1_bootstrap_template.sh` sets the trap before `unzip`, which covers `SIGHUP` and `SIGTERM`, but not `SIGKILL`. For a large app (hundreds of MB), unzipping takes long enough for this to happen in practice, and each occurrence leaves the partial extraction in `$TMPDIR`. To fix, the detached watcher now creates the directory itself and prints its path, so the directory only exists once something is already waiting to remove it: * The watcher runs `mktemp -d`, prints the path, then waits for the launcher PID's identity to change and removes the directory. It ignores `SIGPIPE` too, since the launcher can die before reading the path. * `unzip` runs from inside the directory with an absolute path to the zip, so an orphaned `unzip` that outlives a killed launcher can't recreate the directory after the watcher removes it. The watcher retries `rm` until the directory is gone. * The `EXIT` trap is kept so startup failures before `exec` still remove the directory right away. * Persistent extraction (`RULES_PYTHON_EXTRACT_ROOT`) and `RULES_PYTHON_BOOTSTRAP_VERBOSE` behave as before: no watcher. Tests signal the launcher as soon as its extraction directory appears in a private `TMPDIR` (`SIGHUP`, `SIGTERM` and `SIGKILL`, five times each) and check that nothing is left behind, for both `py_zipapp_binary` and the stage1 zip bootstrap. Follow-up to bazel-contrib#4161
|
This PR was prepared with help from an AI coding agent (Claude Code). It ports a fix I had already deployed and tested in a downstream repository. The agent ported and tested it here under my direction, and I reviewed the diff and description before opening the PR. I'll handle review comments myself, and any agent-assisted replies will say so. |
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues for the launcher temporary-directory cleanup changes.
No open issues 🎉
What changed in this PR
This PR closes a cleanup gap in rules_python’s shell ZIP launchers: temporary extraction directories can be left behind when the launcher is killed during unzipping.
Changes:
- Moves temporary-directory creation into the detached cleanup watcher, covering termination before extraction finishes.
- Extracts from inside the temporary directory and retries cleanup so an orphaned
unzipcannot recreate the removed directory. - Adds repeated early-termination tests for both shell launcher paths and documents the fix.
| File | Description |
|---|---|
tests/py_zipapp/zipapp_signals_test.py |
Adds extraction-time cleanup tests for SIGHUP, SIGTERM, and SIGKILL. |
tests/bootstrap_impls/run_binary_zip_yes_test.sh |
Adds equivalent termination tests for the stage1 shell ZIP bootstrap. |
python/private/zipapp/zip_shell_template.sh |
Starts cleanup protection before extraction and extracts within the temporary directory. |
python/private/stage1_bootstrap_template.sh |
Applies the same cleanup lifecycle to the stage1 ZIP bootstrap. |
news/4520.fixed.md |
Documents cleanup after termination during extraction. |
Review effort: Balanced
…s TMPDIR On macOS, `mktemp -d` creates the directory in the per-user temporary directory even when TMPDIR is set, so the tests can't see the launcher's extraction directory appear in a private TMPDIR. Check whether mktemp honors TMPDIR and skip the case if it doesn't.
rickeylev
left a comment
There was a problem hiding this comment.
The PR description body wraps at ~79 columns, exceeding the 72-column commit message limit in .editorconfig and splitting lines into single-word orphans on squash-merge.
Suggested fix: Re-wrap the PR description body at 72 columns on GitHub without amending or rebasing existing commits.
This is an agent-generated comment reviewed and approved by the user.
Guard against an infinite watcher loop when process_identity is empty and prevent early exit flakes in the stage1 signal test. Fall through to kill -0 when ps fails, only spawn the detached watcher when launcher_identity is non-empty, pass unhandled in the stage1 test so the binary blocks until signaled, add ifttt-lint and shell flag comments, and condense the news entry.
The shell zip launchers (
py_zipapp_binaryand--build_python_zipwith
bootstrap_impl=script) leak a partly extracted temporarydirectory if they are killed while still unzipping. #4161 added a
detached watcher that removes the directory once the launcher PID
exits, but the watcher only starts just before
exec, after extractionhas finished. Before that:
zip_shell_template.shcreates the directory withmktemp -d, runsunzip, and only then sets theEXITtrap. ASIGHUP(e.g. closingthe terminal),
SIGTERMorSIGKILLduringunzipleaves thedirectory behind.
stage1_bootstrap_template.shsets the trap beforeunzip, whichcovers
SIGHUPandSIGTERM, but notSIGKILL.For a large app (hundreds of MB), unzipping takes long enough for this
to happen in practice, and each occurrence leaves the partial
extraction in
$TMPDIR.To fix, the detached watcher now creates the directory itself and
prints its path, so the directory only exists once something is already
waiting to remove it:
mktemp -d, prints the path, then waits for thelauncher PID's identity to change and removes the directory. It
ignores
SIGPIPEtoo, since the launcher can die before reading thepath.
unzipruns from inside the directory with an absolute path to thezip, so an orphaned
unzipthat outlives a killed launcher can'trecreate the directory after the watcher removes it. The watcher
retries
rmuntil the directory is gone.EXITtrap is kept so startup failures beforeexecstillremove the directory right away.
RULES_PYTHON_EXTRACT_ROOT) andRULES_PYTHON_BOOTSTRAP_VERBOSEbehave as before: no watcher.Tests signal the launcher as soon as its extraction directory appears
in a private
TMPDIR(SIGHUP,SIGTERMandSIGKILL, five timeseach) and check that nothing is left behind, for both
py_zipapp_binaryand the stage1 zip bootstrap.Follow-up to #4161