Skip to content

Send a healthcheck a browser can read on the session events socket - #2811

Merged
oliverlaz merged 3 commits into
mainfrom
oliver/router-session-heartbeat
Dec 1, 2026
Merged

oliverlaz merged 3 commits into
mainfrom
oliver/router-session-heartbeat

Conversation

@oliverlaz

@oliverlaz oliverlaz commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Why

A browser answers the router's WebSocket control pings itself and never shows them to the page. A page whose network went away without a close (a laptop sleeping, a change of wifi) keeps a socket that looks open until TCP gives up, which can take minutes. The router already pings every 30 s, but only the router can see those.

What

  • Opt-in healthcheck. healthcheck=true on /v1/agents/sessions/{id}/events sends {"type": "healthcheck", "interval_ms": 10000} every interval from when the socket opens. It is named healthcheck to match the rest of the product.
    • The node holding the socket writes it, whether it runs the session or relays it.
    • It has no cursor, is not kept in the 256-frame replay buffer, and is never replayed on resume.
    • Client rule: start no timer at the open. Once the first healthcheck arrives, a client that reads no frame for 2–3 intervals treats the socket as dropped. A router without this change ignores the parameter and sends none, and a timer started at the open would drop a healthy, quiet socket every 20–30 s.
  • Relayed sockets no longer hang when the session's node dies. The relaying node already sends the node running the session a refresh every 30 s. Now that node answers with a new refreshed bus message. If no answer comes for 90 s, the socket is closed with 1011, so the close lands within about 2 minutes of the node going away. Before, the socket stayed open and silent forever, and a healthcheck would have made that look healthy.
    • This applies to every relayed socket, with or without a healthcheck or resume. Go text sessions already reconnect on it, as they do on any close but 1000. JavaScript, Dart, Swift and Kotlin don't resume yet, so for them, and for a Go call, a 1011 ends the session with an error rather than leaving it hanging.
    • Rolling-deploy safe: the relaying node marks each refresh as wanting an answer (answer_refresh), and the session's node says on attach that it answers (answers_refresh). An older node is never timed out, and never sent an answer it would forward to its client as an empty frame.
  • Swift and Kotlin name the new frame (AgentEvent.Kind.healthcheck / Healthcheck). Their wire tests read every frame type sessionws.go writes and would fail otherwise. Neither SDK asks for healthchecks yet.
  • The spec is regenerated, along with the JS types, the Python plugin and the Ruby spec. Swift's --check passes, and Go, Kotlin, .NET, PHP and Rust had no diff. The Dart image could not be pulled, but Dart's generator skips watchSession.
  • The SDK note .claude/skills/sdk/notes/2026-11-30-session-healthcheck.md covers what each SDK should do. The JS reconnect loop must treat both the healthcheck timeout and 1011 as a drop.

Tests

  • New SessionHealthcheckSuite, covering the local and relayed node, no healthcheck unless asked, no cursor, no replay on resume, and the 10 s default.
  • New RelayedSessionSuite tests:
    • a node that went quiet gets the socket closed with 1011;
    • a live node keeps it open;
    • an older node that never answers is not taken for gone;
    • only a refresh that asks for an answer gets one;
    • the 30 s and 90 s defaults the spec promises.
  • Seven mutants each failed a test.
  • The relay, healthcheck and socket-end suites pass under -race, and the full internal/api integration package passes. TestPoliciesSuite failed once because the run crossed midnight, and passed when rerun.
  • Swift WireTests pass on an iOS simulator, and Kotlin WireTest passes. Both include the check that every frame the router writes is named.

Not covered: the dispatch socket (/v1/dispatch) still has no healthcheck.

A browser answers the router's WebSocket pings without telling the page, so a page whose
network went away without a close cannot tell its socket is dead until TCP gives up.

- `heartbeat=true` on /v1/agents/sessions/{id}/events sends
  {"type": "heartbeat", "interval_ms": 10000} every interval, written by the node holding the
  socket. It carries no cursor and is never replayed on resume.
- A relayed socket now closes with 1011 when the node running its session has not answered a
  refresh for 90 s (it crashed or lost Redis), instead of staying open and silent. The edge
  asks for an answer (answer_refresh) and the holder says it gives one (answers_refresh), so
  mixed-version nodes keep today's behavior during a rolling deploy.
