Skip to content

Conversation

@xsahil03x
Copy link
Member

@xsahil03x xsahil03x commented Jul 17, 2025

Submit a pull request

Fixes: #2106

Description of the pull request

Previously, the RetryQueue listened to channel message events and removed messages from the queue when they were successfully sent. This introduced a race condition: when the retry loop reached finally, it would call removeFirst() assuming the message was still in the queue — but the event listener had already removed it. As a result, the next (unrelated) message would be incorrectly removed and skipped.

This change removes the _listenMessageEvents() logic and ensures that message removal is done explicitly inside _processQueue() using removeMessage(message). This guarantees that only the message currently being retried is removed.

Additionally:

  • Optimized add() to filter out duplicates more efficiently.
  • Improved containsMessage and removeMessage using unorderedElements.
  • Fixed _byDate() comparator to ensure null dates are sorted to the bottom.

Screenshots / Videos

Before After
Screen_recording_20250717_022159.mp4
Screen_recording_20250717_021731.mp4

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of message retry queue management, ensuring messages are properly tracked and removed after retry attempts.
  • Refactor
    • Streamlined message filtering and queue operations for better performance and maintainability.
  • New Features
    • Added configurable retry policy with customizable retry conditions for network errors.

Previously, the RetryQueue listened to channel message events and removed messages
from the queue when they were successfully sent. This introduced a race condition:
when the retry loop reached `finally`, it would call `removeFirst()` assuming the
message was still in the queue — but the event listener had already removed it.
As a result, the next (unrelated) message would be incorrectly removed and skipped.

This change removes the `_listenMessageEvents()` logic and ensures that message
removal is done explicitly inside `_processQueue()` using `removeMessage(message)`.
This guarantees that only the message currently being retried is removed.

Additionally:
- Optimized `add()` to filter out duplicates more efficiently.
- Improved `containsMessage` and `removeMessage` using `unorderedElements`.
- Fixed `_byDate()` comparator to ensure null dates are sorted to the bottom.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 17, 2025

Walkthrough

The changes refactor the message retry queue logic by removing the event listener for message events, simplifying message filtering, updating how messages are removed from the queue, and improving the internal queue element lookup and removal methods. The message comparison logic for ordering has also been adjusted.

Changes

