Skip to content

Commit e059665

Browse files
VelikovPetarclaude
andauthored
Fix network callback and lifecycle observer not being unregistered during disconnect (#6125)
* Fix network callback and lifecycle observer not being unregistered during disconnect Co-Authored-By: Claude <noreply@anthropic.com> * Update CHANGELOG Co-Authored-By: Claude <noreply@anthropic.com> * Refactor code * Fix LogTag --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 24e88e6 commit e059665

3 files changed

Lines changed: 14 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
## stream-chat-android-client
1414
### 🐞 Fixed
1515
- Fix race condition crash when accessing `globalState` during disconnect. [#6122](https://github.com/GetStream/stream-chat-android/pull/6122)
16+
- Fix network callback and lifecycle observer not being unregistered during disconnect. [#6125](https://github.com/GetStream/stream-chat-android/pull/6125)
1617
- Fix crash when `onNext` is called on a disposed subscription. [#6126](https://github.com/GetStream/stream-chat-android/pull/6126)
1718

19+
1820
### ⬆️ Improved
1921

2022
### ✅ Added

stream-chat-android-client/src/main/java/io/getstream/chat/android/client/StreamLifecycleObserver.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import androidx.lifecycle.LifecycleOwner
2222
import io.getstream.chat.android.core.internal.coroutines.DispatcherProvider
2323
import io.getstream.log.taggedLogger
2424
import kotlinx.coroutines.CoroutineScope
25+
import kotlinx.coroutines.NonCancellable
2526
import kotlinx.coroutines.launch
2627
import kotlinx.coroutines.withContext
2728
import java.util.concurrent.atomic.AtomicBoolean
@@ -38,20 +39,20 @@ internal class StreamLifecycleObserver(
3839
private val isObserving = AtomicBoolean(false)
3940

4041
suspend fun observe(handler: LifecycleHandler) {
41-
if (isObserving.compareAndSet(false, true)) {
42-
recurringResumeEvent = false
43-
withContext(DispatcherProvider.Main) {
42+
withContext(DispatcherProvider.Main) {
43+
handlers = handlers + handler
44+
if (isObserving.compareAndSet(false, true)) {
45+
recurringResumeEvent = false
4446
lifecycle.addObserver(this@StreamLifecycleObserver)
4547
logger.v { "[observe] subscribed" }
4648
}
4749
}
48-
handlers = handlers + handler
4950
}
5051

5152
suspend fun dispose(handler: LifecycleHandler) {
52-
handlers = handlers - handler
53-
if (handlers.isEmpty() && isObserving.compareAndSet(true, false)) {
54-
withContext(DispatcherProvider.Main) {
53+
withContext(NonCancellable + DispatcherProvider.Main) {
54+
handlers = handlers - handler
55+
if (handlers.isEmpty() && isObserving.compareAndSet(true, false)) {
5556
lifecycle.removeObserver(this@StreamLifecycleObserver)
5657
logger.v { "[dispose] unsubscribed" }
5758
}

stream-chat-android-client/src/main/java/io/getstream/chat/android/client/socket/ChatSocket.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ internal open class ChatSocket(
141141
tokenManager.expireToken()
142142
streamWebSocket?.close()
143143
healthMonitor.stop()
144-
userScope.launch { disposeObservers() }
144+
disposeObservers()
145145
}
146146
is State.Disconnected.NetworkDisconnected -> {
147147
streamWebSocket?.close()
@@ -156,7 +156,7 @@ internal open class ChatSocket(
156156
tokenManager.expireToken()
157157
streamWebSocket?.close()
158158
healthMonitor.stop()
159-
userScope.launch { disposeObservers() }
159+
disposeObservers()
160160
}
161161
is State.Disconnected.DisconnectedTemporarily -> {
162162
healthMonitor.onDisconnected()
@@ -205,9 +205,9 @@ internal open class ChatSocket(
205205
networkStateProvider.subscribe(networkStateListener)
206206
}
207207

208-
private suspend fun disposeObservers() {
209-
lifecycleObserver.dispose(lifecycleHandler)
208+
private fun disposeObservers() {
210209
disposeNetworkStateObserver()
210+
userScope.launch { lifecycleObserver.dispose(lifecycleHandler) }
211211
}
212212

213213
private fun disposeNetworkStateObserver() {

0 commit comments

Comments
 (0)