Skip to content

test(git): pin FETCH_HEAD lock order in the admission lifetime test - #17641

Merged
nwparker merged 1 commit into
stablyai:mainfrom
nwparker:nwparker/fix-git-admission-lifetime-flake
Aug 31, 2026
Merged

nwparker merged 1 commit into
stablyai:mainfrom
nwparker:nwparker/fix-git-admission-lifetime-flake

Conversation

@nwparker

@nwparker nwparker commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Root cause

git-exec-admission-lifetime.test.ts > serializes FETCH_HEAD callers before they enter admission assumed that two same-repo fetches issued back to back join the FETCH_HEAD lock lane in call order. They do not.

runWithGitFetchHeadLock first awaits fetchLockPath, which walks the filesystem — realpath(worktree), a stat per parent directory up to /, readFile of commondir, then realpath again — and only calls runWithGitOperationLock (which registers the lane synchronously) after that walk resolves. For the test's non-existent /repo that is five libuv threadpool round-trips per caller. The two callers run their chains concurrently, so lane order is threadpool completion order, not call order.

When the interactive fetch won that race, it entered the lock lane ahead of the background fetch. As soon as the first fetch released the lock, interactive reached admission, and because its tier is interactive it took the free network headroom slot immediately instead of queueing — while the background fetch stayed parked on the lock and never entered admission at all. So queued settled at 0 and never reached the asserted 1, and vi.waitFor timed out.

Instrumented failure state confirms it exactly:

DEBUG callbacks [ 'first', 'interactive' ]
{"queued":0,"budgets":{"network":{"baseUsed":1,"headroomUsed":1}}}

This is a test-premise defect, not a scheduler bug. The scheduler behaved correctly in both orderings; only one of the two orderings matches what the test asserts.

Evidence it was a flake, not a regression

Fix

Fix the premise rather than the symptom — no timeout bump, no retry, no weakened assertion. The test file now stubs only the FETCH_HEAD key derivation, delegating to the real runWithGitOperationLock. The FIFO lock that the test actually exercises stays real, and the lane is now registered synchronously with the call, so lock order is call order.

Key derivation keeps its own coverage in src/shared/git-fetch-head-lock.test.ts; this file has never been the place that verifies it. Two incidental improvements fall out: the fetch tests in this file no longer share one global /.git/FETCH_HEAD lane with each other, and they no longer issue real filesystem syscalls against / from a unit test.

Determinism proof

Scenario Result
git-exec-admission-lifetime.test.ts alone 40/40 pass
Same, after the final refactor 30/30 pass
Same, under load — 12 CPU hogs + a concurrent src/main/git/command-runner/ vitest run 25/25 pass
Full src/main/git/command-runner/ directory 9 files, 57 passed / 1 skipped
src/shared/git-fetch-head-lock.test.ts (unaffected) 12 passed

pnpm exec oxlint on the changed file and pnpm tc both clean.

Follow-up worth considering (not in this PR)

The same fs-walk race exists in production: two same-repo fetches acquire the FETCH_HEAD lock in threadpool completion order rather than call order. It is not a correctness bug — mutual exclusion still holds — and the exposure is narrow: it only shuffles arrivals issued within the same sub-millisecond window, since outside that window the walk completes in call order anyway.

Worth being precise about what a fix would and would not buy, so nobody over-values it: runWithGitOperationLock is pure FIFO with no tier awareness at all, so the FETCH_HEAD lock already sits as a priority inversion in front of the tiered, aging-aware admission scheduler by design — an interactive fetch issued after a background fetch on the same repo waits behind it regardless. Memoizing the in-flight lock-path resolution per worktree would make lane order deterministic and skip a repeated 5-syscall walk per fetch; it would not restore tiered priority. That would take a tier-aware FETCH_HEAD lock, which is a much larger design. Left out here because sharing an in-flight resolution across callers with different AbortSignals needs its own design too.

`serializes FETCH_HEAD callers before they enter admission` assumed that two
same-repo fetches join the FETCH_HEAD lock lane in call order. They do not.

`runWithGitFetchHeadLock` first `await`s `fetchLockPath`, which walks the
filesystem (`realpath`, `stat` per parent directory, `readFile` of `commondir`,
`realpath` again) before it calls `runWithGitOperationLock`, and the lane is
registered only after that walk resolves. For a non-existent `/repo` that is
five libuv threadpool round-trips per caller. Two callers issued back to back
run their chains concurrently, so lane order is threadpool completion order,
not call order.

