Repository navigation
[AI-3616] A relayed socket cut by its holder says the relay ended - #2419
Conversation
…y ended When the node running a session gave back the proxy watcher of a socket on another node (expireProxies) while the session went on, that socket logged close_reason=session_ended and closed with 1000 "the session ended", so every SDK took the conversation for over. The holder now marks relay.Closed as detached when its session had not ended, and the socket's node logs close_reason=relay_ended and closes with 1011 "the relay to the session ended", which SDKs read as a dropped socket. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kanat
left a comment
There was a problem hiding this comment.
VERDICT: GO
Security round 1 on head 016e5f0. No findings.
- Only the node holding the session sets
Detached; client frames travel as a different message type on the command topic and never reach the field. A probe sending such frames from a client left the socket open and the session's own end still closed it assession_ended. - A running session reads
Detached, and an ended one does not, becauseEndedis set before the watchers close. Halted and close-command ends keep their old close. Detachedis read only through the existing watcher and owner lookup, so it adds no new state across apps or users. A probe showed one expired watcher closes only its own socket and leaves a sibling socket of the same session open.- The close text and the new log cause are constants. The diff has no
err.Error()or error-formatting hit. - Removing the store of
Detachedindeliverfails the new test.
Unverified: access control on the relay bus in a deployment. A node with bus access can already close any watcher's socket, so Detached adds nothing there.
kanat
left a comment
There was a problem hiding this comment.
VERDICT: NO-GO
Round 1 at 016e5f0ae, merged onto main at cd3f7371d (merges clean, no conflict). CI: draft, so go and integration are skipped on this head. They run at gh pr ready.
The verdict rests on R1.1 (Should fix): the README row and the PR body say a caller reconnects. Only one client does that. The code change itself is sound.
[Nit] R1.4 acceleration/internal/api/handwritten.go:249-250 (not in the diff): the events socket's spec description names the 1000 and 1012 closes. It does not name the new 1011 "the relay to the session ended", and a relayed socket can now get that close whether it is resumable or not. Client authors read the spec. Ticket: add one sentence and run go run ./cmd/openapi.
Checked and sound:
- Ordering:
session.gosetsEndedin one place only (close(), :1982), and it does so unders.mubefore the watchers close (:1992). Every end goes throughclose():Close,haltandabandon. A watcher channel closes in only three places. :619 is a watch on a session that has already ended, soStatereadsEnded. :1992 is the end. :704 is the detach, which is the case this PR flags. The reverse race (a detach, then the end, then therunning()read) reportssession_endedfor a session that did end. A node drain halts sessions throughclose(), so it readsHaltedand notDetached, and closes1012as before. - Mixed builds:
relay.Responsehas one decoder,deliver, which uses plainjson.Unmarshal, so an old socket node ignoresdetached. An old holder omits it, soDetachedis false and the socket behaves as on base. - Reconnect: Go reconnects with
resumable,cursorandrequest_id. The dispatch path asks again only for aninterruptedcommand (sdks/go/agents/dispatch.go:454), and only a router stopping produces that, not this path. A replayed tool call is re-sent only while it still waits (session.go:634-640). An expiry needs 90 s of missed refreshes, and failed reconnects back off, so neither a tight loop nor a duplicate turn follows. 1011is not used on the events socket elsewhere, so it does not collide with1012(shutdown) or1000.- Generated files are in step (
generated.sh). gofmt and vet are clean. Unit tests for./internal/apiand./internal/relaypass. Integration suitesSocketCloseLogSuite,RelayedSessionSuite,CrossShardSessionSuite,ForwardedSessionSuiteandNodeRetrySuitepass on the merged tree, and the two new tests pass at-count=5. - Mutants: 13 run, 11 killed. They are 6 of the author's 14, rerun, plus 7 new ones: Detached derived from
!byCommand()or!halted(), the session state read once at attach, no close frame, falling through to the session-end branch, the tick skippingdropExpired, and the store moved after the close. The tick (R1.2) and the store ordering (R1.3) survived. Both are Nits.
| | `client_gone` | The connection broke with no close frame, or a write to the caller failed | | ||
| | `timeout` | The caller answered no ping in time, or sent no start frame in time on `/v1/agents/socket` and `/v1/{modality}/stream` | | ||
| | `session_ended` | The session ended for another reason. `end_reason` says which. Session sockets only | | ||
| | `relay_ended` | A socket relayed from another node lost the watcher the node running the session held for it, such as one given back for a socket it had not heard from in time, while the session went on (`1011`). The caller reconnects to go on watching. Events socket only | |
There was a problem hiding this comment.
[Should fix] R1.1 "The caller reconnects to go on watching" holds only for Go, and there only for a text session's resumable socket (sdks/go/stream/accelerated.go:377 resumable := session.CallId == "", :948). JS (socket.ts:285 → session.ts:905 end), Kotlin (SessionSocket.kt:30-33 "No automatic reconnection, on purpose"; AgentSession.kt:502-507 ends with a failure) and Dart (socket.dart:16-17, :120-123) report a failure and stop. Swift (SessionSocket.swift:116-119) reports socketClosed for any close, 1000 included (.claude/skills/sdk/SKILL.md:758). The PR body says the same thing twice: in the table ("Every SDK reads anything but 1000 as a dropped socket, so a caller reconnects") and in "Who reads this" ("Each treats 1000 as the end").
The behaviour is right: the session still runs. Fix the wording, for example: "A resumable socket's caller reconnects (the Go SDK, for a text session); other clients see the socket fail, not the session end." Correct the body to match.
| "watcher", watcher) | ||
| state.dropProxy(watcher) | ||
| } | ||
| s.dropExpired(state) |
There was a problem hiding this comment.
[Nit] R1.2 No test reaches this tick. A mutant that deletes the dropExpired call survives. The wiring is moved code and had no test on base either, and pingEvery is a 30 s const. Ticket only.
| socket.byCommand.Store(true) | ||
| } | ||
| if response.Detached { | ||
| socket.detached.Store(true) |
There was a problem hiding this comment.
[Nit] R1.3 No test pins "set before closed". Moving this store after socket.close() survived 10 of 10 runs. It is the same pattern as halted and byCommand above, and no deterministic test can see the gap between two adjacent statements. Ticket only, or accept.
… socket spec names the 1011 relay close Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kanat
left a comment
There was a problem hiding this comment.
VERDICT: GO
Delta round 2 at 014a887e1 (was 016e5f0ae), merged onto main at dca845c2e. The delta changes the README row, one spec sentence, the PR body and the generated clients. No Go, SDK or test code changed.
Prior findings:
- R1.1 (Should fix, the
relay_endedrow and the PR body): fixed. Each SDK checked against its code: Go reconnects only a resumable socket (sdks/go/stream/accelerated.go:377,:948;droppedat:1031). JSsocket.ts:285, KotlinSessionSocket.kt:30-34, Dartsocket.dart:119-120, SwiftSessionSocket.swift:117-118. The PR table row, "Who reads this" and the first Why paragraph now match. - R1.4 (Nit, spec text): fixed.
handwritten.gonames the 1011 relay close. Mutation:openapi.yamlreset to016e5f0ae, andhandwritten.goreset to016e5f0ae, each makesTestPostureSuite/TestTheCommittedSpecIsRenderedFromTheOperationsfail; the head passes. - R1.2, R1.3 (Nits): left for tickets by decision. Not re-checked in this delta.
New findings:
- Nit, not blocking (changed hunk): the
relay_endedrow says the other clients "see the socket fail, not the session end". That holds for the signal the router sends. Kotlin still ends the local conversation on any socket failure (AgentSession.kt:194-195,:502-507setsConversation.State.Ended), and JS does the same (sdks/js/src/session.ts:909-912). Wording option: "the other clients see their socket fail with that code; the session still runs."
Checks:
go run ./cmd/openapi,uv run plugins/stream/generate.py,uv run sdks/swift/generate.py --check, andnpm run typesafternpm ci: no diff against the head. The tree has nodashboard/.go test ./internal/api ./cmd/openapi(unit) passes. IntegrationTestPostureSuite(including the freshness test) andTestSocketCloseLogSuitepass, on the head merged ontodca845c2e.- CI: the PR is a draft;
gh pr checksshows Analyze, CodeQL, labeler and GitGuardian passing, the rest skipped. - Code behaviour: the only Go change is a string in the events socket's description.
| | `client_gone` | The connection broke with no close frame, or a write to the caller failed | | ||
| | `timeout` | The caller answered no ping in time, or sent no start frame in time on `/v1/agents/socket` and `/v1/{modality}/stream` | | ||
| | `session_ended` | The session ended for another reason. `end_reason` says which. Session sockets only | | ||
| | `relay_ended` | A socket relayed from another node lost the watcher the node running the session held for it, such as one given back for a socket it had not heard from in time, while the session went on (`1011`). A resumable socket's caller reconnects to go on watching (the Go SDK does, for a text session); the other clients see the socket fail, not the session end. Events socket only | |
There was a problem hiding this comment.
Nit, not blocking (wording). "The other clients see the socket fail, not the session end" holds for the signal the router sends. Kotlin still ends the local conversation on any socket failure (AgentSession.kt:502-507 sets Conversation.State.Ended) and JS does too (sdks/js/src/session.ts:909-912). Option: "the other clients see their socket fail with that code; the session still runs."
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Why
A relayed events socket is one that lands on a node other than the one running its session. The node running the session holds a proxy watcher for it, and gives that watcher back when the socket's node stops refreshing it (
expireProxies,acceleration/internal/api/relayws.go). On base, the socket's node then logsclose_reason=session_endedand closes the socket with1000"the session ended", while the session still runs. The round-1 review of #2218 found it (R1.6).The close frame matters more than the log line. The Go SDK reads
1000as the end of the conversation and does not reconnect (dropped,sdks/go/stream/accelerated.go:1028-1032), and Kotlin marks the conversationEnded(AgentSessionTest.kt:1140-1159:1000isConversation.State.Ended,1011aSocketClosedfailure).How it works
A session sets
Endedbefore it closes its watchers (acceleration/internal/session/session.go:1982sets it,:1992closes them), so a watcher whose events closed while the session is notEndedwas detached, not ended. The holder asks the session itself rather than flagging the expiry path, so any detach that leaves the session running reads the same way.Close code and value, chosen as #2218 and #2271 chose theirs:
relay_ended(new)10111011is RFC 6455's "unexpected condition" (section 7.4.1): the holder lost a socket that was still open.1000told every caller the conversation was over;1011is a failure. Only the Go SDK reconnects, and only a text session's resumable socket (accelerated.go:377,:948)1012was the other candidate. It says "router restarting", which this is not, and on a session socket it already meansshutdown.Before and after (fake ids):
During a rolling deploy a holder on the old build sends no
detached, so its sockets log and close as on base. A socket node on the old build ignores the field.Changes
relay.ResponsegetsDetached, set onClosedbypublishSessionEventswhen the session had not ended.writeRelayedEventscloses a detached socket with1011and logsrelay_ended, with noend_reason.expireProxies's tick moves intodropExpired, which the test calls.acceleration/README.mdlistsrelay_endedwith the otherclose_reasonvalues; the events socket's spec text names the1011close (regenerated:openapi.yaml, the JS and Python clients).Who reads this
close_reason; it is in the access log only. The close code is read by the session socket of Go (sdks/go/stream/accelerated.go:1031), JS (socket.ts:285), Dart (socket.dart:120), Kotlin (SessionSocket.kt) and Swift (SessionSocket.swift:118). Go reconnects a text session's resumable socket on1011. JS, Kotlin and Dart treat1000as the end and any other code as a failure, and stop. Swift reportssocketClosedfor every close,1000included. No SDK changes here.close_reasonisLowCardinality(String)andclose_codeNullable(UInt16)inagent_requestsandagent_access_logs(GetStream/chatinfra/charts/clickhouse/templates/init-schema-job.yaml, master6c2420c92e), read askv['close_reason'], with no enum of values.dashboard/,tui/andplugins/streamdo not read the value.Validation
SocketCloseLogSuite)cd3f7371dTestARelayedSocketWhoseWatcherExpiredIsTheRelaysEnd: an expired proxy logsrelay_ended 1011, closes1011"the relay to the session ended", and a new socket still watches the session1000"the session ended",session_endedTestARelayedSocketWhoseSessionEndedIsTheSessionsEnd(control): a stopped session logssession_ended 1000and closes1000"the session ended"The base run used the same test file with
dropExpiredinlined asexpired()+dropProxy. Every added guard and constant was mutated (14 mutants, one script), and each failed at least one of the two tests. The one line no test covers:expireProxiescallingdropExpiredon its 30 s tick (the test callsdropExpireditself).🤖 Generated with Claude Code