Fix network callback and lifecycle observer not being unregistered during disconnect#6125
Conversation
…ring disconnect Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Claude <[email protected]>
a1e06c3 to
e673393
Compare
SDK Size Comparison 📏
|
WalkthroughThis PR fixes network callback and lifecycle observer cleanup during disconnection. It refactors handler management in StreamLifecycleObserver to ensure proper thread safety and context-aware cancellation, and modifies ChatSocket to execute disposeObservers synchronously with internal async cleanup of the lifecycle observer. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 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 |
PR checklist ✅All required conditions are satisfied:
🎉 Great job! This PR is ready for review. |
|


Goal
During
ChatClient.disconnect(), thedisposeObservers()call was launched in a fire-and-forget coroutine that could be cancelled byuserScope.cancelChildren()before completing. This left network callbacks and lifecycle observers registered.Note: This is a potential cause for TooManyRequestsException thrown by android.net.ConnectivityManager.registerNetworkCallback.
resolves: https://linear.app/stream/issue/AND-1043/crash-in-chateventsobservable
Implementation
ChatSocket.kt:
disposeNetworkStateObserver()synchronously (not in a coroutine) so it cannot be cancelleddisposeObservers()functionStreamLifecycleObserver.kt:
NonCancellablecontext indispose()to ensurelifecycle.removeObserver()completes even if the parent coroutine is cancelled🎨 UI Changes
No UI changes.
Testing
This can be reproduced by rapidly logging in and logging out. In about 1 out of 10 logouts,
ChatSocket.userScopeis cancelled before the clean-up completes. After this change, it should never fail to unregister the network and lifecycle observers.Summary by CodeRabbit
Bug Fixes