Skip to content

Fix race in concurrent uv bootstrap on hosts without uv on PATH - #139

Merged
OwenPriceSkelly merged 2 commits into
mainfrom
owen/fix-uv-bootstrap-race
Sep 2, 2026
Merged

OwenPriceSkelly merged 2 commits into
mainfrom
owen/fix-uv-bootstrap-race

Conversation

@OwenPriceSkelly

Copy link
Copy Markdown
Member

Problem

Spotted while verifying #138: on a host with no uv on PATH, the shell command's bootstrap block runs $PYTHON -m pip install uv on every task, into the same shared location (the user site). pip installs aren't concurrency-safe, so concurrent tasks can observe — and try to execute — a half-written uv binary. Observed as .../bin/uv: Permission denied when 6 tasks bootstrapped at once. It's rarer in practice than the env-creation race (#138) because deployed endpoints usually have uv on PATH, but any uv-less host running concurrent tasks can hit it.

Fix

Same publish-by-rename pattern as #138:

  • Bootstrap installs with pip install --target into a unique temp dir ($UV_BOOT.tmp.$(hostname).$$), then atomically renames it to a persistent per-user dir under GROUNDHOG_CACHE_BASE (.../uv-bootstrap-$(uname -m); the arch suffix keeps installs from colliding when the cache base is shared across architectures). Only a working install (-x bin/uv) is published; the loser of a publish race discards its copy and uses the winner's.
  • Once published, later tasks skip pip entirely — previously every task re-ran pip install uv even when it was already installed.
  • The uv.find_uv_bin() discovery is kept as a fallback (e.g. the uv PyPI package already installed some other way), and command -v uv short-circuits everything exactly as before.
  • The GROUNDHOG_CACHE_BASE block moves above uv resolution, since the bootstrap dir lives under it. No other reordering.

Independent of #138 (different section of the template); both apply cleanly.

Verification

  • Full suite passes (357 tests), plus new tests asserting the bootstrap installs to the private target dir, publishes by rename, discards on a lost race, and that the cache base is computed before uv resolution.
  • End-to-end: ran the bootstrap section of a real rendered shell command 6× concurrently with uv stripped from PATH and a cold cache — all 6 succeeded and resolved a working uv 0.12.8 (previously this exact setup produced Permission denied), leaving exactly one published uv-bootstrap-arm64 dir and no temp leftovers. A subsequent warm run reused it without invoking pip.

🤖 Generated with Claude Code

OwenPriceSkelly and others added 2 commits September 2, 2026 10:33
On a host with no uv on PATH, every task ran 'pip install uv' into the
same (user site) location. pip installs aren't concurrency-safe, so
concurrent tasks could observe -- and try to execute -- a half-written
uv binary ('Permission denied').

Bootstrap now installs with 'pip install --target' into a unique temp
dir (hostname + pid) and renames it into place under
GROUNDHOG_CACHE_BASE, publishing only a working install; the loser of
a publish race discards its copy and uses the winner's. The bootstrap
dir is suffixed with 'uname -m' so installs sharing a filesystem
across architectures don't collide, and once published, later tasks
skip pip entirely (previously every task re-ran it). The
uv.find_uv_bin() discovery is kept as a fallback.

The GROUNDHOG_CACHE_BASE block moves above uv resolution since the
bootstrap dir lives under it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…stale installs

- Validate uv by running it (--version) instead of trusting command -v /
  mode bits at every stage: PATH fast path (a concurrently pip-installed
  uv can be half-written), reuse gate, publish gate, and final selection.
- Repair a stale $UV_BOOT (scratch purges delete bin/uv but leave the
  dir): clear broken leftovers before publishing instead of letting the
  [ -d ] guard discard every good rebuild forever.
- Salt the bootstrap dir with the groundhog version
  (uv-bootstrap-<arch>-<version>) so upgrades re-bootstrap a fresh uv;
  moved the GROUNDHOG_VERSION assignment above the uv resolution block.
- Avoid temp-dir collisions across PID namespaces by appending $RANDOM
  to UV_BOOT_TMP, and extend the EXIT trap to clean up UV_BOOT_TMP so a
  hard-killed bootstrap doesn't leak it.
- Docs: note that the per-user uv bootstrap dir also lives under
  GROUNDHOG_CACHE_DIR.
- Tests: cover version salt, --version validation at each gate,
  stale-dir repair ordering, trap coverage, and new tmp naming.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@OwenPriceSkelly
OwenPriceSkelly force-pushed the owen/fix-uv-bootstrap-race branch from 626a9fc to 222c454 Compare September 2, 2026 15:33
@OwenPriceSkelly
OwenPriceSkelly merged commit 7539f0f into main Sep 2, 2026
2 checks passed
@OwenPriceSkelly
OwenPriceSkelly deleted the owen/fix-uv-bootstrap-race branch September 2, 2026 15:38
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.

1 participant