Skip to content

Conversation

@Kriys94
Copy link
Contributor

@Kriys94 Kriys94 commented Oct 23, 2025

Explanation

This PR releases @metamask/core-backend@3.0.0 with significant improvements to WebSocket reconnection reliability and robustness for real-time balance updates.

Current State & Problems Addressed

The previous WebSocket implementation had several reliability issues:

  1. Duplicate reconnection timers could be created when multiple events triggered reconnects simultaneously, leading to memory leaks and inflated reconnect attempt counters
  2. Rapid reconnection loops could occur when the server accepted connections but immediately closed them
  3. Race conditions in reconnection scheduling could bypass exponential backoff, causing aggressive retry behavior
  4. Manual subscription cleanup was fragile when recovering from subscription issues or orphaned subscriptions

Solution

This release introduces robust reconnection logic with the following improvements:

1. Idempotent Reconnection Scheduling

  • Prevents duplicate reconnection timers from being created
  • Checks if a reconnect is already scheduled before creating a new timer
  • Eliminates memory leaks from orphaned timers
  • Prevents unnecessary growth of reconnect attempt counters

2. Stable Connection Timer

  • Connection must remain stable for 10 seconds before resetting reconnect attempts
  • Prevents rapid reconnection loops when server accepts then immediately closes connections
  • Provides grace period for connection stability before declaring success

3. Force Reconnection Method

  • New forceReconnection() public API for controlled subscription state cleanup
  • Performs a controlled disconnect-then-reconnect sequence with exponential backoff
  • Useful for recovering from subscription/unsubscription issues and cleaning up orphaned subscriptions
  • Exposed via BackendWebSocketService:forceReconnection messenger action

4. Improved Error Handling

  • Always schedules reconnect on connection failure (exponential backoff prevents aggressive retries)
  • Removes redundant schedule calls from error paths
  • Better logging throughout for easier debugging

5. Better State Management

  • disconnect() now properly resets reconnect attempts counter
  • Fixed race condition in connect() that could bypass exponential backoff
  • More predictable state transitions

Technical Details

Reconnection Behavior:

  • Uses Cockatiel's exponential backoff with decorrelated jitter to prevent thundering herd
  • Initial delay: 1000ms, max delay: 30000ms
  • Backoff sequence resets only after 10 seconds of stable connection
  • Automatic reconnect on unexpected disconnects, stays disconnected on manual disconnects

Breaking Changes:

  • AccountActivityService messenger allowed actions updated: removed BackendWebSocketService:disconnect, added BackendWebSocketService:forceReconnection

Impact

This improves the reliability of real-time balance updates across MetaMask Extension and Mobile by:

  • ✅ Preventing connection thrashing and resource exhaustion
  • ✅ Providing graceful recovery from network instability
  • ✅ Eliminating memory leaks from reconnection logic
  • ✅ Offering controlled subscription cleanup via forceReconnection()
  • ✅ Enabling better monitoring with improved logging

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed, highlighting breaking changes as necessary
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes (MetaMask Extension and Mobile updated)

Note

Releases @metamask/core-backend@3.0.0 with robust WebSocket reconnection and updates dependent packages/peer deps (assets-controllers@83, bridge-controller@55, bridge-status-controller@55); bumps monorepo version to 638.0.0.

  • Core Backend
    • Release @metamask/core-backend@3.0.0 with improved, idempotent WS reconnection, stable-connection timer, forceReconnection() API, logging and error‑handling tweaks.
    • BREAKING: AccountActivityService messenger actions updated (remove disconnect, add BackendWebSocketService:forceReconnection).
  • Assets
    • @metamask/assets-controllers@83.0.0: BREAKING peer dep bump to @metamask/core-backend@^3.0.0.
  • Bridge
    • @metamask/bridge-controller@55.0.0: BREAKING peer/dev dep bump to @metamask/assets-controllers@^83.0.0.
    • @metamask/bridge-status-controller@55.0.0: BREAKING peer/dev dep bump to @metamask/bridge-controller@^55.0.0.
  • Monorepo
    • Bump root version to 638.0.0; update changelogs and package versions accordingly.

Written by Cursor Bugbot for commit 54996e2. This will update automatically on new commits. Configure here.

@Kriys94 Kriys94 force-pushed the release/638.0.0 branch 2 times, most recently from d6462d7 to 13cc042 Compare October 23, 2025 09:02
- Bump `@metamask/base-controller` from `^8.4.1` to `^8.4.2` ([#6917](https://github.com/MetaMask/core/pull/6917))
- Update `AccountActivityService` to use new `forceReconnection()` method instead of manually calling disconnect/connect ([#6861](https://github.com/MetaMask/core/pull/6861))
- **BREAKING:** Update allowed actions for `AccountActivityService` messenger: remove `BackendWebSocketService:disconnect`, add `BackendWebSocketService:forceConnect` ([#6861](https://github.com/MetaMask/core/pull/6861))
- **BREAKING:** Update allowed actions for `AccountActivityService` messenger: remove `BackendWebSocketService:disconnect`, add `BackendWebSocketService:forceReconnection` ([#6861](https://github.com/MetaMask/core/pull/6861))
Copy link
Contributor Author

@Kriys94 Kriys94 Oct 23, 2025

Choose a reason for hiding this comment

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

For some reason there was a typo in the messenger action

@Kriys94 Kriys94 marked this pull request as ready for review October 23, 2025 09:25
@Kriys94 Kriys94 requested review from a team as code owners October 23, 2025 09:25
Copy link
Contributor

@cryptodev-2s cryptodev-2s left a comment

Choose a reason for hiding this comment

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

LGTM!

@Kriys94 Kriys94 merged commit 17e48a4 into main Oct 23, 2025
451 of 503 checks passed
@Kriys94 Kriys94 deleted the release/638.0.0 branch October 23, 2025 10:24
Gudahtt added a commit that referenced this pull request Oct 24, 2025
…r/multichain-transactions-controller

* origin/main: (35 commits)
  feat: `JsonRpcEngineV2` (#6176)
  Release 641.0.0 (#6940)
  feat: Add transaction emulation actions (#6935)
  Release/640.0.0 (#6934)
  fix(core-backend): control randomness to fix flaky test (#6936)
  chore: Add `@metamask-previews/*` to NPM age gate exceptions (#6937)
  Release/639.0.0 (#6931)
  feat: make getCryptoApproveTransactionParams synchronous (#6930)
  feat: add new actions to `KeyringController` (#6928)
  feat: add `getAccounts` to `AccountsController` (#6927)
  chore: remove `Monad Mainnet` single call balance contract and add into account v4 (#6929)
  Release/638.0.0 (#6923)
  fix: Downgrade `multiformats` to `^9.9.0` to avoid ESM-only dependency (#6920)
  Release/637.0.0 (#6919)
  feat(account-tree-controller): add callbacks for hidden and pinned data (#6910)
  Release 636.0.0 (#6918)
  fix(core-backend): reconnection logic (#6861)
  fix:  Tx state listener and signature coverage (#6906)
  Release/635.0.0 (#6917)
  fix(base-controller): add TypeScript declaration file for legacy module resolution (#6915)
  ...
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.

4 participants