When the `interactive` fetch won that race it entered the lane ahead of the
`background` fetch. On the first caller's release it reached admission
immediately and, being interactive, took the free network headroom slot instead
of queueing, while the background fetch stayed parked on the lock. `queued`
therefore settled at 0 and never reached the asserted 1. Measured inversion
rate for the bare lock-path walk was 54/500 on an idle machine; the test itself
failed 5/20 locally, always at the same assertion, matching the two CI failures
on unrelated PRs (stablyai#17530, stablyai#17630) at the same line.

Fix the premise rather than the symptom: stub only the key derivation, keeping
the real FIFO `runWithGitOperationLock` that the test actually exercises, so the
lane is registered synchronously with the call. Key derivation keeps its own
coverage in `src/shared/git-fetch-head-lock.test.ts`. This also stops the fetch
tests in this file from sharing one global `/.git/FETCH_HEAD` lane with each
other and from touching the real filesystem.

Verified deterministic: 40/40, then 30/30 clean runs, plus 25/25 with twelve CPU
hogs and a concurrent `src/main/git/command-runner/` run saturating the box.
@greptile-apps

greptile-apps Bot commented Aug 31, 2026

Copy link
Copy Markdown

Greptile Summary

The PR makes FETCH_HEAD admission-lifetime tests deterministic by mocking only asynchronous lock-key derivation while retaining the real FIFO operation lock.

  • Replaces filesystem-derived FETCH_HEAD keys with synchronous per-worktree test keys.
  • Preserves the lock ordering and admission behavior exercised by the existing tests.
  • Avoids filesystem access and accidental global lock-lane sharing in this test file.

Confidence Score: 5/5

The PR appears safe to merge with no actionable correctness or test-validity issues identified.

The mock keeps the real operation-lock implementation, and every affected test uses identical worktree paths for callers expected to share a FIFO lane.

Important Files Changed

Filename Overview
src/main/git/command-runner/git-exec-admission-lifetime.test.ts Adds a narrowly scoped module mock that preserves the tested FIFO lock semantics for all current same-path fetch scenarios.

Reviews (1): Last reviewed commit: "test(git): pin FETCH_HEAD lock order in ..." | Re-trigger Greptile

@coderabbitai

coderabbitai Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0d233897-ed8a-40be-9e7a-6304c467cee9

📥 Commits

Reviewing files that changed from the base of the PR and between 6aba202 and 3e62a18.

📒 Files selected for processing (1)
  • src/main/git/command-runner/git-exec-admission-lifetime.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 2 remain after this review.


📝 Walkthrough

Walkthrough

The test adds a type-only import for the Git FETCH_HEAD lock module. It mocks runWithGitFetchHeadLock while preserving the module's other exports. The mock delegates to runWithGitOperationLock, so concurrent same-repository callers use the real FIFO lock behavior without filesystem-based key derivation.

Merge Risk: ⚪ Minimal · up to 3e62a

This change makes the Git admission lifetime test deterministic without changing production behavior or user-facing functionality. No actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description gives strong root-cause, fix, and validation details, but it does not follow the required template. It omits the required Linked Issue, Visual Proof with an explicit N/A statement, Tes… Update the description to include all required template sections. Add the issue link after Fixes #, state N/A with a reason under Visual Proof because this is not a UI change, complete the Testing checklist, and address the remaining Re…
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the test change and the removal of the FETCH_HEAD lock-order flake. It is concise and specific.
Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1 files.

Full details: Description check

Explanation

The description gives strong root-cause, fix, and validation details, but it does not follow the required template. It omits the required Linked Issue, Visual Proof with an explicit N/A statement, Testing checklist, AI Disclosure, Review, Agent skill upstream boundary, Notes, and Checklist sections.

Resolution

Update the description to include all required template sections. Add the issue link after Fixes #, state N/A with a reason under Visual Proof because this is not a UI change, complete the Testing checklist, and address the remaining Review, Agent skill upstream boundary, Notes, and Checklist sections.

  • Fix all pre-merge checks with AI

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ No new issues found.

Reviewed changes

  • Pin FETCH_HEAD lock order in the admission lifetime test — the test now mocks runWithGitFetchHeadLock to delegate straight to runWithGitOperationLock(worktreePath, signal, run), so same-repo fetch callers register the lock lane synchronously in call order instead of in libuv threadpool completion order.

The fix is sound: the FIFO lock under test stays real (only the fs key walk is stubbed), resolveGitFetchHeadCommand is preserved through ...actual, and the key derivation retains its own coverage in src/shared/git-fetch-head-lock.test.ts (including linked-worktree and symlink serialization). The two tests that exercise the lock (/repo and /same-repo) still share a lane via their cwd key, so the serialization premise holds. Verified locally: the file passes 10/10 consecutive runs, and oxlint reports 0 warnings/errors on the changed file.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@nwparker
nwparker merged commit ca516a4 into stablyai:main Aug 31, 2026
30 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…tablyai#17641)

`serializes FETCH_HEAD callers before they enter admission` assumed that two
same-repo fetches join the FETCH_HEAD lock lane in call order. They do not.

`runWithGitFetchHeadLock` first `await`s `fetchLockPath`, which walks the
filesystem (`realpath`, `stat` per parent directory, `readFile` of `commondir`,
`realpath` again) before it calls `runWithGitOperationLock`, and the lane is
registered only after that walk resolves. For a non-existent `/repo` that is
five libuv threadpool round-trips per caller. Two callers issued back to back
run their chains concurrently, so lane order is threadpool completion order,
not call order.

When the `interactive` fetch won that race it entered the lane ahead of the
`background` fetch. On the first caller's release it reached admission
immediately and, being interactive, took the free network headroom slot instead
of queueing, while the background fetch stayed parked on the lock. `queued`
therefore settled at 0 and never reached the asserted 1. Measured inversion
rate for the bare lock-path walk was 54/500 on an idle machine; the test itself
failed 5/20 locally, always at the same assertion, matching the two CI failures
on unrelated PRs (stablyai#17530, stablyai#17630) at the same line.

Fix the premise rather than the symptom: stub only the key derivation, keeping
the real FIFO `runWithGitOperationLock` that the test actually exercises, so the
lane is registered synchronously with the call. Key derivation keeps its own
coverage in `src/shared/git-fetch-head-lock.test.ts`. This also stops the fetch
tests in this file from sharing one global `/.git/FETCH_HEAD` lane with each
other and from touching the real filesystem.

Verified deterministic: 40/40, then 30/30 clean runs, plus 25/25 with twelve CPU
hogs and a concurrent `src/main/git/command-runner/` run saturating the box.
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