Skip to content

Interrupted Node.js runtime downloads leave js_runtime/.tmp* directories that are never cleaned up #3626

Description

@hakchin

Summary

When a Node.js runtime download is interrupted (the process gets SIGTERM or SIGKILL), its staging directory $VP_HOME/js_runtime/.tmpXXXXXX/ stays on disk with the partial node-vX.Y.Z-<os>-<arch>.tar.gz in it. Nothing removes it later: a successful install of the same version afterwards leaves the old .tmp* directories in place.

If something keeps calling a shim with a short timeout, the directories add up quickly. On my machine they filled the disk.

Reproduction (vp v1.6.0, macOS 27.2, arm64)

R="$HOME/.local/share/vite-plus/js_runtime"
vp env exec --node 24.22.1 node -v >/dev/null 2>&1 &   # any version not yet installed
pid=$!; sleep 2.5; kill -TERM $pid                      # -KILL gives the same result
sleep 2; ls -A "$R"; ls -la "$R"/.tmp*
# -> .tmpAIgHeO/node-v24.22.1-darwin-arm64.tar.gz (27 MB) remains

vp env exec --node 24.22.1 node -v                      # completes normally
ls -A "$R"
# -> .tmpAIgHeO is still there

How it hit me

A Claude Code status line runs $VP_HOME/bin/node <script> every 5 s. The host kills it when it overruns its budget. When a new Node LTS (26.14.0) was published, the shim resolved it, started downloading it, and was killed on every refresh before it could extract. Over about 4 hours this left 3274 .tmp* directories (~132 GB) in js_runtime/, all holding the same partial tarball. The loop stopped only when one call happened to finish.

Workaround: pin with vp env default <version> so a shim call never starts a download.

Suggested fixes

  1. Clean up stale staging directories: when an install starts (or from vp env clean), remove js_runtime/.tmp* directories that no live process holds. The per-version .lock should already show whether an install is in progress.
  2. Reuse or resume a download that was already partly fetched, instead of starting a new temp directory each time.
  3. Optional: when a shim call would have to download a runtime, print a one-line notice to stderr. Then an unattended caller that keeps re-triggering the download is visible.

