Skip to content

fix(api): Don't block subscription events during WebSocket reconnect - #7282

Open
VarshithaPamisetty wants to merge 7 commits into
mainfrom
fix/api-subscription-events-blocked-on-reconnect
Open

fix(api): Don't block subscription events during WebSocket reconnect#7282
VarshithaPamisetty wants to merge 7 commits into
mainfrom
fix/api-subscription-events-blocked-on-reconnect

Conversation

@VarshithaPamisetty

Copy link
Copy Markdown
Contributor

Description

The WebSocket reconnect ran on the serial event queue (asyncExpand), so a slow reconnect blocked every event behind it: subscription data and keep-alives were received but never processed, freezing active subscriptions on a healthy socket. This moves reconnect off the queue so events keep flowing, and guards against shutdown mid-reconnect.

Testing

  • Regression test: data flows during an in-progress reconnect (fails without the change); plus a clean-shutdown-mid-reconnect test.
  • WebSocket suite 35/35; analyze/format clean (Dart 3.11 and 3.13).
  • Verified e2e against live AppSync: delivery, reconnect recovery, new subscription mid-reconnect.

Fixes #7001

@VarshithaPamisetty VarshithaPamisetty self-assigned this Aug 17, 2026
@codecov-commenter

codecov-commenter commented Aug 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 40.98%. Comparing base (3cb8311) to head (6dfbba8).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7282   +/-   ##
=======================================
  Coverage   40.98%   40.98%           
=======================================
  Files         121      121           
  Lines        8273     8273           
  Branches     3598     3598           
=======================================
  Hits         3391     3391           
  Misses       4882     4882           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@VarshithaPamisetty
VarshithaPamisetty marked this pull request as ready for review August 18, 2026 11:43
@VarshithaPamisetty
VarshithaPamisetty requested a review from a team as a code owner August 18, 2026 11:43
late WebSocketState _currentState;

/// The in-flight reconnection, if any. Ensures at most one runs at a time.
Future<void>? _reconnectOperation;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Don't we need to cancel or await this? E.g. in _close?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Tested awaiting it in _close() and it hangs shutdown (breaks the shutdown-during-reconnect test), and a Future can't be cancelled. Instead, _isShuttingDown makes the reconnect a no-op once the bloc closes.

Comment on lines +392 to +394
_reconnectOperation ??= _performReconnect(
_currentState,
).whenComplete(() => _reconnectOperation = null);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we handle errors that aren't Exception (https://api.dart.dev/dart-core/#exceptions)? _performReconnect catches Exception only (} on Exception catch (e, st) ).

@VarshithaPamisetty VarshithaPamisetty Aug 18, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

on Exception is intentional here. Per Effective Dart we shouldn't swallow Errors, and the one that actually surfaced in testing, add-after-close, is already prevented by the _isShuttingDown/_safeAdd guards. So leaving it as-is would be better.

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.

Subscription events are blocked while reconnect retry is in progress

3 participants