Repository navigation
AI-3985: clear the harness after the reply goroutine ends - #2565
Conversation
stopPipeline cleared a.harness while consumeLLM still read replies the model had queued before it closed: a data race (pipeline.go:548 against agent.go:3632 under -race) and, once the read saw nil, a nil dereference in say that ended the call. The stop now waits for consumeLLM before it clears the fields. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
kanat
left a comment
There was a problem hiding this comment.
VERDICT: NO-GO
Reviewed 7d8b53de6 merged onto main b03fcde1d. CI: draft, so go and integration are skipped. They run at gh pr ready. main is red for an unrelated reason: go vet ./internal/api fails at internal/api/http_metrics_test.go:87 (Forward now takes 5 args), and that also fails this PR's go job until main is fixed.
[Should fix] R1.1 (unchanged code, sibling sweep): other goroutines read a.harness without a.mu against the same writer (the clear in stopPipeline). The new wait covers only consumeLLM. The sites:
agent.go:2226perform/ActSupersede:a.harness.CancelDecisionwith no nil check. It is reached fromtranscribed->converse.Observe/overlapon an STT listener goroutine, whichreleasePipelinecloses but does not wait for.native.go:837-853nativeDelegate: the nil check is unlocked, thenCancelSkill/Delegaterun.telephony.go:739-740executeTool: the nil check is unlocked, thenDelegateruns.quiesce(ctx, false)does not wait for a tool still running, so one can finish across a restart.
Probe: perform(ActSupersede) in a loop while stopPipeline(false) runs (-race, -cpu 4). On head, 1/3 runs report a DATA RACE (write pipeline.go:555, read agent.go:2226), and 182 of 200 calls panic on the nil harness. Fix: snapshot a.harness under a.mu and return on nil (the #2525 pattern, which fits these because they are other goroutines). Test row, checked to panic on head and pass with that fix:
// A ruling superseded while the pipeline is stopped finds no harness and is let go.
func (s *AgentSuite) TestASupersedeAfterThePipelineStoppedIsLetGo() {
s.join(false)
s.agent.stopPipeline(false)
s.NotPanics(func() { s.agent.perform(Action{Kind: ActSupersede, TurnID: "candidate"}) })
}Add one row each for nativeDelegate and executeTool.
[Nit] R1.3: the commit trailer names Sonnet 5.5. Set the squash message trailer at merge.
The ordering fix itself is right. A snapshot taken after the clear is nil: on main, a forced-order probe panics in Harness.Filter 3/3. stopPipeline holds no lock while it waits. consumeLLM never takes a.swapping. stopPipeline(true) (pipeline.go:172, native.go:510) only ever stops a native pipeline, where replying is zero. Close (hangup, errors) uses releasePipeline and is unchanged. The wait cannot outlast the old p.running.Wait(). With a voice held in Synthesize, the stop is held on main and on head alike, and returns once the voice is released. In production a restart runs only after quiesce (!generating, utterances == 0), so no heard reply is still queued. What can remain is an abandoned turn's events, which still touched the harness (resetTurn/Release).
Checked: PR test x100 on main: 100/100 fail at -cpu 1 and -cpu 4. On head: 0/100 fail. Free-running probe (200 deltas, stop at the 100th) x100 at -cpu 4: 12/100 races on main (say, resetTurn), 0/100 on head. go test -race ./internal/agent ok (0 races). Mutations: 5 checked, 0 survived (drop Wait, drop Add, drop the guard, drop Done, Wait after the clear).
Verdict: request changes, for R1.1.
| for _, request := range s.voice.spoken() { | ||
| said = append(said, request.Text) | ||
| } | ||
| s.Contains(said, "And then it ended.", "the reply queued at the stop was not read to its end") |
There was a problem hiding this comment.
[Nit] R1.2 This catches the panic only through its effect: the second sentence has to reach a voice that releasePipeline already closed. A fix that drops the queued reply at the stop (also correct) would fail it. The comment at lines 8-10 also says the read is a data race under -race, but the forced order reports 0 races (main, 200/200 runs). Suggest logging to s.logs and asserting no a goroutine panicked record (or that the call did not end), and dropping the race claim. Ticket, not this PR.
There was a problem hiding this comment.
Fixed in 761077d: TestAReplyQueuedWhenThePipelineStopsIsSpokenBeforeTheHarnessIsCleared now logs to s.logs and asserts that no "a goroutine panicked" record was logged. The comment now says that the forced order gives a nil read, not a reported race. With the p.replying.Wait() removed, both assertions fail: "the reply queued at the stop was not read to its end" and "the reply goroutine read the harness the stop cleared".
…nd executeTool A superseded ruling, a delegation and a tool's picture are handled on goroutines the pipeline stop does not wait for, and each read a.harness while stopPipeline cleared it: a data race, and a nil harness panics. Each now takes a copy under a.mu and lets the work go when it is nil, as performing() does for a.performs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The test saw the panic only through the second sentence going missing, which a stop that drops the queued reply would also cause. It now reads the log for the panic itself, and its comment says what the forced order shows: a nil read, not a reported race. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
R1.1 fixed in 3193cfc (head 761077d). Each site now copies
Kill rate, with
Each site reverted alone fails only its own test, with and without I reran |
kanat
left a comment
There was a problem hiding this comment.
VERDICT: GO
Delta round 1, head 761077d (was 7d8b53d), merged onto main 24f671e.
Prior findings
- R1.1 fixed. The three reads (perform/ActSupersede, nativeDelegate, executeTool) copy
a.harnessundera.muand return on nil.- No new lock is taken under
a.mu: every caller ofact()/respondQueued()that I read is outside it,dropSpeculationalready locks it in the same branch, andexecuteTool's copy joins the existing critical section. - The harness-set path calls the same object as before.
- On nil, a superseded ruling is let go, a delegation answers the tool with "agent: no subagent configured" (no hang), and a tool's picture skips the vision hand-off.
- Each place reverted alone fails only its own test under -race. Kill rates over 100 separate -race process runs per cell, with all three places reverted: all 3 tests fail 100/100 at -cpu 1 and -cpu 4, and every run reports a DATA RACE. On the fix: 0/100 at both, 0 races.
stubLLM.readingCapabilitiesexists only inagent_test.go; no production file has it.
- No new lock is taken under
- R1.2 fixed. The comment no longer claims a race, and the panic-log assertion uses the real message (goroutine.go:50). Residual, see R2.1.
- R1.3 stays for the squash message.
New findings
- R2.1 [Nit] inline on harness_stop_test.go:64.
Checks: go test -race -count=1 ./internal/agent ok (354 s) and go vet clean, on the merged tree. CI (Go) is red on main for http_metrics_test.go:87 (#2566, #2568), not this PR. Draft: go and integration are skipped, the rest pass.
Details: reviewer delta-1.md in the ledger.
| said = append(said, request.Text) | ||
| } | ||
| s.Contains(said, "And then it ended.", "the reply queued at the stop was not read to its end") | ||
| s.Empty(s.logs.records("a goroutine panicked"), "the reply goroutine read the harness the stop cleared") |
There was a problem hiding this comment.
Nit, not blocking. The no-panic assertion is a weak detector: stopPipeline does not wait for the reply goroutine (that is exactly what this test pins), so the panic log can land after this line. With p.replying.Wait() commented out, over 40 -race runs the "not read to its end" assertion fires 40/40 and this one 5/40. The test is still killed by the first assertion, so no mutant survives. Optional: wait for the goroutine to end before asserting, or say in the comment that the first assertion is the one that catches it.
Problem
AI-3985, follow-up of #2525.
sayandfinishreada.harnesswithouta.muon theconsumeLLMgoroutine.stopPipeline(false)sets it to nil undera.mu(pipeline.go:548) afterreleasePipelinehas closedreplies, and beforep.running.Wait().repliesholds up to 64 events (replyBuffer), andspeaking()stays true (stopPipelinedoes not clearspeakingTurn), so a reply queued at the stop is still read afterwards.Probe on main
stopPipeline(false)at the 100th,-race, 100 runs at-cpu 4DATA RACE: writepipeline.go:548, readagent.go:3632(say,Filter)-cpu 1Synthesize, one delta queued, stop clears, voice released), 100 runs at-cpu 1and 100 at-cpu 4Filteron a nil harness panics,goroutine.Gorecovers and ends the call; 0 race reports (the read is ordered after the write byspeaking()'s lock)A snapshot under
a.muas in #2525 does not fix the second case: the snapshot is nil. Other reads on the same goroutine also have no lock (resetTurn,finish, thehandlecases).Fix
pipelinegetsreplying, a wait group forconsumeLLM.stopPipelinewaits for it before it clears the fields. The goroutine ends oncereleasePipelineclosedrepliesand the voice, so the wait adds no new blocking:p.running.Wait()waited for it already. The other pipeline goroutines are left alone:consumeCadencereadsa.harness == nilas "gone", so clearing later for all of them would change it.Test
TestAReplyQueuedWhenThePipelineStopsIsSpokenBeforeTheHarnessIsClearedholds the voice, queues a second delta, stops, and asserts the second sentence reaches the voice.-cpu 1-cpu 4TestStoppingAPipelineThatIsAlreadyStoppedReturnscovers thep != nilguard.Mutations: drop
p.replying.Wait(), dropp.replying.Add(1), drop thep != nilguard: each fails a test. Dropdefer p.replying.Done(): the test hangs to its timeout.go test -race ./internal/agent: ok (242 s).🤖 Generated with Claude Code