Skip to content

fix: implement single-flight connection handling and add tests for Wa…#7205

Closed
fairlighteth wants to merge 5 commits intodevelopfrom
fix/max-listeners
Closed

fix: implement single-flight connection handling and add tests for Wa…#7205
fairlighteth wants to merge 5 commits intodevelopfrom
fix/max-listeners

Conversation

@fairlighteth
Copy link
Copy Markdown
Contributor

@fairlighteth fairlighteth commented Mar 20, 2026

Summary

Reduce MaxListenersExceededWarning / register_error listeners added noise from the WalletConnect v2 path by fixing the app-side triggers instead of raising the listener limit.

This change covers the full repro we investigated:

  • dedupe concurrent WalletConnect activate() calls while no session exists
  • dedupe concurrent connectEagerly() calls
  • preserve the existing-session path for normal WalletConnect network switching
  • fully clean up transient pre-session WalletConnect providers during deactivate()
  • stop BlockNumberProvider from polling/subscribing through the wallet transport and use the chain RPC provider instead
  • retry failed WalletConnect activations through the correct per-chain cached connector
  • reset failed pre-session WalletConnect connectors only after cleanup, while preserving live sessions on switch-chain errors
  • add focused regression tests for concurrent activation, eager reconnect, transient cleanup, retry-chain preservation, and session-safe error handling

To Test

  1. Open cowswap-frontend locally and connect with a WalletConnect-compatible wallet
  • WalletConnect connection succeeds
  • The console does not show MaxListenersExceededWarning: Possible EventEmitter memory leak detected. ... register_error listeners added
  1. Cancel the WalletConnect flow a few times, click Try Again, then connect successfully
  • Repeated cancel/retry attempts still work
  • The warning does not appear after multiple canceled attempts followed by a successful connect
  • The app does not open duplicate WalletConnect connection flows
  1. Disconnect or switch away from WalletConnect, then reconnect with WalletConnect several times
  • Reconnecting still works consistently
  • The warning does not appear after repeated reconnect attempts
  • The app keeps using the correct wallet state after reconnecting
  1. Refresh the app with a remembered WalletConnect session
  • Eager reconnect restores the session successfully
  • The warning does not appear during app bootstrap
  1. While an existing WalletConnect session is active, switch networks if that flow is available in your environment
  • Network switching still works for an already connected WalletConnect session
  • A rejected switch-chain attempt does not break the existing WalletConnect session
  • Account and chain state stay in sync after the switch or failed switch
  1. Leave the app open on a connected wallet and let block updates continue normally
  • Block-based updates still work as expected
  • No WalletConnect listener warning appears from background block polling

Background

The warning was not coming from CoW-owned frontend event emitters. The register_error listener is added inside WalletConnect's JSON-RPC WebSocket/provider stack.

The issue turned out to have multiple app-side triggers:

  • overlapping WalletConnect bootstrap attempts from manual activation and eager reconnect
  • transient pre-session WalletConnect instances not being cleaned up aggressively enough after failed/canceled attempts
  • retry logic reusing or recreating the wrong per-chain WalletConnect connector after failures
  • block polling going through the wallet provider instead of the chain RPC provider

This patch fixes those triggers directly rather than increasing the listener limit, which would only hide the underlying problem.

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 20, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
cowfi Ready Ready Preview Mar 21, 2026 8:17pm
explorer-dev Ready Ready Preview Mar 21, 2026 8:17pm
swap-dev Ready Ready Preview Mar 21, 2026 8:17pm
widget-configurator Ready Ready Preview Mar 21, 2026 8:17pm
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
cosmos Ignored Ignored Mar 21, 2026 8:17pm
sdk-tools Ignored Ignored Preview Mar 21, 2026 8:17pm

Request Review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 20, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 5581417d-865b-485b-8105-1c3910d39d8f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/max-listeners

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@fairlighteth
Copy link
Copy Markdown
Contributor Author

Closing this one as the current assessment is that it will be superseded/resolved by #7061

Re-open in case that isn't resolved after merging #7061

@github-actions github-actions bot locked and limited conversation to collaborators Apr 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants