gateway: a turn's end reads its length and hook as the reply ends, not in its timer (race in 7cba316b9's tests)

CI's -race run (38146011985, ubuntu and macOS) caught TestTurnDoneSaidAtTheTurnsEnd and TestTurnDoneWaitsForEveryRequest: the quiet timer read turnAfter and TurnDone on its own goroutine while a test put them back or changed the length. end() now reads both as the reply ends and hands them to the timer; the tests change the length through an atomic and wait out a timer still going before restoring the hooks.

Run: go test -race -count=5 ./internal/gateway -run 'TestTurnDone|TestToolScan' (ok, 37s); gofmt -l clean.
This commit is contained in:
yetone
2026-10-11 22:34:41 +08:00
parent 234b2399c4
commit a2afc18282
2 changed files with 22 additions and 17 deletions
+4 -6
View File
@@ -129,7 +129,8 @@ func (ts *turns) end(r *Route, final bool, now time.Time) {
if st.going > 0 || !final {
return
}
k, agent, session := sessionTurnKey(r), r.Agent, r.Session
// read here, not in the timer: what is in force as the reply ends
k, agent, session, after, done := sessionTurnKey(r), r.Agent, r.Session, turnAfter(), TurnDone
var t *time.Timer
t = time.AfterFunc(turnQuiet, func() {
ts.mu.Lock()
@@ -140,11 +141,8 @@ func (ts *turns) end(r *Route, final bool, now time.Time) {
}
delete(ts.m, k)
ts.mu.Unlock()
took, after := cur.last.Sub(cur.start), turnAfter()
if after > 0 && took >= after {
if f := TurnDone; f != nil {
f(TurnEnd{Agent: agent, Session: session, Folder: cur.folder, Took: took})
}
if took := cur.last.Sub(cur.start); after > 0 && took >= after && done != nil {
done(TurnEnd{Agent: agent, Session: session, Folder: cur.folder, Took: took})
}
})
st.timer = t
+18 -11
View File
@@ -12,17 +12,24 @@ import (
"github.com/yetone/magpie/internal/provider"
)
// turnsFollowed has turns followed for a test: every turn long enough,
// a short quiet, and each turn's end sent on the channel returned.
func turnsFollowed(t *testing.T, after time.Duration) chan TurnEnd {
// turnsFollowed has turns followed for a test: a turn as long as the
// length returned (set it to change), a short quiet, and each turn's end
// sent on the channel returned. The test waits out a timer still going
// before the hooks are put back.
func turnsFollowed(t *testing.T, after time.Duration) (chan TurnEnd, *atomic.Int64) {
t.Helper()
ends := make(chan TurnEnd, 8)
length := new(atomic.Int64)
length.Store(int64(after))
oldDone, oldAfter, oldQuiet := TurnDone, turnAfter, turnQuiet
TurnDone = func(e TurnEnd) { ends <- e }
turnAfter = func() time.Duration { return after }
turnAfter = func() time.Duration { return time.Duration(length.Load()) }
turnQuiet = 50 * time.Millisecond
t.Cleanup(func() { TurnDone, turnAfter, turnQuiet = oldDone, oldAfter, oldQuiet })
return ends
t.Cleanup(func() {
time.Sleep(4 * turnQuiet)
TurnDone, turnAfter, turnQuiet = oldDone, oldAfter, oldQuiet
})
return ends, length
}
// noEnd fails when a turn's end is said within a while: four times the
@@ -95,7 +102,7 @@ func TestTurnDoneSaidAtTheTurnsEnd(t *testing.T) {
{"anthropic", "/v1/messages", messages, "/Users/u/work/magpie"},
} {
t.Run(c.name, func(t *testing.T) {
ends := turnsFollowed(t, time.Nanosecond)
ends, length := turnsFollowed(t, time.Nanosecond)
session := "s-" + c.name
send(c.path, c.body, session, "", true)
noEnd(t, ends, "after a reply that called a tool")
@@ -114,7 +121,7 @@ func TestTurnDoneSaidAtTheTurnsEnd(t *testing.T) {
noEnd(t, ends, "said once")
// a turn shorter than the length set isn't said
turnAfter = func() time.Duration { return time.Hour }
length.Store(int64(time.Hour))
send(c.path, c.body, session, "", false)
noEnd(t, ends, "a short turn")
})
@@ -123,7 +130,7 @@ func TestTurnDoneSaidAtTheTurnsEnd(t *testing.T) {
// Off (TurnDone unset, or no length) follows nothing: nothing is kept.
func TestTurnDoneOff(t *testing.T) {
ends := turnsFollowed(t, 0)
ends, _ := turnsFollowed(t, 0)
var ts turns
r := &Route{turn: true, Agent: "claude", Session: "s"}
ts.begin(r, time.Now())
@@ -137,7 +144,7 @@ func TestTurnDoneOff(t *testing.T) {
// A request still going holds the turn open, whatever another's reply
// was; a session idle past turnStale starts its next turn over.
func TestTurnDoneWaitsForEveryRequest(t *testing.T) {
ends := turnsFollowed(t, time.Minute)
ends, _ := turnsFollowed(t, time.Minute)
var ts turns
t0 := time.Now().Add(-10 * time.Minute)
a, b := &Route{turn: true, Agent: "codex", Session: "s"}, &Route{turn: true, Agent: "codex", Session: "s"}
@@ -216,7 +223,7 @@ func TestTurnDoneCodexBackend(t *testing.T) {
`data: {"type":"response.output_item.done","item":`+item+`}`,
`data: {"type":"response.completed","response":{"id":"r1","status":"completed","usage":{"input_tokens":9,"output_tokens":2}}}`))
})
ends := turnsFollowed(t, time.Nanosecond)
ends, _ := turnsFollowed(t, time.Nanosecond)
s := New()
post := func(calls bool) {
tool.Store(calls)