Skip to content

fix(zipapp): remove the extraction directory when killed while unzipping - #4520

Merged
rickeylev merged 5 commits into
bazel-contrib:mainfrom
gfrankliu:zipapp-cleanup-during-extract
Nov 22, 2026
Merged

rickeylev merged 5 commits into
bazel-contrib:mainfrom
gfrankliu:zipapp-cleanup-during-extract

Conversation

@gfrankliu

@gfrankliu gfrankliu commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

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. #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 #4161

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
Copilot AI balanced review requested due to automatic review settings November 17, 2026 08:00
@gfrankliu

Copy link
Copy Markdown
Contributor Author

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 unzip cannot 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.
Comment thread news/4520.fixed.md Resolved
Comment thread news/4520.fixed.md Resolved

@rickeylev rickeylev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@rickeylev
rickeylev added this pull request to the merge queue Nov 22, 2026
Merged via the queue into bazel-contrib:main with commit 39e16e8 Nov 22, 2026
15 checks passed
@gfrankliu
gfrankliu deleted the zipapp-cleanup-during-extract branch November 25, 2026 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants