Skip to content

AI-3985: clear the harness after the reply goroutine ends - #2565

Merged
kanat merged 3 commits into
mainfrom
fix/ai-3985-harness-read-under-lock
Nov 26, 2026
Merged

kanat merged 3 commits into
mainfrom
fix/ai-3985-harness-read-under-lock

Conversation

@kanat

@kanat kanat commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Problem

AI-3985, follow-up of #2525. say and finish read a.harness without a.mu on the consumeLLM goroutine. stopPipeline(false) sets it to nil under a.mu (pipeline.go:548) after releasePipeline has closed replies, and before p.running.Wait(). replies holds up to 64 events (replyBuffer), and speaking() stays true (stopPipeline does not clear speakingTurn), so a reply queued at the stop is still read afterwards.

Probe on main

Probe Result
Free-running: 200 deltas, stopPipeline(false) at the 100th, -race, 100 runs at -cpu 4 1 DATA RACE: write pipeline.go:548, read agent.go:3632 (say, Filter)
Same, 100 runs at -cpu 1 0 reports
Forced order (voice held in Synthesize, one delta queued, stop clears, voice released), 100 runs at -cpu 1 and 100 at -cpu 4 200/200: Filter on a nil harness panics, goroutine.Go recovers and ends the call; 0 race reports (the read is ordered after the write by speaking()'s lock)

A snapshot under a.mu as in #2525 does not fix the second case: the snapshot is nil. Other reads on the same goroutine also have no lock (resetTurn, finish, the handle cases).

Fix

pipeline gets replying, a wait group for consumeLLM. stopPipeline waits for it before it clears the fields. The goroutine ends once releasePipeline closed replies and the voice, so the wait adds no new blocking: p.running.Wait() waited for it already. The other pipeline goroutines are left alone: consumeCadence reads a.harness == nil as "gone", so clearing later for all of them would change it.

Test

TestAReplyQueuedWhenThePipelineStopsIsSpokenBeforeTheHarnessIsCleared holds the voice, queues a second delta, stops, and asserts the second sentence reaches the voice.

-cpu 1 -cpu 4
main 100/100 fail 100/100 fail
fix 0/100 fail 0/100 fail

TestStoppingAPipelineThatIsAlreadyStoppedReturns covers the p != nil guard.

Mutations: drop p.replying.Wait(), drop p.replying.Add(1), drop the p != nil guard: each fails a test. Drop defer p.replying.Done(): the test hangs to its timeout.

go test -race ./internal/agent: ok (242 s).

🤖 Generated with Claude Code

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 kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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:2226 perform / ActSupersede: a.harness.CancelDecision with no nil check. It is reached from transcribed -> converse.Observe/overlap on an STT listener goroutine, which releasePipeline closes but does not wait for.
  • native.go:837-853 nativeDelegate: the nil check is unlocked, then CancelSkill / Delegate run.
  • telephony.go:739-740 executeTool: the nil check is unlocked, then Delegate runs. 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")

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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".

kanat and others added 2 commits November 26, 2026 14:27
…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>
@kanat

kanat commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

R1.1 fixed in 3193cfc (head 761077d). Each site now copies a.harness under a.mu and stops on nil, the same inline lock-copy-unlock that delegating() and ask() use:

Site Change Test
perform / ActSupersede (agent.go) copies the harness, returns on nil TestASupersedeAfterThePipelineStoppedIsLetGo (your row)
nativeDelegate (native.go) the nil check moves under the lock, and CancelSkill/Delegate use the copy TestADelegationAsThePipelineStopsFindsNoSubagent: the test holds a.mu until the delegation waits on it, then clears the harness
executeTool (telephony.go) copies in the lock block that already computes charged, then the vision check and Delegate use the copy TestAToolsPictureAsThePipelineStopsIsNotHandedToAClearedHarness: stopPipeline(false) runs from the model's Capabilities(), which looksItself reads between the check and the Delegate

Kill rate, with -race and 100 separate runs per cell (head = the three sites as on 7d8b53d):

-cpu 1 -cpu 4
head 100/100 fail, for each of the 3 tests 100/100 fail, for each of the 3 tests
fix 0/100 0/100

Each site reverted alone fails only its own test, with and without -race: perform panics, nativeDelegate panics (plus a reported race under -race), and executeTool panics. Dropping only the lock around the nativeDelegate copy also fails, because the stale harness is used.

I reran grep -n 'a\.harness\.[A-Z]\|a\.harness [!=]= nil'. Every other hit is under a.mu or on the consumeLLM path that this PR's wait covers. go test -race ./internal/agent passes (227 s).

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.harness under a.mu and return on nil.
    • No new lock is taken under a.mu: every caller of act()/respondQueued() that I read is outside it, dropSpeculation already locks it in the same branch, and executeTool'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.readingCapabilities exists only in agent_test.go; no production file has it.
  • 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")

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

@kanat
kanat marked this pull request as ready for review November 26, 2026 19:58
@kanat
kanat merged commit 25b4f15 into main Nov 26, 2026
20 of 21 checks passed
@kanat
kanat deleted the fix/ai-3985-harness-read-under-lock branch November 26, 2026 20:52
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