Fix the socket never reconnecting after a transient connectivity drop - #6663
Fix the socket never reconnecting after a transient connectivity drop#6663aleksandar-apostolov wants to merge 4 commits into
Conversation
NetworkStateProvider only notifies listeners when its cached flag changes, but ChatSocket concluded "no network" from the live isConnected() read without the provider ever learning. The cached value stayed true, so the network coming back was not seen as a transition and listeners were never told, leaving the socket offline for the lifetime of the process. Record the result of every direct read, and route the "all networks lost" branch of onLost through the same writer so it cannot desync either. Also drop NET_CAPABILITY_VALIDATED from the check. It reports whether the platform's connectivity probe passed, not whether a socket can be opened, so captive portals and revalidating networks read as a hard outage.
The state stopped the health monitor without scheduling a replacement and declared no transition for Event.Resume, so it could only be left if a network callback arrived. One missed callback stranded the socket until the process was killed. Keep a backed-off retry armed and restart the connection on resume, matching Disconnected.Stopped. The existing state machine test pinned the old behaviour, asserting that a resume left NetworkDisconnected unchanged.
observe() suppressed the first ON_RESUME in order to swallow the replay that addObserver dispatches when the owner is already resumed. Subscribing from a process that is not resumed gets no replay, so the suppression discarded a genuine foregrounding instead. Only suppress when the owner is already resumed at subscribe time.
Recording a positive read consumed the transition the listeners wait for, when isConnected() ran after the network returned but before the system callback landed. That is the same defect this PR fixes, mirrored, and it is reachable from app code through ClientState.isNetworkAvailable. Only a negative answer is recorded now, which is the direction that can strand the socket. Restore healthMonitor.stop() in NetworkDisconnected. The replacement retry emitted Event.WebSocketEventLost, which that state does not handle, so it was inert; worse, a pending one could knock a later Connecting state into Disconnected.WebSocketEventLost. Restore the NET_CAPABILITY_VALIDATED check. With the provider fixed a false negative is no longer terminal, so the behaviour change is not worth its risk in this PR.
PR checklist ✅All required conditions are satisfied:
🎉 Great job! This PR is ready for review. |
SDK Size Comparison 📏
|
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (6)
Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review. WalkthroughThe changes correct lifecycle resume event delivery, centralize network connectivity transitions, and restart network-disconnected sockets on lifecycle resume. New tests cover lifecycle replay, connectivity callbacks, deduplication, disconnection, and socket recovery. ChangesLifecycle and connectivity recovery
Estimated code review effort: 3 (Moderate) | ~20 minutes Merge Risk: ⚪ Minimal · up to The change targets socket recovery and includes unit tests; no actionable merge-blocking risk remains beyond normal checks and review. Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Description checkExplanation The description includes the goal, implementation, and testing details. It explains the affected socket behavior, the internal fixes, the lack of API changes, and the new unit tests. UI, checklist, reviewer, and GIF sections are omitted, but they are not critical for this non-UI change.
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |



Port of #6662 to develop.
Goal
Fixes AND-1450 — socket gets into
Disconnected.Networkand never comes back out, client stays offline until the process dies.Implementation
Provider now records direct
isConnected()reads so the cached flag can't go stale.Disconnected.NetworkhandlesEvent.Resume. Lifecycle observer only swallows the replayed resume. All internal, no API change.Testing
New unit tests.
Summary by CodeRabbit