File(s) Change Summary
packages/stream_chat/lib/src/client/retry_queue.dart Removed _listenMessageEvents and its usage; simplified message filtering; updated queue removal logic; revised message comparison; optimized extension methods for queue element lookup and removal.
packages/stream_chat/CHANGELOG.md Added bug fix entry for RetryQueue skipping messages due to premature removal.
packages/stream_chat/lib/stream_chat.dart Added export of src/client/retry_policy.dart to public API.
sample_app/lib/app.dart Added retryPolicy configuration to StreamChatClient instantiation with max retries and conditional retry logic.
packages/stream_chat/test/src/client/channel_test.dart Removed unused import of retry_policy.dart.
packages/stream_chat/test/src/client/retry_queue_test.dart Removed unused import of RetryPolicy.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant RetryQueue
    participant MessageQueue

    Client->>RetryQueue: Add message to retry queue
    RetryQueue->>MessageQueue: Insert message (with new filtering)
    loop On network reconnect or retry trigger
        RetryQueue->>MessageQueue: Process next message (by date)
        MessageQueue-->>RetryQueue: Provide message
        RetryQueue->>Client: Attempt send
        alt Send successful or max retries
            RetryQueue->>MessageQueue: Remove specific message
        end
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure all offline messages are sent on reconnect, preserving original send order (#2106)
Prevent loss of messages and order mismatch during offline-to-online transition (#2106)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Suggested reviewers

  • renefloor

Poem

In the warren of code, where messages queue,
The rabbits have tidied the logic anew.
No more lost carrots—each message in line,
Sorted and sent, in order just fine.
With a hop and a skip, the bugs disappear—
The chat flows as smoothly as springtime each year!
🐇✨


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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/stream_chat/lib/src/client/retry_queue.dart (1)

92-94: Critical fix for the race condition issue.

This change correctly removes the specific message being processed instead of assuming it's the first in the queue, which prevents accidentally removing unrelated messages. This is the core fix for the reported issue.

Note: The removeMessage method now returns a boolean, but the return value is not being checked here. Consider logging a warning if the removal fails for debugging purposes.

       } finally {
         // remove the message from the queue after it's handled.
-        _messageQueue.removeMessage(message);
+        if (!_messageQueue.removeMessage(message)) {
+          logger?.warning('Failed to remove message ${message.id} from retry queue');
+        }
       }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a7b49f5 and d286372.

📒 Files selected for processing (1)
  • packages/stream_chat/lib/src/client/retry_queue.dart (4 hunks)
🔇 Additional comments (5)
packages/stream_chat/lib/src/client/retry_queue.dart (5)

51-51: Good refactoring to improve readability.

The use of whereNot with the method reference makes the filtering logic more concise and declarative.


116-119: Correct adjustment to null date handling in the comparator.

The inverted null handling now ensures that messages with null dates are sorted to the bottom of the priority queue, preventing them from blocking the processing of properly dated messages. This is a sensible improvement to the queue processing order.


135-143: Efficient implementation using unorderedElements.

Good performance improvement by using unorderedElements instead of converting to a list. The boolean return value also provides useful feedback about the operation's success.


145-151: Performance improvement for message lookup.

Using unorderedElements avoids unnecessary list creation and provides better performance for the containment check.


14-17: Confirmed: Event listener removal prevents race condition.

The removal of _listenMessageEvents() from the constructor is the key fix for the race condition. By eliminating the event-driven message removal and relying solely on explicit removal in _processQueue(), the queue management is now deterministic and thread-safe.

@codecov
Copy link

codecov bot commented Jul 17, 2025

Codecov Report

Attention: Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.

Project coverage is 63.55%. Comparing base (a7b49f5) to head (dc7dc15).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...ckages/stream_chat/lib/src/client/retry_queue.dart 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2308      +/-   ##
==========================================
+ Coverage   63.50%   63.55%   +0.04%     
==========================================
  Files         409      409              
  Lines       25584    25571      -13     
==========================================
+ Hits        16247    16251       +4     
+ Misses       9337     9320      -17     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@xsahil03x xsahil03x requested a review from renefloor July 17, 2025 00:26
renefloor
renefloor previously approved these changes Jul 17, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 56ecffa and dc7dc15.

📒 Files selected for processing (5)
  • packages/stream_chat/lib/src/client/retry_queue.dart (4 hunks)
  • packages/stream_chat/lib/stream_chat.dart (1 hunks)
  • packages/stream_chat/test/src/client/channel_test.dart (0 hunks)
  • packages/stream_chat/test/src/client/retry_queue_test.dart (0 hunks)
  • sample_app/lib/app.dart (1 hunks)
💤 Files with no reviewable changes (2)
  • packages/stream_chat/test/src/client/retry_queue_test.dart
  • packages/stream_chat/test/src/client/channel_test.dart
✅ Files skipped from review due to trivial changes (1)
  • packages/stream_chat/lib/stream_chat.dart
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/stream_chat/lib/src/client/retry_queue.dart
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: stream_chat_flutter
  • GitHub Check: stream_chat_localizations
  • GitHub Check: stream_chat_persistence
  • GitHub Check: stream_chat_flutter_core
  • GitHub Check: test
  • GitHub Check: analyze_legacy_versions
  • GitHub Check: stream_chat
  • GitHub Check: build (ios)
  • GitHub Check: build (android)
  • GitHub Check: analyze

@xsahil03x xsahil03x merged commit 89d1c1e into master Jul 17, 2025
19 checks passed
@xsahil03x xsahil03x deleted the fix/retry-mechanism branch July 17, 2025 11:50
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.

Offline Messages Not Sending Properly & Order Mismatch on Reconnect - stream_chat_persistence Issue

3 participants