test(executor): deflake TestQueueTask_ConcurrentDuplicate_DispatchesOnce (GH-4513)#4516
Merged
Conversation
…nce (GH-4513) Root cause: the test wired the dispatcher to NewRunner()'s real ClaudeCodeBackend. The winning QueueTask call's ensureWorker() starts a background ProjectWorker goroutine outside dispatchMu's protection that immediately runs the real backend against the test's non-existent project path; its preflight check fails fast (observed ~0.5-1s locally) and marks the execution row terminal. If that terminal transition landed before all 8 concurrent goroutines made it through dispatchMu's serialized IsTaskQueued check, a "loser" goroutine no longer saw the row as queued/running and fell through to the legitimate dead-claim repick path, minting a second real dispatch and inflating `successes` past 1 — exactly what killed PR #4509/GH-4507 despite that change never touching dispatch concurrency. Fix: swap in a blockingBackend that never returns from Execute until the test tears the dispatcher down (context cancellation), with skipPreflightChecks/config set the same way sibling tests in this file already do for mock backends. This pins the row at "running" for the entire assertion window deterministically, removing the real backend's uncontrolled completion latency as a timing dependency. No assertion was weakened; -count=200 -race and -cpu 1,2,4 both pass.
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes GH-4513:
TestQueueTask_ConcurrentDuplicate_DispatchesOnce/canary-sandbox-style_path(internal/executor/dispatcher_test.go) was flaky and today killed a correct PR (#4509 / GH-4507) via the ci_pre_merge auto-close path.Root cause
The test wired the dispatcher to
NewRunner()— the realClaudeCodeBackend— instead of a mock.dispatchMuonly serializesQueueTask's duplicate check + insert; it does not cover what happens after. The winningQueueTaskcall'sensureWorker()starts a backgroundProjectWorkergoroutine that immediately picks up the freshly-queued row, transitions it to"running", and callsrunner.Executeagainst the test's non-existent project path (/Users/pilot-op/Projects/startups/pilot...). Against the real backend,Execute's preflight check (git statuschdir) fails fast — observed ~0.5-1s locally, confirmed by direct probe — and marks the row terminal ("failed").IsTaskQueuedonly treatsqueued/pending/runningas "active". If that terminal transition landed before all 8 concurrent goroutines made it throughdispatchMu's serialized check, a "loser" goroutine checking afterward no longer saw the row as active and fell through tobeginWithGenerationRetry's legitimate dead-claim repick path — minting a second real dispatch and a second nil-errorQueueTaskreturn, inflatingsuccessespast 1. SincedispatchMufully serializes the 8 calls' fast SQL work but the real backend's failure latency isn't bounded relative to that, this is a genuine race whose outcome depends on CI scheduler/load — exactly the kind of thing that doesn't reproduce reliably locally (confirmed: 100 local runs all passed before the fix) but bites under CI conditions.This is unrelated to PR #4509/GH-4507's actual change (
internal/executor/intent_judge.go, preflight-cap logic) — the subtest failure was pure test-harness flake, not a regression from that PR.Fix
Swap in a
blockingBackend(Executeblocks on<-ctx.Done()) withskipPreflightChecks/configset the same way sibling tests in this file already do for mock backends. This pins the winning execution row at"running"for the entire concurrent-dispatch assertion window deterministically — no sleeps, no timing assumptions — removing the real backend's uncontrolled completion latency as a race variable entirely. The row only transitions oncedispatcher.Stop()cancels the context, which happens after all assertions in both subtests have already completed.No assertion was weakened — both subtests still verify exactly one dispatch out of
concurrency(8) concurrentQueueTaskcalls.Test plan
go build ./...go test ./internal/executor/...(full package, includes new-etc)go test -race -run 'TestQueueTask_ConcurrentDuplicate_DispatchesOnce' -count=200 ./internal/executor/— passesgo test -race -run 'TestQueueTask_ConcurrentDuplicate_DispatchesOnce' -count=50 -cpu 1,2,4 ./internal/executor/— passesgofmt -lclean on the changed file