Repository navigation
fix(kotlin): resume a text session's socket, and notice when it's gone - #2810
Open
aleksandar-apostolov wants to merge 9 commits into
Open
aleksandar-apostolov wants to merge 9 commits into
aleksandar-apostolov wants to merge 9 commits into
Conversation
aleksandar-apostolov
marked this pull request as ready for review
December 1, 2026 09:09
👋 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.
Or comment |
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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
resumable=trueand keeps the lastcursor. On any close but 1000 or 1005, or a lost connection, it reopens with the cursor and therequest_idof the last question asked throughresponses. 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.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 reachevents()and do not move the cursor.StreamAgentstakes an optionalcontext. 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.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. Addslifecycle-process(Chat's version) and declaresACCESS_NETWORK_STATE.isConnectedis false andfailurestays null;events()readers, approvals and tools carry on. A tool result that could not be sent waits for the next socket.resumedwithcomplete: false: waiting approvals are dropped, the reply in flight is finished when the receipt says it is settled or there is none, and aninterruptedquestion is asked once more under a new request id, within 5 minutes.sdk-kotlinskill describe all of it. One CHANGELOG line:failureno longer becomesSocketClosedon 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:compileDebugKotlinpass; the demo app compiles.sending after the router ended the session says the socket closed…is flaky onmaintoo and passes on rerun.Resume: 13 tests against
TestRouter. Healthcheck: 3 tests (no timeout before the first, not emitted toevents(), 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.