Skip to content

fix(flaky): ubi-runner pack reads the live index with cat, not cp - #1

Draft
vorflux[bot] wants to merge 3 commits into
mainfrom
vorflux/ubi-pack-index-copy-race
Draft

vorflux[bot] wants to merge 3 commits into
mainfrom
vorflux/ubi-pack-index-copy-race

Conversation

@vorflux

@vorflux vorflux Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

ubi-runner pack no longer fails when git rewrites the index during the copy

ubi-runner pack builds the checkout tarball that a Ubicloud VM unpacks. It copied the live git index with cp. When another git process replaced the index at the wrong moment, cp exited 1 and the whole pack failed. This PR reads the index with cat instead.

Why — cp refuses an index that git replaces mid-copy

  • Git writes the index atomically: it writes index.lock, then renames it over index.
  • GNU coreutils cp (9.4 here) stats the source path, then opens it, then compares the two.
  • If the rename lands between the stat and the open, cp prints skipping file ..., as it was replaced while being copied and exits 1.
  • The script runs under set -euo pipefail, so the pack fails. cp never copies a torn index. It refuses a whole, valid new one.
  • Review subagents and trial merges run git in the same checkout during a pack, so this race happens in real runs.

What — the index copy reads one file, once

  • cat opens the index path once and reads that inode to the end. A rename after the open does not affect the read, and a rename before the open gives the complete new index.
  • A failed copy now stops with pack: could not copy the index of <src>, in the same style as the other pack errors.
  • Scope: only the index copy changes. The ref fetch, the staged-blob pack, and the working-tree tar are unchanged. Their own races are listed under Out-of-Scope Feedback.

How — pack flow

---
config:
  flowchart:
    wrappingWidth: 1000
---
flowchart TD
  A["Fetch refs into<br/>a fresh repo"] --> B["Replay remote.*<br/>and branch.* config"]
  B --> C["Read live index<br/>with cat (was cp)"]
  C --> D["Pack staged blobs<br/>HEAD does not have"]
  D --> E["tar working tree<br/>plus snapshot .git"]
  G["git elsewhere renames<br/>index.lock over index"] -. "race" .-> C
Loading
  1. Snapshot refs — fetch every ref into a new bare repository.
  2. Copy index — cat reads the live index once. A concurrent rename cannot make it fail.
  3. Pack staged blobs — read the copied index and pack the blobs no ref reaches.
  4. Tar — write the tracked and untracked files plus the snapshot .git.

In one line: a concurrent git write can no longer make pack fail on the index copy.

Evidence: failure rate before and after

All runs use the real ubi-runner.sh pack against the test's own fixture repository, with git writers that replace the index during each pack. "Before" is the cp version; "after" is this branch. Both variants ran the same number of packs on the same machine (16 cores, git 2.43.0, coreutils 9.4).

Run Writers Packs per variant Before: cp refused the index After: cp/index failures Other failures (before / after)
1 test churn + 3 --index-version togglers 4 × 1000 = 4000 3 (0.075%) 0 0 / 0
2 test churn + 8 update-index --force-write-index loops 4 × 2000 = 8000 15 (0.19%) 0 3 / 5
  • Every "before" index failure had the exact message skipping file ..., as it was replaced while being copied.
  • The "other failures" are all fatal: git upload-pack: not our ref 0000… from the ref fetch, when a ref is deleted mid-fetch. That failure exists on fork main and is not in this PR's scope. 3 vs. 5 is within noise for that rate.
  • Test-level rate: 40 runs each of the strengthened test file with cp and with cat both passed 40/40. The flake rate is too low to see at n=40 at the test level, so the stress harness above is the measurement.

Testing

Check Command Result
Focused file, branch bun test test/ubicloud-runner.test.ts 31 pass, 0 fail, 258 expect() calls, 61.1s
Focused file, upstream main + this patch same, in a worktree of garrytan/gstack main at 5532345 34 pass, 0 fail, 300 expect() calls, 62.9s
Tripwires bun test spawnsync-timeout-tripwire, egress-receipt-wiring, bun-entry-isolation, script-invocation-tripwire 29 pass, 0 fail
Syntax bash -n scripts/ubicloud/ubi-runner.sh ok
Full free suite bun run test (17 shards) exit 1: 31,398 pass, 79 fail, 138 skip, 1 unhandled error between tests. test/ubicloud-runner.test.ts passed in it.

The full free suite is red. This PR is not release-accepted. What I measured about the 79 failures:

  • They are in 38 files, mostly browse/, make-pdf/, and design-lint tests that launch Chromium. Logs show Chromium sandboxing failed! and daemon readiness timeouts in this Linux sandbox.
  • None of the 38 files reference ubi-runner or ubicloud.
  • I ran the same 38 files with this PR's two files reset to fork main (ce3a093): 361 pass, 75 fail. The two runs share 58 failing test names. 5 fail only on fork main, and 5 fail only on the branch.
  • The 4 files with branch-only failures (test/bootstrap-retention-shard.test.ts, test/cso-cli.test.ts, browse/test/xvfb.test.ts, test/autoplan-driver.test.ts) pass alone on the branch: 79 pass, 0 fail. They failed under 16-shard load.
  • My conclusion: the failures come from this environment, not from this change. A CI run on a supported runner must confirm that before the PR ships.

Upstream overlap (garrytan/gstack)

Review