Activity

  1. added
    bug
    on Dec 1, 2026
  2. added theissue type on Dec 1, 2026
  3. self-assigned this
    on Dec 1, 2026
  4. voidzero-bot commented on Dec 1, 2026

    @voidzero-bot
    Contributor
    • Reproduced: Yes — on current main (vp v1.8.0), with SIGTERM and SIGKILL mid-download (throttled local mirror via VP_NODE_DIST_MIRROR, since the runner's network finishes a real download in ~3 s)
    • Exploration: Yes — View branch
    • Unit Test: Yes — crates/vp_js_runtime/src/staging.rs (staging cleanup), plus an end-to-end regression test in crates/vp_js_runtime/src/runtime.rs and a CLI snapshot fixture in crates/vp_cli_snapshots/.../command_env_clean/
    • Priority: Priority P2: Has Workaround. The leak only accumulates for callers that repeatedly kill vp mid-download (e.g. status lines with short timeouts), and pinning with vp env default <version> prevents the downloads entirely — but for that usage the disk fills at roughly a gigabyte per hour, so the residue is worth cleaning up.

    The root cause is in crates/vp_js_runtime/src/runtime.rs (~line 241): each runtime download stages into a tempfile::TempDir created directly under $VP_HOME/js_runtime, and cleanup relies solely on the TempDir destructor at normal function exit. SIGTERM/SIGKILL terminate the process without running destructors, so the .tmpXXXXXX directory with its partial tarball survives — and nothing ever removes it: a later successful install of the same version early-returns or publishes via move_to_cache without scanning .tmp*, and vp env clean enumerates only version directories under js_runtime/node, so leftovers are invisible to it. All entry points (the <DATA>/bin/node shim, vp env exec, vp env use/pin, vp self setup) funnel into the same function, so every killed attempt leaks one staging directory — which is how 3274 dirs (~132 GB) piled up in the linked report.

    I found a potential fix for this issue. Staging directories only ever exist while their version's installation lock is held, so the fix names them .tmp-<provider>-<version>-<rand>, removes same-version leftovers under the install lock before each new download (capping residue at one directory and clearing it on the successful attempt), attributes legacy .tmpXXXXXX leftovers via the partial archive inside them, and adds a lock-checked sweep to vp env clean node using a new non-blocking try_lock_exclusive so concurrent live installs are never disturbed. Please review the branch — this needs human review before merging.

    Full Triage Report

    Triage report: issue #3626 — interrupted Node.js runtime downloads leave js_runtime/.tmp* directories

    Step: reproduce — REPRODUCED (Bug, high confidence)

    Issue summary. When vp env exec --node <version> (or a shim call resolving an uninstalled version) is interrupted while downloading the Node.js runtime (SIGTERM/SIGKILL), the staging directory $VP_HOME/js_runtime/.tmpXXXXXX/ with the partial node-vX.Y.Z-<os>-<arch>.tar.gz remains on disk. A later successful install of the same version does not remove the stale .tmp* directories, so repeatedly killed callers accumulate them indefinitely (reporter: 3274 dirs, ~132 GB over 4 hours).

    • Expected: interrupted installs are cleaned up (on the next install and/or via vp env clean).
    • Actual: .tmp* staging dirs persist forever; subsequent successful installs leave them in place.

    Environment: Vite+ vp v1.8.0 built from the current checkout (5cf0b72c8); Node.js v24.22.3 (runner), v24.22.1 (runtime under test); pnpm 12.11.2; Ubuntu Linux x64.

    Relevant source (observations at reproduction time): crates/vp_js_runtime/src/runtime.rs (~line 241) creates the staging directory with TempDir::new_in(&cache_dir) inside download_runtime_with_provider. The tempfile::TempDir destructor only runs on normal unwind/exit; SIGTERM (exit 143) and SIGKILL bypass it, so .tmpXXXXXX under js_runtime/ survives. No startup or vp env clean path scans for stale .tmp* dirs.

    Reproduction method. A fresh VP_HOME was used so the default installation was untouched. The runner's network downloads the ~58 MB tarball in under 3 s, so a real nodejs.org download could not be interrupted reliably; the download was served by a local throttling caching mirror (64 KiB every 0.2 s) via the supported VP_NODE_DIST_MIRROR environment variable. This changes only download speed, not vp behavior.

    Interrupt case (fresh VP_HOME, VP_NODE_DIST_MIRROR=http://127.0.0.1:8137):

    vp env exec --node 24.22.1 node -v &   # vp pid recorded
    # waited until js_runtime/.tmpYrEfOD/node-v24.22.1-linux-x64.tar.gz held 2.2 MiB
    kill -TERM <vp pid>                     # vp exit code 143 (SIGTERM)
    ls -A "$VP_HOME/js_runtime"
    # -> .tmpYrEfOD
    #    node
    ls -la "$VP_HOME/js_runtime/.tmpYrEfOD"
    # -> node-v24.22.1-linux-x64.tar.gz  2,293,760 bytes  (leftover partial archive)

    Follow-up successful install of the same version:

    vp env exec --node 24.22.1 node -v
    # stdout: v24.22.1, exit 0
    ls -A "$VP_HOME/js_runtime"
    # -> .tmpYrEfOD
    #    node          <- stale .tmp dir STILL present after a successful install

    Control case (uninterrupted install in a fresh home, full-speed download): ls -A "$VP_HOME/js_runtime" shows only node — no .tmp* residue. A clean install leaves nothing behind, confirming the leftovers are caused by the interruption and that nothing in the successful-install path removes pre-existing stale staging dirs.

    Note on the very first attempt: killing the wrapper 3 s after starting an unthrottled real nodejs.org download did not reproduce — the install had already completed (exit 0). This was a timing artifact of the fast runner network, not evidence against the report.

    Step: diagnose — root cause, confidence: high

    The runtime install path stages downloads in a tempfile::TempDir created directly inside the cache root and relies only on the process running to completion for cleanup:

    • crates/vp_js_runtime/src/runtime.rs:241 (download_runtime_with_provider): TempDir::new_in(&cache_dir) — cache_dir is <DATA>/js_runtime (crates/vp_js_runtime/src/cache.rs). tempfile 3.27's default builder names the directory .tmp + 6 random alphanumerics, matching the observed .tmpYrEfOD, .tmpVbH7Eu, .tmpe2X91R and the reporter's .tmpAIgHeO.
    • Cleanup happens exclusively through TempDir's Drop guard at function exit. No signal handlers are installed, so SIGTERM (143) and SIGKILL (137) terminate the process without running destructors; the staging directory and partial archive stay on disk. Both signals were tested; both leave residue.
    • Nothing anywhere removes leftover .tmp* dirs:
      • A later successful install takes the try_exists(binary_path) early return or publishes via move_to_cache, which only touches js_runtime/node/<version>. Empirically confirmed: .tmpYrEfOD survived a successful install of the same version.
      • vp env clean (crates/vp_global_cli/src/commands/env/clean.rs, clean_node_runtimes/list_installed_versions) enumerates only version directories under js_runtime/node and package-manager version dirs; .tmp* is invisible to it. Empirically confirmed: vp env clean printed "✓ Removed 0 Node.js runtimes" and both stale .tmp* dirs remained.
    • All entry points funnel into the same function, so every repeatedly-killed caller leaks one staging dir per attempt: the <DATA>/bin/node shim (shim/dispatch.rs:1407), vp env exec (commands/env/exec.rs:166), vp env use/pin (use.rs:320, pin.rs:319), vp self setup (vp_setup/src/install.rs), lifecycle installs. This is exactly the reporter's status-line scenario.

    Additional evidence: kill -KILL mid-download (exit 137) left a partial archive the same way. The content-integrity retry (up to 3 attempts) reuses the same staging dir within one process; the leak is per killed process, not per retry. git log on runtime.rs shows no prior cleanup attempt — an original design gap (EXDEV-safe staging with Drop-only cleanup), not a regression.

    Edge cases considered: concurrent installs of different versions (cleanup must not delete a peer's live staging dir); concurrent installs of the same version (the lock serializes them, so same-version cleanup under the lock is safe); vp env clean racing a live install; Windows remove_dir_all on open handles. A related instance of the same pattern exists in package-manager downloads (vp_pm_cli/src/package_manager.rs:1869, 2057, 2327 stage via tempfile::tempdir_in), so interrupted package-manager installs leak <DATA>/package_manager/.tmp* the same way — same bug class, separate code path.

    Step: verify — verdict: bug, confidence: high

    No source asserts that these staging directories are meant to persist; every relevant artifact expresses the opposite intent:

    1. Design RFC (rfcs/js-runtime.md, "Concurrent Download Protection", from the crate's introduction in 79194a2): the temp dir + atomic rename exist for atomic, concurrent-safe installs; nothing mentions keeping or reusing staging directories.
    2. Stale-artifact hygiene is explicit elsewhere in the same pipeline (crates/vp_js_runtime/src/download.rs:455-465, move_to_cache): "dir without binary → a stale/partial install, remove then move". The codebase deliberately removes stale partial installs under the lock; killed-process staging dirs are the same class of residue, just not covered by that cleanup.
    3. Cleanup intent for the identical pattern in package managers (crates/vp_pm_cli/src/package_manager.rs:1860-1873): "Keep the TempDir guard alive so a failure path cleans up the temp dir". Failure-path cleanup is a stated design requirement; only the signal-kill path escapes the Drop guard.
    4. vp env clean is documented to remove unused installs (docs/guide/env.md:204; help text "Clean all unused managed versions"), yet clean_node_runtimes cannot see .tmp*.
    5. No test, doc, or comment requires the leftovers to survive. gh search found only this issue. This is Vite+'s own runtime-management code, not behavior forwarded from an upstream project.

    Step: fix — implemented on branch triagebot/fix-3626

    Staging directories exist only while their version's installation lock is held (created after lock_installation, consumed by move_to_cache, both under the lock). This makes leftover removal precise and concurrency-safe:

    1. Version-scoped staging names: staging dirs are now created as .tmp-<provider>-<version>-<rand> (previously tempfile's default .tmp<rand>), so a leftover can be attributed without inspecting its contents.
    2. Cleanup on install (remove_stale_staging_dirs): while holding a version's lock, every staging dir for that version is by definition stale and is removed before the new download is staged. Staging dirs of other versions may belong to concurrent live installs and are untouched. In the reporter's loop this caps residue at the one directory the latest kill leaves, and the attempt that finally succeeds clears everything.
    3. Legacy leftovers (Vite+ ≤ 1.8.0, .tmp<rand>): attributed via the partial archive/extracted dir inside them (node-v<version>-<platform>...), and removed by (2) when they name the installed version, or by (4) generally.
    4. vp env clean sweep (clean_interrupted_staging_dirs, wired into the node scope): each staging dir's version lock is taken with a new non-blocking vp_shared::file_lock::try_lock_exclusive; the dir is removed only when the lock is free, so a live install is never disturbed. Empty unattributable dirs are kept. Removals are reported as Removed N interrupted downloads only when N > 0, so existing vp env clean snapshots are unchanged.

    Reporter suggestions 2 (resume partial downloads) and 3 (stderr notice) are enhancements outside this minimal fix. The analogous package-manager staging leak was left as a follow-up.

    Changed files: crates/vp_shared/src/file_lock.rs (non-blocking try_lock_exclusive + tests), new crates/vp_js_runtime/src/staging.rs (naming, install-time cleanup, lock-checked sweep + 5 unit tests), crates/vp_js_runtime/src/runtime.rs (version-scoped prefix, cleanup under the install lock, end-to-end regression test), crates/vp_js_runtime/src/lib.rs (export), crates/vp_global_cli/src/commands/env/clean.rs (sweep + reporting), and a new snapshot fixture case env_clean_interrupted_download_staging.

    Regression tests: unit tests for same-version-only install cleanup (current + legacy formats), lock-protected clean sweep (a staging dir whose lock is held survives and is removed once free), legacy attribution, an end-to-end download test with pre-seeded leftovers, a try_lock_exclusive test, and the CLI snapshot recording ✓ Removed 2 interrupted downloads with the surviving empty leftover.

    Validation:

    • cargo test -p vp_js_runtime — 147 passed (incl. the real-network integration download test); cargo test -p vp_shared -p vp_global_cli — 577 + 151 passed.
    • cargo check --workspace --all-features --all-targets --locked, the just lint clippy command, and cargo fmt --all --check — all clean; no lockfile change.
    • just snapshot-test-global env_clean — all 3 cases pass; existing snapshots byte-identical.
    • End-to-end rerun of the original reproduction with the rebuilt release binary in a fresh isolated VP_HOME against the throttled mirror: SIGTERM mid-download → one .tmp-node-24.22.1-… leftover (the unavoidable one); second SIGTERM attempt → still only one leftover (previously they accumulated); completed install → zero .tmp* dirs; vp env clean node removed 4 seeded legacy leftovers ("✓ Removed 4 interrupted downloads") and kept the unattributable empty one; vp env clean node during a live install left the live staging dir untouched and the install completed successfully; control case (uninterrupted install) → no leftovers.

    This report was made by an LLM. The analysis may be wrong, and the potential fix might not work, but is intended as a starting point for exploring the issue.

    I've opened a pull request with this fix: #3627

  5. added
    - P2: has workaround
    on Dec 1, 2026
  6. added a commit that references this issue on Dec 1, 2026
    25f78f7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

- P2: has workaroundbugtriage: fix verified

Type

Fields

Priority

None yet

Effort

None yet

Target date

None yet

Start date

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions