Skip to content

fix(stub): extract cache entries to a temp dir and rename into place - #78

Merged
avelino merged 3 commits into
mainfrom
avelino/fix/stub-extract-race
Oct 16, 2026
Merged

avelino merged 3 commits into
mainfrom
avelino/fix/stub-extract-race

Conversation

@avelino

@avelino avelino commented Oct 12, 2026 •

Copy link
Copy Markdown
Owner

What

The launcher stub extracted the runtime straight into ~/.jbundle/cache/rt-<hash> and checked for bin/ to decide it was done. When two copies of the same binary start at once on an empty cache, the second one sees bin/ while lib/ is still being written and runs a half-extracted java. That's the flake we've been seeing in the e2e job.

Now every cache entry (runtime, app.jar, CRaC checkpoint) is extracted into a private temp path next to its final spot and renamed into place:

  • runtime and CRaC: mktemp -d then mv. If another copy won the race, the loser's dir is cleaned up (including the case where mv drops it inside the existing one).
  • app.jar: decompressed to a temp file in the same dir, then mv -f, which is atomic.
  • the cache hit check is now bin/java being executable, and a leftover rt-<hash> without bin/ from an older stub gets cleared before retrying.

How it was tested

  • New test concurrent_first_runs_never_see_partial_extraction: packs a real binary with a fake runtime (a bin/java script that fails unless the last lib/ file and the full app.jar are there), starts 4 copies at once on an empty HOME, 5 rounds, and checks nothing is left behind in the cache.
    • Against the stub on main: fails 3 out of 3 runs.
    • With this change: passes 10 out of 10 runs.
  • cargo test: 190 unit + 2 e2e passed (the e2e cases skip here, no JDK on this machine, so CI is the real check for them).
  • cargo clippy --all-targets -- -D warnings and cargo fmt --check clean.

Not tested: a real macOS run by hand. The test runs on the macOS CI job, and mktemp -d <template> is supported there.

Real-world confirmation: on the same day, the e2e job on #62 (twice) and #79 (once) failed with could not open .../.jbundle/cache/rt-<hash>/lib/jvm.cfg, which is exactly this race. This branch's own CI run passed on all three OSes.

Copilot AI balanced review requested due to automatic review settings October 12, 2026 16:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Cache cleanup still contains deletion races, incomplete-cache detection is insufficient, and failed extraction can leak temporary artifacts.

1 open finding
What changed in this PR

Updates launcher cache extraction to reduce concurrent first-run failures.

Changes:

  • Extracts runtime, application, and CRaC data through temporary paths.
  • Atomically publishes completed cache entries.
  • Adds concurrent extraction regression tests.
File Description
src/​pack/​stub.rs Implements temporary extraction, publication, cleanup, and concurrency tests.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/pack/stub.rs Resolved
@avelino

avelino commented Oct 12, 2026

Copy link
Copy Markdown
Owner Author

Going through the rest of the Copilot summary on 6c0938b: the leak on failed extraction was real, a broken tar or gzip under set -e left the temp dir in the cache. Fixed in 587d571, with a test that feeds a corrupt payload and checks the cache is empty afterwards. The incomplete-cache detection is covered by the marker in 4f5533f. The remaining deletion race is narrow on purpose: a markerless entry only gets moved aside right before our own rename, and a complete entry is never touched, so a copy that is already running keeps its runtime.

Two copies of the same binary started on an empty cache could race: the
second saw bin/ while lib/ was still being written and launched a broken
runtime. Runtime, app.jar and CRaC checkpoint now extract into a private
temp path and get renamed into the cache, so a reader only ever sees a
complete entry.

Signed-off-by: Avelino <31996+avelino@users.noreply.github.com>
bin/java existing doesn't prove the runtime is whole: a run killed
mid-extraction, or an older stub that extracted in place, leaves bin/
with lib/ half written. Each entry now gets .jbundle-complete written
in the temp dir after extraction succeeds, and markerless entries are
moved aside and rebuilt once.

Signed-off-by: Avelino <31996+avelino@users.noreply.github.com>
Under set -e a failed tar or gzip exited the stub with its temp dir or
temp jar still in the cache. Clean it up before exiting.

Signed-off-by: Avelino <31996+avelino@users.noreply.github.com>
@avelino
avelino force-pushed the avelino/fix/stub-extract-race branch from 587d571 to a315965 Compare October 16, 2026 11:03
@avelino
avelino merged commit 6ea11f7 into main Oct 16, 2026
9 checks passed
@avelino
avelino deleted the avelino/fix/stub-extract-race branch October 16, 2026 11:42
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.

2 participants