Repository navigation
fix(android): keep the foreground service through reconnects - #33
Conversation
The keep-alive coordinator halted the foreground service whenever no session was Connected, which includes Reconnecting and the brief Disconnected that a network-change reconnect passes through. Android 12+ refuses to start a foreground service while the app is in the background, so with the screen locked the service could not come back: the start threw inside the connection's state change, the reconnect that raised it failed (or, in the backoff loop, skipped the auto-login), and without the wake lock the process was frozen. - Count Connecting and Reconnecting as active, and halt only after a 15 s grace period with nothing active. - Never let a keep-alive failure escape into the connection; log it. - Once the service runs, change its notification text in place instead of calling StartForegroundService again. - Move the coordinator into Core so it has tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Rqqt7WhTrKwT2PgaSu4HcZ
|
Caution Review failedThe pull request is closed. WalkthroughThe connection keep-alive coordinator moves to Core and manages session activity, reconnect requests, and delayed halts. MauiProgram routes network changes to the coordinator. Android retains the current notification status when the network changes. ChangesConnection keep-alive
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant MauiProgram
participant NetworkChangeSignal
participant ConnectionKeepAliveCoordinator
participant Session
MauiProgram->>NetworkChangeSignal: Subscribe ReconnectAll to Changed
NetworkChangeSignal->>ConnectionKeepAliveCoordinator: Changed invokes ReconnectAll
ConnectionKeepAliveCoordinator->>Session: ForceReconnectAsync
|
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
|
The Generated by Claude Code |
Requested by Grave · project thread
Symptom
On Android, locking the screen loses or closes telnet connections again.
Root cause
ConnectionKeepAliveCoordinatorhalted the foreground service whenever no session wasConnected. That includesReconnecting, and the briefDisconnectedthatForceReconnectAsyncpasses through after a network change (it callsConnectAsync, which disconnects first).Android 12+ refuses
StartForegroundServicewhile the app is in the background. So once a session blipped with the screen locked:Startthrew insideTelnetConnection.SetState(Connected), because the coordinator is a synchronousStateChangedsubscriber;ConnectAsync, leaving the session inErrorwith no retry. In the backoff loop it was swallowed as a failed attempt, the next attempt tore down the socket that had just connected, and the auto-login was skipped becauseSession.OnStateChangedraisesStateChangedbefore it sends the login;The Android code and toolchain did not change between v1.6 and v2.0: both release builds use SDK 10.0.300 and MAUI 10.0.20, and v2.0 used Android workload 36.1.69 (API 36). This failure mode has been there since the keep-alive was added. I could not find what makes the first drop happen more often now, and a device log would show it.
Fix
SharpClient.Core.Connection) so it can be tested.ConnectingandReconnectingcount as active. The service halts only after 15 s with nothing active, which covers the transientDisconnected.Start/Haltfailure is logged (it reaches the diagnostics log) and never reaches the connection. The next state change tries again.Startis called only when the notification text changes. Once the service runs,AndroidConnectionKeepAlivere-posts the notification instead of callingStartForegroundServiceagain.MauiProgramtoReconnectAll, which logs a failed reconnect instead of dropping it unobserved.Verification
ConnectionKeepAliveCoordinatorTests(7): start on connect, no halt throughReconnectingor throughDisconnected → Connecting → Connected, halt after the grace period, no restart for an unchanged status, a throwingStartcontained and retried,ReconnectAllreaching every session.Out of scope
Errorafter a long outage still cannot start the service from the background. That would need a battery-optimisation exemption.TelnetConnection.SetStatestill lets any subscriber exception fail a connect.🤖 Generated with Claude Code
https://claude.ai/code/session_01Rqqt7WhTrKwT2PgaSu4HcZ
Summary by CodeRabbit