Separate review by Claude Opus 5.5 (claude-opus-5-5, direct API call; the reviewer did not write the change). Verdict: APPROVE WITH NITS. Risk: 2/10.

Addressed in 290ecc0 and 2040565:

  • The comment said cp could copy a torn index. Reworded: cp refuses a whole new index; cat reads the file it opened.
  • || die "pack: could not copy the index of $src" added, so a failed copy names itself.
  • The test churn only rewrote the index when stat data changed. It now uses --force-write-index, so every pass renames a new index over the live one.
  • The test now extracts packs 1 and 2 and runs git ls-files -s and git status --short on each, so "no broken index" is an assertion.
  • The broken-pack case pins the pack: could not copy the index of message.

Not done:

  • "Confirm the coreutils version in the comment." The comment no longer cites a version. I measured 9.4 locally only.
  • "The test proves nothing on macOS or busybox cp." Correct; the regression check is only meaningful with GNU coreutils. Not changed.

Risk Assessment

  • Score: 2/10 (Opus review). The change is one command in one shell function, plus test hardening.
  • cat ... > file creates the copy with the umask mode instead of the source mode. For a normal 0644 index the result is the same. Under core.sharedRepository the tarball gets 0644 instead of 0664; the VM does not depend on that mode.
  • The pack's other races (ref fetch, staged blobs vs. live HEAD, working-tree tar) remain. This PR does not make them worse.
  • Not verified: CI on a supported runner, and a green full free suite.

Out-of-Scope Feedback

Finding Status
ubi-runner pack can lose staged blobs when HEAD moves during the pack Pending
ubi-runner pack fails with 'not our ref' when a ref is deleted during the pack Pending
ubi-runner pack can fail when a working-tree file changes or disappears during tar Pending
ubi-runner pack ignores GIT_INDEX_FILE and some split indexes Pending

Session Details

  • Session: View Session
  • Requested by: System User
  • Address comments on this PR. Add (aside) to your comment to have me ignore it.

vorflux Bot added 3 commits October 30, 2026 16:30
pack copied the checkout's .git/index with cp while other git processes
may rewrite it. git replaces the index through a lockfile rename, and GNU
cp 9 exits 1 when its source is replaced mid-copy ("skipping file ...,
as it was replaced while being copied"). Under set -e that failed the
pack, and it flaked the free test "pack snapshots a plain checkout
without reading its live .git" (flaky-pass on main, 2026-10-30, shard 4).

cat keeps reading the inode it opened, so a rename beside it still
yields one whole index (the old or the new one). A missing index still
fails the pack with exit 1 and names .git/index.
…t/open race

Opus review: cp never copied a torn index; it refused a whole one whose
inode changed between its stat and open. Reword the comment to say so,
and route a failed copy through die like the neighbouring pack steps.
…ks every snapshot

--force-write-index makes each churn pass rename a new index over the
live one, the race GNU cp refused. Packs 1 and 2 are now extracted and
read with ls-files and status, and the missing-index case pins the
pack: prefix.
@vorflux

vorflux Bot commented Nov 2, 2026

Copy link
Copy Markdown
Author

Drift check against upstream garrytan/gstack main dd9834d44d2650818cb6d8e17c57f795cb56bf98 (v1.92.61.0)

I did this check on a local scratch branch. I pushed nothing and did not change this PR branch (head 2040565d).

  • Re-apply: there was a conflict in scripts/ubicloud/ubi-runner.sh. It was in the comment above the index copy, in 0bc200b and 290ecc0. The test commit 2040565 applied cleanly.
    • Upstream already made the main change of this PR in upstream PR 3697 (1cac25cb, v1.92.33.0, merged 2026-11-01): cmd_pack reads the index with cat, not cp.
    • When I keep upstream's side, 0bc200b has nothing left to apply. The rest of this PR is:
      • the || die "pack: could not copy the index of $src" line;
      • the stronger churn test, which renames the index on every pass and checks every snapshot.
  • Focused test: bun test test/ubicloud-runner.test.ts gave 24 pass, 0 fail, including the churn test.
    • The count was 34 at 5532345a. Upstream commit 83ae96e split this test file into several files.
  • cp index-copy sites in ubi-runner.sh at dd9834d4:
    • Line 1149, cmd_pack: this line now uses cat, so cp is gone. Upstream and this PR both cover this site. This PR only adds || die here.
    • Line 344, TREE_SH: cp "$gi" "$idx/index" is still there.
      • This is the same stat/open race on the live index. It runs on the local checkout at line 1296 (TREE for the run key). It also runs on the VM at line 353.
      • This PR does not cover this site. It did not exist on this PR's base ce3a093a.
      • Here a failed cp does not stop the script. I simulated the cp failure: TREE_SH exited 0 with a different tree, because it left out a tracked file that .gitignore matches.
      • A complete fix should cover this site too. A human must decide whether to add it to this PR.
  • Overlap:
    • Upstream PR 3746 is merged (v1.92.41.0). It changed the tar step of cmd_pack, but not the index-copy line.
    • Upstream PR 3756 is merged (v1.92.53.0). It changed the reap and owner code, not the index copy.
    • Upstream issue 3748 is open. Its owner_id 12-character truncation (${raw:0:12}) is now at line 166. It has no overlap with this PR.
    • None of the 76 open upstream PRs changes scripts/ubicloud/ubi-runner.sh.

I have not rebased this PR. A human must decide on the rebase.

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.

0 participants