Skip to content

Commit d36fb32

Browse files
VelikovPetarclaude
andauthored
Fix crash when onNext is called on disposed subscription (#6126)
* Fix crash when onNext is called on disposed subscription Co-Authored-By: Claude <noreply@anthropic.com> * Update CHANGELOG Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 40119da commit d36fb32

3 files changed

Lines changed: 9 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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 crash when `onNext` is called on a disposed subscription. [#6126](https://github.com/GetStream/stream-chat-android/pull/6126)
1617

1718
### ⬆️ Improved
1819

stream-chat-android-client/src/main/java/io/getstream/chat/android/client/utils/observable/Subscriptions.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ internal open class SubscriptionImpl(
4949
}
5050

5151
final override fun onNext(event: ChatEvent) {
52-
check(!isDisposed) { "Subscription already disposed, onNext should not be called on it" }
52+
if (isDisposed) return
5353

5454
if (filter(event)) {
5555
try {
@@ -78,7 +78,8 @@ internal class SuspendSubscription(
7878
}
7979

8080
override fun onNext(event: ChatEvent) {
81-
check(!isDisposed) { "Subscription already disposed, onNext should not be called on it" }
81+
if (isDisposed) return
82+
8283
scope.launch {
8384
if (filter(event)) {
8485
listener?.onEvent(event)

stream-chat-android-client/src/test/java/io/getstream/chat/android/client/utils/observable/SubscriptionImplTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,19 @@ internal class SubscriptionImplTest {
127127
}
128128

129129
@Test
130-
fun `onNext should throw IllegalStateException if subscription is already disposed`() {
130+
fun `onNext should not deliver event if subscription is already disposed`() {
131+
val mockListener = mock<ChatEventListener<ChatEvent>>()
131132
val subscription = SubscriptionImpl(
132133
filter = { true },
133-
listener = mock(),
134+
listener = mockListener,
134135
).apply {
135136
dispose()
136137
}
137138

138139
val event = mock<ChatEvent>()
140+
subscription.onNext(event)
139141

140-
assertThrows<IllegalStateException> {
141-
subscription.onNext(event)
142-
}
142+
verify(mockListener, never()).onEvent(event)
143143
}
144144

145145
@Test

0 commit comments

Comments
 (0)