- Spec and generated clients regenerated; SDK note in .claude/skills/sdk/notes/.
@getstream-metis

getstream-metis Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

👋 Thanks, Oliver! I'm on it.

How to work with me

  • 💬 Disagree with a finding? Reply in its thread and tell me why. I'll withdraw it, or answer with evidence.
  • 🔁 Fixed it? Just push. The next round checks your fix and resolves the thread.
  • 👍 👎 Rate my findings. I learn what your team doesn't want flagged.
  • ✅ I approve once nothing blocking is left: must-fix always blocks, should-fix only in the first two rounds. CI isn't part of that; branch protection still holds the merge until it's green. On repos that require a human approval, a teammate still has to approve too.
Commands

Comment any of these on the PR:

  • /jeffery review · start a review
  • /jeffery next-round · start the next round now
  • /jeffery pause · /jeffery resume · stop or restart reviews on this PR
  • /jeffery status · where things stand
  • /jeffery help · all commands

@getstream-metis getstream-metis Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Round 1 · 🆕 1 new · 1 should-fix to go

Finding Where
🟡 Should Fix The note says Go has no resume path, but Go resumes text sessions on a 1011 close 2026-11-30-session-heartbeat.md:17

➡️ Round 2 started on 3a09c52.

…Kotlin

- The query parameter and frame are now `healthcheck`, as the rest of the product spells it.
- Swift and Kotlin name the frame in AgentEvent.Kind: their wire tests read every frame type
  sessionws.go writes, and failed on the new one.
- A client starts its timer only once the first healthcheck arrives. A router without this
  change ignores the parameter, and a timer started at the open would drop a healthy, quiet
  socket every 20-30 s. The spec and the SDK note say so.
@oliverlaz oliverlaz changed the title Send a heartbeat a browser can read on the session events socket Send a healthcheck a browser can read on the session events socket Dec 1, 2026
Go dials text sessions with resumable=true and reconnects on any close but 1000, so it already
treats the new 1011 as a drop. Its calls, and the JavaScript, Dart, Swift and Kotlin sessions,
do not resume yet.

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Round 3 · ✅ 1 fixed · 🎉 Approved

Nice work, Oliver! Nothing blocking is left on 3d7f4619a6e6. Ship it 🚀

martinmitrevski added a commit that referenced this pull request Dec 1, 2026
URLSession never reads a connection that went away without a close as
gone: receive() waits on, and the request timeout does not apply once the
socket is open. A text session now:

- asks for the router's heartbeats (#2811) and, once one has come, takes
  three intervals with nothing read as a lost connection. Nothing is timed
  before the first, so a router without heartbeats is not taken for dead;
- lets its socket go when the network goes, and makes no attempt to reopen
  it until the network is back;
- keeps its socket for one background task, then lets it go, and makes no
  attempt until the app is back. A session moved onto a call keeps it. The
  background task ends on every way out.

A socket let go here ends its stream with socketClosed itself, for the
opening it was decided for, so it reads as a drop and is resumed.
AgentEvent.Kind names heartbeat, which WireTests needs once the router
writes it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
martinmitrevski added a commit that referenced this pull request Dec 1, 2026
URLSession never reads a connection that went away without a close as
gone: receive() waits on, and the request timeout does not apply once the
socket is open. A text session now:

- asks for the router's healthchecks (#2811) and, once one has come,
  takes three intervals with nothing read as a lost connection. Nothing
  is timed before a socket's first, so a router without them is not
  taken for dead;
- lets its socket go when the network goes, and makes no attempt to
  reopen it until the network is back. Only a change counts: a first
  reading of no network is not believed;
- keeps its socket for one background task, then lets it go, and makes
  no attempt until the app is back. A socket that opens in the background
  with no time left is let go at once, and a session moved onto a call
  keeps its socket. The background task ends on every way out.

A socket let go here ends its stream with socketClosed itself, for the
opening it was decided for, so it reads as a drop and is resumed.

Reviewed by a fresh agent: a first reading of no network stopped every
reconnect, the app going to the background while a socket opened was
missed, and two test checks could not fail. All fixed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
martinmitrevski added a commit that referenced this pull request Dec 1, 2026
URLSession never reads a connection that went away without a close as
gone: receive() waits on, and the request timeout does not apply once the
socket is open. A text session now:

