Skip to content

fix(kotlin): resume a text session's socket, and notice when it's gone - #2810

Open
aleksandar-apostolov wants to merge 9 commits into
mainfrom
fix/kotlin-resume-socket
Open

aleksandar-apostolov wants to merge 9 commits into
mainfrom
fix/kotlin-resume-socket

Conversation

@aleksandar-apostolov

@aleksandar-apostolov aleksandar-apostolov commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Why

Refs AND-2082

When a text session's socket drops (network, background, a router deploy), the conversation stops updating and the app has to attach again. A socket whose network went away without a close looks open until TCP gives up. The router can resume a socket (#1211) and send a healthcheck (#2811); this ports Swift #2802 to Kotlin.

Changes

  • Resume: a text session's socket opens with resumable=true and keeps the last cursor. On any close but 1000 or 1005, or a lost connection, it reopens with the cursor and the request_id of the last question asked through responses. It stops when the socket opens, on a 4xx (a 401 is retried once with a fresh token, a 429 is retried), or on close or detach. A call's socket is not resumable and drops as before.
  • Backoff: first retry at once, then 1 s doubling to 25 s, each wait at a random point in its second half (jitter), from zero on every reconnect. No time limit.
  • Healthcheck: the socket asks for healthcheck=true. Once the first healthcheck has come, three intervals with nothing read count as a drop. Nothing is timed before the first, so a router without healthchecks is never taken for dead. Healthchecks do not reach events() and do not move the cursor.
  • Network: StreamAgents takes an optional context. With it, the network going lets the socket go and no attempt is made until it is back, which then reconnects at once. Only a change counts.
  • Background: with a context, a text socket is kept for 10 s after the app goes to the background, then let go until the app is back. A session on a call keeps its socket. Adds lifecycle-process (Chat's version) and declares ACCESS_NETWORK_STATE.
  • While reconnecting: isConnected is false and failure stays null; events() readers, approvals and tools carry on. A tool result that could not be sent waits for the next socket.
  • resumed with complete: false: waiting approvals are dropped, the reply in flight is finished when the receipt says it is settled or there is none, and an interrupted question is asked once more under a new request id, within 5 minutes.
  • Juno passes its application context. README, KDoc and the sdk-kotlin skill describe all of it. One CHANGELOG line: failure no longer becomes SocketClosed on a text session's drop.

Validation

./gradlew :core:testDebugUnitTest :core:lintDebug: 367 tests pass (10 live-router tests skipped). Juno: :app:testDebugUnitTest (27) and :app:compileDebugKotlin pass; the demo app compiles. sending after the router ended the session says the socket closed… is flaky on main too and passes on rerun.

Resume: 13 tests against TestRouter. Healthcheck: 3 tests (no timeout before the first, not emitted to events(), the timeout kept per opening). Network and background: the decisions are in a plain class with 12 JVM tests; the Android wiring is compiled and exercised in Juno. Each test failed under a mutation of the code it covers.

@aleksandar-apostolov
aleksandar-apostolov marked this pull request as ready for review December 1, 2026 09:09
@getstream-metis

Copy link
Copy Markdown

👋 Hi Aleksandar, want a second pair of eyes on this?

I'm Jeffery, Stream's review agent. I look for bugs, regressions, missing tests and slow paths, and I only post findings I've checked against the code.

  • Review this PR

Or comment /jeffery review. I'll stay quiet unless you ask.

A connection that went away without a close reads as open until TCP gives
up. A text session's socket now asks for the router's healthchecks and,
once one has come, takes three of their intervals with nothing read as
a lost connection: it is closed with 1001 and its flow ends with
SocketClosed, so the existing resume path opens it again.

Nothing is timed before a socket's first healthcheck, so a router that
sends none is never taken for dead, and the timeout is kept per opening.
Healthchecks never reach events() or Conversation, and move no cursor.
A call's socket is unchanged.
StreamAgents takes an optional Context. With one, a text session watches
the default network (registerDefaultNetworkCallback) from before its
socket opens: the network going lets the socket go as a drop, no attempt
to reopen it is made until the network is back, and it coming back cuts
the wait short. Only a change counts: a first reading of no network is
not believed. The callback is unregistered when the session is closed,
detached or stops. Without a Context nothing changes.

The decisions live in Recovery, a pure class tested on the JVM; DeviceWatch
is the Android adapter. core's manifest asks for ACCESS_NETWORK_STATE,
which it now calls itself. Juno passes its application.
With a Context, a text session also observes ProcessLifecycleOwner, on
the main thread. Once the app is in the background its socket is kept
for 10 seconds, so a reply in flight or a tool running can finish, then
let go as a drop; no attempt to reopen it is made until the app is back,
which wakes the wait. A socket that opens in the background with the
grace period over is let go at once. A session moved onto a call keeps
its socket.

Stream Chat lets its socket go at ProcessLifecycleOwner's stop, 700 ms
after the last activity stops; the extra time is for a reply in flight,
kept short of the point a cached app is frozen. core depends on
lifecycle-process 2.8.7, which Stream Chat already brings at runtime.
@aleksandar-apostolov aleksandar-apostolov changed the title fix(kotlin): resume a text session's socket after it drops fix(kotlin): resume a text session's socket, and notice when it's gone Dec 1, 2026

This branch has not been deployed

No deployments
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.

1 participant