Repository navigation
fix(review): let offload-review runs for different PRs run in parallel - #1434
Conversation
The fetch wrote the shared ref refs/remotes/origin/<base>. Two runs at
the same time failed with "cannot lock ref". Each run now fetches into
refs/offload-review/pr<N>/{head,base}, and --cleanup deletes them.
A "cannot lock ref" error is tried again, up to 5 times.
The worktree lock now holds its owner's pid. A lock whose owner is
dead, or a lock with no pid older than 60 s, is removed. An EXIT trap
removes a lock this process holds.
Refs #1430
Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.332 | offload-review skill, @amiddavid's session | fresh | 2 of 2 | 1 of 2 | 15d7e8b27acb |
approve |
Verdict: approve
The change works as claimed. Runs for different PRs no longer share a ref, and a stale
worktree lock is removed. I found no defect that breaks a run. The points below are narrow
races and test nits. None blocks the merge.
Findings
None.
Non-blocking
-
scripts/offload-review/lib.sh:30-33 — a three-run race can delete a live holder's lock.
- What can fail: run A reads a dead pid and calls
lock_take_stale. Run B takes the stale
lock over first (rename, remove, newmkdir, new pid). A'smvthen steals B's live lock.
If a third run C locks while A holds the lock renamed away, A'smvback fails and A runs
rm -rf -- "$gone", which deletes B's lock. B and C then both hold it. - Why it does not block: the window is two
mvcalls wide and needs three concurrent runs.
The harm is one failedgit worktree add, the same failure the lock existed to avoid
before this PR. The next run recovers. - Fix: when the
mvback fails, remove$goneonly when its pid is dead, or leave it (it
is a$lockdir.stale.<pid>name and cannot block anyone).
- What can fail: run A reads a dead pid and calls
-
scripts/offload-review/lib.sh:13 —
kill -0cannot tell a dead pid from a pid of another user.- For a pid owned by another user,
kill -0fails with EPERM. The code then treats a live
lock as stale and removes it. - The lock sits in this user's
.git, and the repo rules say the machines run as one user,
so this needs a pid hand-off across users. Note only.
- For a pid owned by another user,
-
scripts/offload-review/lib.sh:22,68 —
BASHPIDneeds bash 4 or newer.- macOS
/bin/bashis 3.2 and has noBASHPID. Underset -uthe name reads as unbound
andlockfails. - It works here:
#!/usr/bin/env bashresolves to Homebrew bash 5.2, first on PATH. This is
the first bash-4 dependency in this script. Worth one line in the header, or$$where
the subshell id does not matter.
- macOS
-
scripts/offload-review_test.sh:96 — the bare
waitblocks about 30 s on thesleep 30fixture.waitwith no argument waits for every job, includinglive(sleep 30 &). The
kill "$live"after it is dead code: the sleep ends first. The test passes, but it spends
half a minute doing nothing.- Fix:
kill "$live"first, thenwait, or wait on the specific pids.
-
scripts/offload-review.sh:8-15 — the new env vars are not in the header.
OFFLOAD_LOCK_TRIESandOFFLOAD_FETCH_RETRY_SLEEPare read in lib.sh but the header
documents only the otherOFFLOAD_*vars. One line each would do.
-
refs/offload-review/pr<N>/* stay until
--cleanup. A PR that never gets a cleanup
leaves two refs behind. They are small and--cleanupremoves them. Note only.
What I checked
- The full diff (4 files) and the whole
scripts/offload-review.sharound each change. - The per-PR fetch: refspecs,
--no-write-fetch-head, the retry on "cannot lock ref", and that
no shared ref (refs/remotes/origin/*,FETCH_HEAD) is written. Two different PRs touch no
common ref. The merge-base base now comes fromrefs/offload-review/pr<N>/base, fetched each
run, so it stays current. - The lock state machine by hand: dead pid, live pid, pidless young lock (age check), pidless
old lock, takeover by rename, restore on pid mismatch, the 60 s wait limit, and
unlock_if_held. The wait paths are bounded byOFFLOAD_LOCK_TRIES; the takeover paths
always make progress. - Bash semantics, verified with small probes on this machine: a subshell and a backgrounded
function do not run the parent's EXIT trap (soattemptandrun_reviewersubshells cannot
delete$skipdiror another process's lock through the new global trap);lockdiris empty
until line 181 andunlock_if_heldguards it; Homebrew bash 5.2 is first on PATH. - The EXIT-trap rewrite: the old per-
skipdirtrap is fully replaced by the line-61 trap;
early exits beforeskipdir=$(mktemp -d)are guarded. - The
--cleanuppath:delete_pr_refsremoves only this PR's two refs, errors are ignored,
and it runs after the open-PR check. - The test file, line by line: the stub
ghanswers every call the--cleanuppath makes; the
backends define only functions, so sourcing the copiedscripts/offload-reviewin the temp
repo is safe; the stubgitretry test counts calls correctly and reaches the real git; the
old-command comparison loop is informational only, as its comment says. - That nothing else in the repo calls the old
lock/unlockororigin/$base_reffor this
script, and that the new test follows the existing by-hand pattern of
scripts/release/changed-since-green_test.sh(no CI wiring for shell tests,make lintis
Go only). - Repo rules: no Go or library change, so no data-dependence row and no behavior-change
approval is needed. The commit has a conventional title,Signed-off-by, andAssisted-By. - No tests were run (live tests not approved). The bash probes above are semantic checks of
the shell, not runs of this repo's test files.
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.332 | offload-review skill, @amiddavid's session | fresh | 1 of 2 | 1 of 2 | 15d7e8b27acb |
blocking |
Verdict: blocking
Findings
- scripts/offload-review/lib.sh:59 — the fetch still writes the shared ref
refs/remotes/origin/<base>, so the cross-PR isolation this PR promises does not hold.- What fails: two runs for different PRs with the same base branch still fight over one ref. Git updates
refs/remotes/origin/mainopportunistically on every fetch ofrefs/heads/main, also with an explicit per-PR destination refspec and--no-write-fetch-head. When that shared ref's lock is held, the whole fetch exits 1 witherror: cannot lock ref 'refs/remotes/origin/main'. This is the exact error the commit message says it removed. Today the 5-try retry hides it, so parallel runs still succeed — but the fix is not structural, and the comment says it is. - Proof (git 2.50.1, probes in a temp clone):
git fetch -q --no-write-fetch-head origin "+refs/pull/1/head:refs/offload-review/pr1/head" "+refs/heads/main:refs/offload-review/pr1/base"movedrefs/remotes/origin/mainfrom the old commit to the fetched one.- The same command with
refs/remotes/origin/main.lockpresent exits 1 and printserror: cannot lock ref 'refs/remotes/origin/main'. - So the claim in lib.sh:52-53 ("It writes neither the shared refs/remotes/origin/* ... runs for different PRs do not fight over a ref lock") is false, and the SKILL.md line "Each run fetches into its own refs" overstates it. The old code had the same shared write, so this is no regression — the change just does not deliver its central claim.
- Fix: add
--refmap=to thegit fetchinfetch_pr_refs. Verified: with--refmap=the same fetch leavesrefs/remotes/origin/mainuntouched, still updates both per-PR refs, and succeeds while the shared ref's lock is held. Then the comment in lib.sh:51-54 and the SKILL.md wording become true. Keep the retry as the backup it says it is.
- What fails: two runs for different PRs with the same base branch still fight over one ref. Git updates
Non-blocking
scripts/offload-review/lib.sh:38— narrow race inlock_take_stale: when the lock it renamed away turns out to be live (pid differs), it moves it back. If that move-back fails because a third process created a new$lockdirin the window, therm -rf -- "$gone"deletes a lock a live holder still believes it holds, and that holder's ownunlockcan then remove the third process's lock. Trigger: a stale lock, three waiters, and a microsecond window. Consequence: two concurrentgit worktree addcalls, which is what the lock prevents. A safer fallback: when the move-back fails, re-read$gone/pidand delete only when it is still the stale pid.scripts/offload-review_test.sh:103(case d) — the test runs the real--cleanupcode with pr=1, which executesrm -f /tmp/offload-review-pr1-rules.mdon the machine that runs the test. It deletes another run's live-test rules file if one is in flight for PR #1. Point the test at a scratch path, or note the side effect in the header.
What I checked
- The full diff (4 files), the whole
scripts/offload-review.shbefore and after,lib.shline by line,SKILL.md, and the backends that the changed code calls. - The test file
scripts/offload-review_test.sh, case by case, including the stubgit/ghpaths, the copiedscripts/tree in case (d), and the glob and ref assertions. I did not run it (live tests not approved). - Bash trap semantics, by probe: subshells, command substitution and backgrounded functions list the inherited EXIT trap but do not run it on exit. So the attempt subshells cannot delete
$skipdirfrom under the parent, and the single EXIT trap at offload-review.sh:61 is safe. - Git fetch semantics, by probe: opportunistic update of remote-tracking refs with explicit refspecs, the failure mode when that ref's lock is held, and that
--refmap=suppresses the shared write (finding 1). bash -non all three changed shell files: clean. No shellcheck gate exists in CI for these files (consistent with the other*_test.shscripts, which also run by hand).- The lock logic: dead-holder takeover, pidless-old takeover, live-holder wait,
unlock_if_heldin the EXIT trap, and the failure message. Traced each against the test cases. - Repo gates: no library or proxy code changes, so no behavior-change note and no data-dependence row is needed. No binaries or LFS files. The commit has
Signed-off-byandAssisted-By.
Review round 1 on #1434 found that git still updates refs/remotes/origin/<base> after a fetch with explicit refspecs. --refmap= stops that, so a held lock on the shared ref no longer fails the fetch. - Add --refmap= to fetch_pr_refs, with a test that holds the lock on refs/remotes/origin/main. The same fetch without --refmap= fails. - lock_take_stale leaves a renamed live lock if the move back fails. - Work with bash 3.2: use BASHPID when it exists, else $$. - Add OFFLOAD_RULES_DIR so the test does not delete a real rules file. - Kill the sleep fixtures before wait in the test. - Document the new variables in the script header. Refs #1430 Assisted-By: Claude Opus 5.5 Signed-off-by: DAVID AMID <[email protected]>
|
Fixed in the new commit.
|
The previous commit read the pid with $(self_pid). A command substitution is a new subshell with another BASHPID on bash 4+, so the exit trap did not remove its own lock. Test (e) failed on bash 5. Refs #1430 Assisted-By: Claude Opus 5.5 Signed-off-by: DAVID AMID <[email protected]>
|
Correction to my last comment. Commit 1 of this round broke test (e) on bash 5: |
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.332 | offload-review skill, @amiddavid's session | resumed | 2 of 2 | 2 of 2 | 9fbe52b965e5 |
approve |
Verdict: approve
Both new commits are correct. The blocking finding from round 1 is fixed and proved by a
test. My probes confirm the two key fixes. What is left are nits. None blocks the merge.
Earlier findings
- Reviewer 1, finding 1 (the fetch still writes
refs/remotes/origin/<base>): fixed.
fetch_pr_refsnow passes--refmap=. I agree with the finding: my round-1 check said
"no shared ref is written", and that was wrong. Git updates the remote-tracking ref
opportunistically also with explicit per-PR refspecs.
My own probe (git 2.50.1, temp clone) confirms the fix: with--refmap=, the fetch succeeds
whilerefs/remotes/origin/main.lockis held, leavesrefs/remotes/origin/mainunmoved, and
updates both per-PR refs. Without--refmap=, the same fetch fails with
error: cannot lock ref 'refs/remotes/origin/main'. Test (f) asserts exactly this, plus the
negative control. The lib.sh comment and the SKILL.md line now say what is true. - My note 1 and reviewer 1's note (three-run race in
lock_take_stale): fixed. When the
move back fails, the renamed lock is removed only when its holder is dead. A live one stays
under its.stale.*name, which blocks nobody. - My note 3 (
BASHPIDneeds bash 4): fixed.self_piduses${BASHPID:-$$}, and the
trade-off on bash 3.2 is documented in the file. - My note 4 (the test's bare
waitstalls 30 s): fixed. The sleep fixtures are killed
beforewait. - My note 5 and reviewer 1's note on
--cleanupdeleting a real/tmprules file:
fixed.OFFLOAD_RULES_DIR(default/tmp) is documented in the header, and the test
uses a scratch folder. The rules file is written by the user, not by the script, so only the
cleanup path needed the variable. - My note 2 (
kill -0cannot tell a dead pid from another user's pid): rejected by the
author, reason: the lock sits in this user's.git. I checked the reason and accept it.
The pid in the file is always written by this user's process. An EPERM can then only mean
the holder died and the pid was reused, where removal is the right outcome. - My note 6 (per-PR refs stay until
--cleanup): rejected, by design. Accepted.
The second commit (9fbe52b)
The fix is right and needed. Commit 6e85ec7 read the pid in unlock_if_held through
$(self_pid). A command substitution is a new subshell with its own BASHPID on bash 4+, so
the trap never matched its own lock. The final code compares against ${BASHPID:-$$} expanded
in the trap's own shell. I probed the final unlock_if_held with the real lib functions under
Homebrew bash 5.2 and /bin/bash 3.2.57: in both, an EXIT trap removes the lock its shell
holds and keeps a lock of another live process.
Findings
None.
Non-blocking
-
scripts/offload-review/lib.sh:45 —
kill -0on an empty pid reads as dead.- If the move back fails and the renamed lock has no pid file yet (a new lock between its
mkdirand its pid write),kill -0 ""fails and the renamed lock is removed. - This is the last sliver of the round-1 race. It needs the full three-run race plus that
microsecond window. Harm is one transientgit worktree addfailure. A[ -n "$now" ]
guard before thekill -0would close it.
- If the move back fails and the renamed lock has no pid file yet (a new lock between its
-
scripts/offload-review/lib.sh:54 — on bash 3.2 only, the main script's EXIT trap can
remove a lock a live attempt subshell holds.- On 3.2 a subshell reports
$$, the script pid, so the main shell and the attempt write
the same pid. A direct kill of the main script duringensure_wtthen frees the lock
while the attempt still works in it. - 3.2-only, needs a signal to the main script alone in a second-wide window, and the next
run recovers. The author already documented the weaker 3.2 pid identity. Note only.
- On 3.2 a subshell reports
-
scripts/offload-review_test.sh:125 — test (d) does not assert the scratch rules file was
removed. It creates$d/rules/offload-review-pr1-rules.mdbut never checks it is gone.
One[ ! -e ... ]would do.
What I checked
- Last review: round 1, 15d7e8b, 2026-11-25T19:58:30Z
- New commits read: 2 (6e85ec7 "stop the fetch from touching the shared origin ref",
9fbe52b "read the lock owner pid in the same shell in the exit trap") - Read in full: 2 reviews, 2 comments, 0 inline comments (from the three files)
- New items since my last review, and what I did about each:
- Reviewer 1's round-1 review (20:00:06Z), blocking on the shared ref: verified the finding
with my own git probe, then verified the--refmap=fix with the same probe. See above. - Author comment (20:02:20Z) with the fix list: checked every bullet in the diff. All match.
- Author comment (20:04:34Z), the correction about
$(self_pid)in the exit trap: read
commit 9fbe52b, then probed the finalunlock_if_heldunder bash 5.2 and 3.2.
- Reviewer 1's round-1 review (20:00:06Z), blocking on the shared ref: verified the finding
- The full new diff and the final files around each change:
lib.shin full,
offload-review.shlines 39-48 and 130-140, the SKILL.md paragraph, and test (f), (c) and
(d) in full. - Test (f) logic by hand: the setup moves origin main forward and resets the local tracking
ref back, so any opportunistic write would show; the assertions cover success under the held
lock, no retry (SECONDS < 3with a 100 s retry sleep), the tracking ref unmoved, the per-PR
base current, and the negative control without--refmap=. - That
OFFLOAD_FETCH_RETRY_SLEEP=100on a function call stays set for the rest of the test
shell (bash keeps such assignments after a function call). No later fetch hits
"cannot lock ref", so it changes nothing. - That the writer of the rules file is the user (SKILL.md), not the script, so the
OFFLOAD_RULES_DIRchange in--cleanupalone is consistent. - Probes run by me (temp dirs, not this repo's test files): the
--refmap=fetch behavior,
and the exit-trap lock removal under two bash versions. I did not run
scripts/offload-review_test.sh(live tests not approved). The author reports it passes on
bash 5 and/bin/bash3.2.57. - Repo rules: no Go or library change, so no data-dependence row and no behavior-change
approval. Both commits have conventional titles,Signed-off-by, andAssisted-By.
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.332 | offload-review skill, @amiddavid's session | resumed | 1 of 2 | 2 of 2 | 9fbe52b965e5 |
approve |
Verdict: approve
Findings
None.
Round-1 findings, resolved
- Mine, blocking (
--refmap=): fixed.fetch_pr_refsnow passes--refmap=(lib.sh:66). The comment in lib.sh and the line in SKILL.md now say what is true. Test (f) holdsrefs/remotes/origin/main.lockwhile origin main moves, and asserts: the fetch succeeds, no retry, the shared ref does not move, and the per-PR base ref does move. Its control runs the same fetch without--refmap=and must fail, so the test can catch the bug. I probed both branches with an empty lock file, as the test creates: without--refmap=the fetch printserror: cannot lock ref 'refs/remotes/origin/main'; with--refmap=it exits 0 and leaves the shared ref untouched. The isolation the PR promises now holds. - Mine, non-blocking (lock race): fixed.
lock_take_stalenow removes a renamed lock only when its holder is dead (lib.sh:44). A live one stays under its.stale.*name. One residual path remains, under Non-blocking. - Mine, non-blocking (test deletes a real
/tmpfile): fixed.--cleanupreads the folder fromOFFLOAD_RULES_DIR(default/tmp), and the test uses a scratch folder. - Reviewer 2, notes 1, 3, 4, 5: fixed. Note 1 is the same race as my item 2. Note 3 (
BASHPIDneeds bash 4):self_pidandunlock_if_helduse${BASHPID:-$$}, which I probed underset -uon/bin/bash3.2.57:BASHPIDis unset there and$$stays the script pid in a subshell, matching the code comment. Note 4 (the barewaitblocked 30 s): the fixture sleep is killed first now. Note 5 (env vars in the header): documented, and the docs match the code. - Reviewer 2, notes 2 and 6: rejected by the author, and I accept the reasons. Note 2:
kill -0cannot tell a dead pid from another user's pid. The lock sits in this user's.git, and only this script writes the pid, so it needs a pid hand-off across users to matter. Note 6: per-PR refs stay until--cleanup. They are two small refs, and--cleanupremoves them.
Non-blocking
scripts/offload-review/lib.sh:54—unlockstill removes$lockdirwithout checking the pid. In the round-1 window (a holder's lock renamed away bylock_take_stale, and the move-back fails because a new lock exists), that holder's ownunlockcan remove the new holder's lock. The direct delete is fixed; this path needs the same three-run window plus the old holder finishing inside it. Fix if you want it closed: makeunlockcheck the pid file asunlock_if_helddoes.scripts/offload-review.sh:136—OFFLOAD_RULES_DIRmoves only the removal path. SKILL.md still tells the caller to create the rules file at/tmp/offload-review-pr<N>-rules.md. A real run withOFFLOAD_RULES_DIRset would not find the file at cleanup. The default matches, and the header says the knob is for the removal folder, so this is a note, not a defect.
What I checked
- Last review: round 1, 15d7e8b, 2026-11-25T20:00:06Z
- New commits read: 2 — 6e85ec7 (stop the fetch from touching the shared origin ref), 9fbe52b (read the lock owner pid in the same shell in the exit trap)
- Read in full: 2 reviews, 2 comments, 0 inline comments (from the three files)
- New items since my last review: the 2 commits above; the author's comment at 20:02:20 (the fix list, checked item by item in the code); the author's comment at 20:04:34 (the correction about commit 6e85ec7 breaking test (e) on bash 5 — I verified the fix:
unlock_if_heldnow expandsBASHPIDdirectly, with no command substitution, which was the bug). Reviewer 2's round-1 review (19:58:30) is older than my last review; I read it in full now and checked every note's resolution above. - The full diff of both commits, and each changed file around the changes:
lib.sh,offload-review.sh,SKILL.md,offload-review_test.sh. - Test (f) traced against the repo state that test (a) leaves: origin main at "one", the lock created empty,
beforeread after the tracking ref is set back one commit, so any shared-ref write would show. Both the assertion and the control match my probes. - The bash-3.2 compatibility of the new code:
${BASHPID:-$$}underset -uprobed on/bin/bash3.2.57;$RANDOMand thelocalforms exist there;bash -npasses on all three files under both bash 5 and/bin/bash3.2.57. - The
--refmap=fix itself, with the probes described in item 1. - Repo gates: still tooling-only, so no behavior-change note and no data-dependence row. Both commits carry
Signed-off-byandAssisted-By. - I did not run the repo's tests (live tests not approved). The author reports they pass with
/bin/bash3.2.57 and the default bash; my check is reading plus probes only.
|
Local tests: waived (owner rule) |
- unlock removes the lock only when the pid file holds this process's pid. It replaces unlock_if_held. - An empty pid file in lock_take_stale means "not yet written", not "dead". The 60 s age rule applies. - The script refuses to run on bash older than 4, with a clear message. Bash 3.2 has no BASHPID, so a subshell cannot be told from the script. - Test (d) checks that --cleanup removes the scratch rules file. - SKILL.md names $OFFLOAD_RULES_DIR as the rules file folder. Refs #1436 Signed-off-by: DAVID AMID <[email protected]> Assisted-By: Claude Opus 5.5
Close the 4 small gaps in the offload-review lock code that the #1434 reviews found. - unlock removes the lock only when its pid file holds this process's pid. - An empty pid file means "not written yet", not "dead". lock_orphan keeps a young orphan and removes one that is 60 s or older, or whose holder is dead. - offload-review.sh needs bash 4 or newer. On bash 3 it stops with a message that names brew install bash. On bash 3.2 a subshell reports the script's pid, so the lock cannot tell its holders apart. - Test (d) checks that the scratch rules file is gone after --cleanup. - SKILL.md names $OFFLOAD_RULES_DIR (default /tmp). Refs #1436 Signed-off-by: DAVID AMID <[email protected]>
Summary
offload-review.shis now safe to run for several PRs at the same time. Tooling only. No product behavior change. No data dependence change.What we found
refs/remotes/origin/<base>. Two fetches at once gave "cannot lock ref".What changed
refs/offload-review/pr<N>/headand/base. It writes no shared ref and no FETCH_HEAD.--cleanupdeletes the per-PR refs.scripts/offload-review/lib.sh, so the test can use them.SKILL.mdsays runs for different PRs are safe in parallel.How it was tested
Tests ran on macOS, Go 1.27.4 (no Go code changed).
scripts/offload-review_test.sh: all pass. It covers 40 parallel per-PR fetches, the retry, a dead lock, an old pidless lock, a live lock, the EXIT trap and--cleanup.bash -nis clean.shellcheckis not installed here.Refs #1430