Files
magpie/internal/gateway/responses_ws_mem_test.go
T
yetone 032340056e gateway: a context kept after its handler returns no longer reaches the request's body (#5250)
TestSocketTurnBodiesFreed went red on ubuntu CI at a3d2a85dd ("turn
bodies [8] of 12 still kept"), after a268aadc4 had let the unrouted
watch's own timers go. Two more paths kept a turn's request, and with
it the whole conversation it was sent with:

- A context timer. Any WithTimeout under the request's context, stopped
  as its call ended, is kept with its context in the timer heap of the
  P that set it until that P tidies its heap, and an idle P never does.
  Through the context's values it reached the watch twice: the
  unroutedKey value held the watch itself, and inflightOf's value held
  the inflight, whose f.unrouted is the watch. The watch holds the
  request (u.r), and the request's Body and GetBody the bytes.
  The unroutedKey value is now the watch's held pointer, as its timers
  already were (unroutedOf), let go as the handler returns; done clears
  f.unrouted too. Its readers (noteRoute, showRedacting, notDescribed)
  run inside the handler, before done.
- An asynchronous release. holdBody and the body room let go of a body
  in a context.AfterFunc, which runs in a goroutine of its own once the
  turn's context ends, so under load the test's one collection could
  come first. The test now collects until every body is gone, within
  10s: far under unroutedShow (15s) and unroutedMost (2m), the times a
  real holder kept one for.

Run (the Linux box, wsl-run, since mote is out of disk):
- origin/main 4ddc150c6, TestSocketTurnBodiesFreed -race -cpu 1,2:
  1 red in 80. With a probe, the red was a body still held after 10s,
  and in 9 of 300 runs bodies were freed only 30-120ms after the first
  collection (the async release).
- With the context-value half only: 3 red in 200, each held over 10s.
  With holdBody's sharing turned off: still 1 red in 200, so it isn't
  a decoded string sharing the body. A goroutine dump at the red had 7
  goroutines, none of the gateway's: a heap reference, not a runner.
- With both halves and the new test: 300 of 300 green (-race,
  -count=150, -cpu 1,2).
- TestUnroutedContextLetsGoOfRequest, new, holds the request's context
  past its handler: red 5 of 5 on the old code, red 5 of 5 with only
  the context-value half, red 3 of 3 with only the f.unrouted half,
  green with both. TestUnroutedTimersLetGoOfRequest still passes.
- go vet ./internal/gateway on Linux: ok. gofmt -l: clean.
- The whole internal/gateway package runs next on the Linux box; its
  result goes on #5250.

Not tried: macOS and Windows runs (mote is out of disk and refuses
ssh); other context values that might hold a request are not audited.
2026-11-11 14:24:25 +08:00

112 lines
3.7 KiB
Go

package gateway
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"runtime"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"weak"
"github.com/yetone/magpie/internal/provider"
)
// A socket turn's body is freed once the turn is over (casyubaji on
// Discord: ~2.8 GB after 6 hours of Codex). Each turn is served from a
// body built in memory, the whole conversation, and the trace keeps its
// last 60 routes: a route that kept the turn's request context (its tool
// scan, for the turn's end) kept the request on it (the inflight's
// unrouted watch), and with it the body, so 60 of Codex's 20 MB
// conversations stayed after every socket had closed.
func TestSocketTurnBodiesFreed(t *testing.T) {
fresh(t)
var served atomic.Int64
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
n := served.Add(1)
w.Header().Set("Content-Type", "text/event-stream")
id := fmt.Sprintf("resp_up%d", n)
item := fmt.Sprintf(`{"type":"message","id":"msg_%d","role":"assistant","status":"completed","content":[{"type":"output_text","text":"answer %d"}]}`, n, n)
io.WriteString(w, sse(
`data: {"type":"response.created","response":{"id":"`+id+`","object":"response","status":"in_progress","output":[]}}`,
`data: {"type":"response.output_item.done","output_index":0,"item":`+item+`}`,
`data: {"type":"response.completed","response":{"id":"`+id+`","object":"response","status":"completed","output":[`+item+`],"usage":{"input_tokens":300,"output_tokens":50,"total_tokens":350}}}`))
}))
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Responses: up.URL + "/v1", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
var mu sync.Mutex
var bodies []weak.Pointer[byte]
socketTurnBody = func(body []byte) {
mu.Lock()
bodies = append(bodies, weak.Make(&body[0]))
mu.Unlock()
}
t.Cleanup(func() { socketTurnBody = nil })
s, ts := sockServer(t)
c, _, err := dialSock(t, ts, "/v1/responses", nil)
if err != nil {
t.Fatal(err)
}
big := `{"type":"message","role":"user","content":[{"type":"input_text","text":"` + strings.Repeat("x", 256<<10) + `"}]}`
const turns = 12
id := ""
for i := range turns {
in := userMore
if i == 0 {
in = big
}
got := c.turn(create(id, in))
end := last(got)
if end.Get("type").String() != "response.completed" {
t.Fatalf("turn %d: %v", i, got)
}
id = end.Get("response.id").String()
}
c.c.CloseNow()
// the socket's handler ends after its client goes (drain waits for
// it); nothing of a turn is kept after, but the trace's routes. A
// held body is let go by a context.AfterFunc (holdBody, body room),
// which runs in a goroutine of its own once the turn's context ends,
// so under load a collection can come before it: collect until each
// is gone, within a bar far under what a real holder keeps one for
// (#5250: a stopped timer kept the last turn's request for up to
// unroutedMost, 2m; unroutedShow is 15s).
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
s.sockets.drain(ctx)
if ctx.Err() != nil {
t.Fatal("the socket's handler did not end after its client went")
}
mu.Lock()
defer mu.Unlock()
if len(bodies) != turns {
t.Fatalf("%d turn bodies for %d turns", len(bodies), turns)
}
var kept []int
for bar := time.Now().Add(10 * time.Second); ; {
runtime.GC()
kept = kept[:0]
for i, b := range bodies {
if b.Value() != nil {
kept = append(kept, i)
}
}
if len(kept) == 0 || time.Now().After(bar) {
break
}
time.Sleep(20 * time.Millisecond)
}
if len(kept) > 0 {
t.Fatalf("turn bodies %v of %d still kept after the socket's handler ended", kept, turns)
}
}