From 4caae1c8e05155eed2af87dd86390cdf9a524731 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 11 Oct 2026 03:11:00 +0800 Subject: [PATCH] gateway tests: the queued-stream tests count an agent's idle timeout only until the reply's last event, and time the minute from the first request's start TestQueuedStreamKeptAliveForTheMinute/responses failed in a full go test ./... at load ~130: "the agent gave up after 1.496s of a queued request: i/o timeout", with the stream read whole: 22 keepalives, then response.created, the delta and response.completed. The keepalives were fine. What timed out was the wait after the reply's last event: serve's bookkeeping (usage ledger, the route's trace, affinity) runs before the handler returns and net/http closes the stream, and the test's idleConn still counted its 400ms idle timeout there. No agent's idle timeout counts that wait (Codex hangs up on response.completed), the same cause 8412a5d1 fixed in TestTranslatedStreamKeepsClientAlive. The product isn't stalling the keepalive: keepQueued sends one every keepaliveEvery while the request waits, and the failing body shows them all. Timing assumptions in the test and the code it covers: - keepHeldAfter 80ms, watchEvery 10ms, keepaliveEvery 40ms: the first keepalive by ~90ms after keepQueued starts, then every ~40-50ms, against the client's 400ms. ~300ms of room; a stall there is what the test is for, and it stays counted. - the handoff from keepQueued to watch: next keepalive at most max(last+40ms, watch start+80ms) later, in room too. - after the reply's last event, until the close: was counted against 400ms; now closeAfterLast (5s), so a stream that never closes fails instead of hanging. - "answered after 1s" was measured from the end of the first request, so a first request slow to close (the same bookkeeping) moved the bound into the minute. It is now measured from before the first request was sent, against rpmWindow (1.2s): the minute counts from the first's start, which is after that, and rpmSlack is 0, so the bound is exact with no room needed on that side. idleConn now watches what it reads for the reply's last event in each protocol (data: [DONE], response.completed/failed, message_stop, an error event) and waits closeAfterLast after it. This covers every test using askIdle: ForASlot, FailureIsTheStreamsError, WaitedOut and Briefly429 too. Run: - Injected 500ms after the handler returns (a wrapper around the gateway's handler): the old test failed ForTheMinute, Failure/messages and WaitedOut with the same "whole reply, then i/o timeout" body as the full run. With this change all TestQueued* pass with the delay. - keepQueued made a no-op by hand: ForTheMinute (all 3) and ForASlot fail with "gave up after ~400ms ... read \"\"", so the test still guards the keepalive. - go test -race -count=5 -run TestQueued ./internal/gateway; the whole gateway package with -timeout 40m; gofmt -l; go vet, GOOS=linux and GOOS=windows go vet -tags nogui ./internal/gateway. --- internal/gateway/queue_keepalive_test.go | 54 ++++++++++++++++++++---- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/internal/gateway/queue_keepalive_test.go b/internal/gateway/queue_keepalive_test.go index 686fdd733..255e51a14 100644 --- a/internal/gateway/queue_keepalive_test.go +++ b/internal/gateway/queue_keepalive_test.go @@ -2,6 +2,7 @@ package gateway import ( "bufio" + "bytes" "context" "io" "net" @@ -68,19 +69,50 @@ func (v *streamVendor) ServeHTTP(w http.ResponseWriter, r *http.Request) { // idleConn is a connection whose reads time out after idle with nothing // read, as an agent's HTTP client with an idle timeout (WorkBuddy, Trae, -// Qoder): any byte, a comment's too, starts it again. +// Qoder): any byte, a comment's too, starts it again. Once it has read +// the reply's last event (replyEnds) it waits closeAfterLast for the +// stream to close instead: what the gateway does after it (the usage +// ledger, the route's trace, affinity) is no wait an agent's idle timeout +// counts, as TestTranslatedStreamKeepsClientAlive has it. Under load that +// passed 400ms with the whole reply read, the stream not yet closed. type idleConn struct { net.Conn - idle time.Duration + idle time.Duration + tail []byte // the end of what was read, for a marker split across reads + ended bool } -func (c idleConn) Read(b []byte) (int, error) { - c.SetReadDeadline(time.Now().Add(c.idle)) - return c.Conn.Read(b) +// closeAfterLast is how long a stream may stay open after its reply's last +// event before the test fails rather than hangs. +const closeAfterLast = 5 * time.Second + +// replyEnds are the last events of a streamed reply, or of its error, in +// each protocol. +var replyEnds = []string{"data: [DONE]", `"type":"response.completed"`, `"type":"response.failed"`, `"type":"message_stop"`, "event: error", `data: {"error"`} + +func (c *idleConn) Read(b []byte) (int, error) { + wait := c.idle + if c.ended { + wait = closeAfterLast + } + c.SetReadDeadline(time.Now().Add(wait)) + n, err := c.Conn.Read(b) + if n > 0 && !c.ended { + c.tail = append(c.tail, b[:n]...) + for _, e := range replyEnds { + if bytes.Contains(c.tail, []byte(e)) { + c.ended = true + } + } + if len(c.tail) > 64 { + c.tail = c.tail[len(c.tail)-64:] + } + } + return n, err } // idleClient is an HTTP client that gives up once it hears nothing for -// idle, headers or body. +// idle, headers or body, until the reply's last event. func idleClient(idle time.Duration) *http.Client { d := &net.Dialer{} return &http.Client{Transport: &http.Transport{ @@ -90,7 +122,7 @@ func idleClient(idle time.Duration) *http.Client { if err != nil { return nil, err } - return idleConn{c, idle}, nil + return &idleConn{Conn: c, idle: idle}, nil }, }} } @@ -148,6 +180,10 @@ func TestQueuedStreamKeptAliveForTheMinute(t *testing.T) { t.Run(a.name, func(t *testing.T) { shortMinute(t, 1200*time.Millisecond, time.Minute) gw := queueGateway(t, &streamVendor{}, func(p *provider.Provider) { p.MaxRPM = 1 }) + // the minute counts from the first request's start, which is + // after this: the second is answered no sooner than a minute + // after it, however long the first took to close + first := time.Now() if code, body, err := askIdle(gw, a.path, a.body, 400*time.Millisecond); err != nil || code != 200 { t.Fatalf("first: %d %v %s", code, err, body) } @@ -159,8 +195,8 @@ func TestQueuedStreamKeptAliveForTheMinute(t *testing.T) { if code != 200 || !strings.Contains(body, ": keepalive") || !strings.Contains(body, "queued ok") { t.Fatalf("queued = %d %q, want 200, keepalives, then the reply", code, body) } - if d := time.Since(began); d < time.Second { - t.Fatalf("answered after %s, inside the minute", d) + if d := time.Since(first); d < rpmWindow { + t.Fatalf("answered %s after the first request, inside the minute (%s)", d, rpmWindow) } }) }