Skip to content

Comments

Fix crash when onNext is called on disposed subscription#6126

Merged
aleksandar-apostolov merged 2 commits intodevelopfrom
bug/ignore_instead_of_crashing_disposed_subscriptions
Feb 4, 2026
Merged

Fix crash when onNext is called on disposed subscription#6126
aleksandar-apostolov merged 2 commits intodevelopfrom
bug/ignore_instead_of_crashing_disposed_subscriptions

Conversation

@VelikovPetar
Copy link
Contributor

@VelikovPetar VelikovPetar commented Feb 4, 2026

🎯 Goal

Fix a race condition crash that occurs when onNext is called on a subscription that has already been disposed:

Fatal Exception: java.lang.IllegalStateException: Subscription already disposed, onNext should not be called on it
at io.getstream.chat.android.client.utils.observable.SubscriptionImpl.onNext(Subscriptions.kt:52)

The crash happens due to a TOCTOU (time-of-check to time-of-use) race condition between:

  1. ChatEventsObservable.notifySubscriptions() checking subscription.isDisposed → returns false
  2. Another thread calling subscription.dispose() → sets isDisposed = true
  3. notifySubscriptions() calling subscription.onNext(event)check(!isDisposed) throws

🛠 Implementation details

  • Replace check(!isDisposed) with if (isDisposed) return in both SubscriptionImpl and SuspendSubscription
  • This handles the race condition gracefully by returning early instead of crashing
  • The listener?.onEvent() safe call provides additional safety if listener becomes null mid-execution
  • Updated tests to verify the new behavior (no exception, listener not called when disposed)

🎨 UI Changes

No UI changes.

🧪 Testing

  • Unit tests updated and passing
  • The race condition is inherently difficult to reproduce, but the fix ensures that even if the race occurs, the app won't crash

Summary by CodeRabbit

  • Bug Fixes
    • Fixed crash when onNext is called on a disposed subscription. The app now gracefully handles this scenario instead of throwing an exception.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

SDK Size Comparison 📏

SDK Before After Difference Status
stream-chat-android-client 5.26 MB 5.26 MB 0.00 MB 🟢
stream-chat-android-offline 5.48 MB 5.48 MB 0.00 MB 🟢
stream-chat-android-ui-components 10.63 MB 10.63 MB 0.00 MB 🟢
stream-chat-android-compose 12.84 MB 12.84 MB 0.00 MB 🟢

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 4, 2026

@aleksandar-apostolov aleksandar-apostolov marked this pull request as ready for review February 4, 2026 11:18
@aleksandar-apostolov aleksandar-apostolov requested a review from a team as a code owner February 4, 2026 11:18
@aleksandar-apostolov aleksandar-apostolov merged commit d36fb32 into develop Feb 4, 2026
14 checks passed
@aleksandar-apostolov aleksandar-apostolov deleted the bug/ignore_instead_of_crashing_disposed_subscriptions branch February 4, 2026 11:18
@coderabbitai
Copy link

coderabbitai bot commented Feb 4, 2026

Caution

Review failed

The pull request is closed.

Walkthrough

This pull request fixes a crash that occurred when onNext is called on a disposed subscription by replacing exception throws with silent returns in SubscriptionImpl and SuspendSubscription classes, with corresponding test updates to verify the new no-event-delivery behavior.

Changes

Cohort / File(s) Summary
Documentation
CHANGELOG.md
Added changelog entry documenting the crash fix for disposed subscriptions.
Core Subscription Logic
stream-chat-android-client/src/main/java/io/getstream/chat/android/client/utils/observable/Subscriptions.kt
Replaced check() assertion with early returns in onNext() for both SubscriptionImpl and SuspendSubscription to silently ignore calls on disposed subscriptions instead of throwing IllegalStateException.
Tests
stream-chat-android-client/src/test/java/io/getstream/chat/android/client/utils/observable/SubscriptionImplTest.kt
Updated test to verify no event delivery occurs when onNext() is called on a disposed subscription, removing prior expectation of exception throwing.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested reviewers

  • gpunto

Poem

🐰 Hoppy times ahead, they say with glee,
No crashes when disposed, just silently be,
onNext whispers softly and fades away,
A gentle return, the rabbit's way! 🌿

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bug/ignore_instead_of_crashing_disposed_subscriptions

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@aleksandar-apostolov aleksandar-apostolov added the pr:bug Bug fix label Feb 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr:bug Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants