Skip to content

fix: preserve instance-scoped event listeners on disconnect and fix beforeunload listener leak#170

Open
chakra-guy wants to merge 3 commits intomainfrom
fix/listener-cleanup-bugs
Open

fix: preserve instance-scoped event listeners on disconnect and fix beforeunload listener leak#170
chakra-guy wants to merge 3 commits intomainfrom
fix/listener-cleanup-bugs

Conversation

@chakra-guy
Copy link
Contributor

Summary

  • display_uri / wallet_sessionChanged not firing on reconnect: MetamaskConnectEVM.disconnect() was removing the display_uri and wallet_sessionChanged listeners from the core instance, but connect() never re-registered them. After a disconnect→reconnect cycle (especially in headless mode), the QR code URI was never emitted and session scope updates were silently lost. Fix: stop removing these instance-scoped listeners on disconnect — they should live for the lifetime of the SDK instance. Session-scoped listeners (notification handler) are still correctly cleaned up.

  • beforeunload listener never actually removed (memory leak): #createBeforeUnloadListener() called .bind(this) separately in addEventListener and removeEventListener, producing two different function references. Since removeEventListener requires the exact same reference, the cleanup was silently a no-op, leaking a listener on every connect/disconnect cycle. Fix: store the bound function once and reuse it.

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

…nload listener leak

- Stop removing `display_uri` and `wallet_sessionChanged` listeners in
  `MetamaskConnectEVM.disconnect()`. These are instance-scoped listeners
  registered once in the constructor and never re-registered, so removing
  them broke reconnection in headless mode (QR code URI and session
  updates stopped firing after disconnect+reconnect).

- Fix `#createBeforeUnloadListener()` in `MetaMaskConnectMultichain` to
  store the bound handler reference and reuse it for both addEventListener
  and removeEventListener. Previously, `.bind()` was called separately for
  each, creating different function references, so the listener was never
  actually removed (memory leak on each connect/disconnect cycle).
@chakra-guy chakra-guy requested a review from a team as a code owner February 6, 2026 14:44
wenfix
wenfix previously approved these changes Feb 6, 2026
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.

2 participants