Skip to content

Prevent focusing message if the listState flow was cancelled before a message item was loaded. #5854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

VelikovPetar
Copy link
Contributor

@VelikovPetar VelikovPetar commented Jun 27, 2025

🎯 Goal

Resolves: https://linear.app/stream/issue/AND-634/upgrade-to-latest-stream-sdk-causing-illegalstateexception-crash-in

Prevents calling focusMessage in the init block of the MessageListController if the listState flow was cancelled before a message was loaded. The following setup in the initialFocusMessage() method:

private fun initialFocusMessage() {
   messageId ?: return // No initial focus if no message id is provided
   scope.launch {
       listState
         .onCompletion {
            // ...
         }
         .first { it.messageItems.isNotEmpty() }
   }
}

behaves in the following way: The onCompletion block will be invoked in two scenarios:

  • When the flow is completed (by .first { it.messageItems.isNotEmpty() })
  • When the flow is cancelled (by cancelling the scope)

We should prevent calling the focusChannelMessage from the onCompletion block in the case when the flow cancelled, because the UI might not be present anymore OR no messages are loaded yet OR the user could already be disconnected.

Note: The crash stack trace is: ChatClient::connectUser() must be called before resolving any dependency - but the crash is triggered much earlier in the flow in the focusMessage method, where we attempt to focus a message when we cancel the MessageList scope.

(See attached videos and testing for reproduction steps)

🛠 Implementation details

  • Prevent calling focusChannelMessage from the onCompleted block in the MessageListController.initialFocusMessage() message.

🎨 UI Changes

Before After
focus-message-crash-before.mp4
focus-message-crash-after.mp4

🧪 Testing

Currently only reproduce-able after a process death

  1. Set background limit process to "No background processes"
  2. Apply the provided patch
  3. Open compose app
  4. Open a channel (note that the channel auto focuses the last message - change from patch)
  5. Put app in background
  6. Open a different app
  7. Return to compose sample
  8. The app should be restored on the MessageList in loading state (because we don't have reconnect logic after process death)
  9. Tap back (this cancels the scope)
  10. The app should not crash

Copy link
Contributor

github-actions bot commented Jun 27, 2025

SDK Size Comparison 📏

SDK Before After Difference Status
stream-chat-android-client 3.16 MB 3.16 MB 0.00 MB 🟢
stream-chat-android-offline 3.38 MB 3.38 MB 0.00 MB 🟢
stream-chat-android-ui-components 10.43 MB 10.43 MB 0.00 MB 🟢
stream-chat-android-compose 12.53 MB 12.53 MB -0.00 MB 🚀

Copy link

@VelikovPetar VelikovPetar marked this pull request as ready for review June 27, 2025 10:21
@VelikovPetar VelikovPetar requested a review from a team as a code owner June 27, 2025 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant