Stop reporting the app being switched away from as a failure
Backgrounding the app left "Lost the event stream (SocketTimeoutException: null)" waiting at the top on return. Android stops the activity, the socket dies with it, and the reconnect loop -- which kept running on a phone nobody was looking at -- recorded the failure. Switching apps is a choice somebody made, not a fault to report. Worse, it could not clear. `streamError` was reset when an event arrived, so a session that reconnected and then sat idle displayed a connection error it had already recovered from, indefinitely. That is the expensive half: a stale failure is indistinguishable from a live one. So the stream now runs only while the screen is at least STARTED, which makes the drop a deliberate close rather than an error (EventStream already distinguishes them), and resuming reconnects from the same cursor. What takes a failure off the screen is `onOpen` -- the measured moment the server accepted the connection -- rather than the first event to follow it. The message that does get shown leads with what will happen next rather than with the exception's class name, which named nothing the reader could act on. lifecycle-runtime-compose is declared rather than inherited from activity-compose, for the reason core-ktx already is: this code calls repeatOnLifecycle and LocalLifecycleOwner directly now, and a transitive could change under it. 2.11.0, the current stable. Verified on the emulator against an idle session, which is the case the old code could never clear: backgrounded 35s, returned, no banner -- and a message sent afterwards arrived live, so the reconnect genuinely reattached rather than merely staying quiet. Build, lint and ktfmt clean. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d7c692a4ec
commit
aff2cb90e2
4 files changed
+71
-31
No files matched your search
@@ -31,12 +31,17 @@ class EventStream(private val settings: ServerSettings, private val sessionId: S
|
||||
/**
|
||||
* Streams events after [after] into [onEvent] until the stream drops.
|
||||
*
|
||||
* [onOpen] fires once the server has accepted the connection. That is the measured moment the
|
||||
* stream is live again, and the only honest thing to clear a previous failure on: an earlier
|
||||
* version cleared on the first event instead, so an idle session went on displaying a
|
||||
* connection error that had already been recovered from, indefinitely.
|
||||
*
|
||||
* [onReset] fires when the server answers that the cursor is too far behind to continue from:
|
||||
* everything already displayed is stale and the events that follow are a fresh window, so the
|
||||
* caller drops what it holds and rebuilds -- the same thing it does when the screen opens. It
|
||||
* arrives before those events, so a caller that clears on it stays in order.
|
||||
*/
|
||||
fun run(after: Long, onReset: () -> Unit, onEvent: (SeqEvent) -> Unit) {
|
||||
fun run(after: Long, onOpen: () -> Unit, onReset: () -> Unit, onEvent: (SeqEvent) -> Unit) {
|
||||
val connection =
|
||||
URL("${settings.baseUrl}/sessions/$sessionId/events?after=$after").openConnection()
|
||||
as HttpURLConnection
|
||||
@@ -55,6 +60,7 @@ class EventStream(private val settings: ServerSettings, private val sessionId: S
|
||||
throw ApiException(detail ?: "HTTP ${connection.responseCode} for the event stream")
|
||||
}
|
||||
|
||||
onOpen()
|
||||
val reader = connection.inputStream.bufferedReader()
|
||||
// SSE framing: `data:` and `event:` lines accumulate until a
|
||||
// blank line ends the frame. `id:` (the seq) is also inside the
|
||||
@@ -83,7 +89,7 @@ class EventStream(private val settings: ServerSettings, private val sessionId: S
|
||||
} catch (e: IOException) {
|
||||
if (!closed) {
|
||||
throw ApiException(
|
||||
"Lost the event stream (${e::class.simpleName}: ${e.message})",
|
||||
"Can't reach the server -- retrying. (${e.message ?: e::class.simpleName})",
|
||||
e,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user