- asks for the router's healthchecks (#2811) and, once one has come,
  takes three intervals with nothing read as a lost connection. Nothing
  is timed before a socket's first, so a router without them is not
  taken for dead;
- lets its socket go when the network goes, and makes no attempt to
  reopen it until the network is back. Only a change counts: a first
  reading of no network is not believed;
- keeps its socket for one background task, then lets it go, and makes
  no attempt until the app is back. A socket that opens in the background
  with no time left is let go at once, and a session moved onto a call
  keeps its socket. The background task ends on every way out.

A socket let go here ends its stream with socketClosed itself, for the
opening it was decided for, so it reads as a drop and is resumed.

Reviewed by a fresh agent: a first reading of no network stopped every
reconnect, the app going to the background while a socket opened was
missed, and two test checks could not fail. All fixed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oliverlaz
oliverlaz merged commit 341e001 into main Dec 1, 2026
27 of 28 checks passed
@oliverlaz
oliverlaz deleted the oliver/router-session-heartbeat branch December 1, 2026 11:31
martinmitrevski added a commit that referenced this pull request Dec 1, 2026
URLSession never reads a connection that went away without a close as
gone: receive() waits on, and the request timeout does not apply once the
socket is open. A text session now:

- asks for the router's healthchecks (#2811) and, once one has come,
  takes three intervals with nothing read as a lost connection. Nothing
  is timed before a socket's first, so a router without them is not
  taken for dead;
- lets its socket go when the network goes, and makes no attempt to
  reopen it until the network is back. Only a change counts: a first
  reading of no network is not believed;
- keeps its socket for one background task, then lets it go, and makes
  no attempt until the app is back. A socket that opens in the background
  with no time left is let go at once, and a session moved onto a call
  keeps its socket. The background task ends on every way out.

A socket let go here ends its stream with socketClosed itself, for the
opening it was decided for, so it reads as a drop and is resumed.

Reviewed by a fresh agent: a first reading of no network stopped every
reconnect, the app going to the background while a socket opened was
missed, and two test checks could not fail. All fixed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
martinmitrevski added a commit that referenced this pull request Dec 1, 2026
URLSession never reads a connection that went away without a close as
gone: receive() waits on, and the request timeout does not apply once the
socket is open. A text session now:

- asks for the router's healthchecks (#2811) and, once one has come,
  takes three intervals with nothing read as a lost connection. Nothing
  is timed before a socket's first, so a router without them is not
  taken for dead;
- lets its socket go when the network goes, and makes no attempt to
  reopen it until the network is back. Only a change counts: a first
  reading of no network is not believed;
- keeps its socket for one background task, then lets it go, and makes
  no attempt until the app is back. A socket that opens in the background
  with no time left is let go at once, and a session moved onto a call
  keeps its socket. The background task ends on every way out.

A socket let go here ends its stream with socketClosed itself, for the
opening it was decided for, so it reads as a drop and is resumed.

Reviewed by a fresh agent: a first reading of no network stopped every
reconnect, the app going to the background while a socket opened was
missed, and two test checks could not fail. All fixed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
martinmitrevski added a commit that referenced this pull request Dec 1, 2026
URLSession never reads a connection that went away without a close as
gone: receive() waits on, and the request timeout does not apply once the
socket is open. A text session now:

- asks for the router's healthchecks (#2811) and, once one has come,
  takes three intervals with nothing read as a lost connection. Nothing
  is timed before a socket's first, so a router without them is not
  taken for dead;
- lets its socket go when the network goes, and makes no attempt to
  reopen it until the network is back. Only a change counts: a first
  reading of no network is not believed;
- keeps its socket for one background task, then lets it go, and makes
  no attempt until the app is back. A socket that opens in the background
  with no time left is let go at once, and a session moved onto a call
  keeps its socket. The background task ends on every way out.

A socket let go here ends its stream with socketClosed itself, for the
opening it was decided for, so it reads as a drop and is resumed.

Reviewed by a fresh agent: a first reading of no network stopped every
reconnect, the app going to the background while a socket opened was
missed, and two test checks could not fail. All fixed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants