Commit 8077384
authored
fix: Fix redux store event handler leak in WCv2 (MetaMask#27932)
## **Description**
Fixes an issue where after a WCv2 connection could still send
chainChanged events to the WC relay when the connection was removed.
This was happening regardless of if the connection was removed on the
wallet side or signaled removed from the WC side.
This PR fixes that by properly unsubscribing from the store.
## **Changelog**
<!--
If this PR is not End-User-Facing and should not show up in the
CHANGELOG, you can choose to either:
1. Write `CHANGELOG entry: null`
2. Label with `no-changelog`
If this PR is End-User-Facing, please write a short User-Facing
description in the past tense like:
`CHANGELOG entry: Added a new tab for users to see their NFTs`
`CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker`
(This helps the Release Engineer do their job more quickly and
accurately)
-->
CHANGELOG entry: null
Not user facing.
## **Related issues**
Fixes: https://consensyssoftware.atlassian.net/browse/WAPI-1355
## **Manual testing steps**
1. Open app in iOS expo build
2. Open js debugger
3. add breakpoint to onStoreChange in WalletConnect2Session.ts
4. Connect a new WC session via [demo
app](https://react-app.walletconnect.com/)
5. You should see onStoreChange fire
6. Visit settings, experimental, and disconnect this session (long tap)
7. You should not see onStoreChange fire anymore
8. reconnect to demo dapp
9. You should see onStoreChange fire
10. disconnect from the demo dapp
11. you should not see onStoreChange fire anymore
Visit https://react-app.walletconnect.com/ in the native browser and
smoke test
## **Screenshots/Recordings**
<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->
### **Before**
<!-- [screenshots/recordings] -->
### **After**
<!-- [screenshots/recordings] -->
## **Pre-merge author checklist**
- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I've included tests if applicable
- [ ] I've documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I've applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
## **Pre-merge reviewer checklist**
- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Touches WalletConnect v2 session lifecycle/cleanup, so mistakes could
leave sessions partially disconnected or change disconnect timing. Logic
is straightforward and covered by updated unit tests, limiting risk.
>
> **Overview**
> Fixes a WalletConnect v2 listener leak by storing the Redux
`store.subscribe` unsubscribe in `WalletConnect2Session` and invoking it
during `removeListeners()`.
>
> Updates `WC2Manager.removeAll()` to **tear down every
`WalletConnect2Session` via `removeListeners()` before clearing the
sessions map**, preventing orphaned subscriptions/bridges when
`session_delete` fires after local state is cleared. Tests were extended
to assert the unsubscribe is called and `removeAll()` invokes
`removeListeners()` for each session.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
7633be5. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent d4cdad3 commit 8077384
4 files changed
Lines changed: 35 additions & 4 deletions
File tree
- app/core/WalletConnect
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
219 | 219 | | |
220 | 220 | | |
221 | 221 | | |
| 222 | + | |
222 | 223 | | |
223 | 224 | | |
224 | 225 | | |
| |||
296 | 297 | | |
297 | 298 | | |
298 | 299 | | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
299 | 303 | | |
300 | 304 | | |
301 | 305 | | |
| |||
438 | 442 | | |
439 | 443 | | |
440 | 444 | | |
| 445 | + | |
441 | 446 | | |
442 | 447 | | |
443 | 448 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| 75 | + | |
75 | 76 | | |
76 | 77 | | |
77 | 78 | | |
| |||
164 | 165 | | |
165 | 166 | | |
166 | 167 | | |
167 | | - | |
| 168 | + | |
168 | 169 | | |
169 | 170 | | |
170 | 171 | | |
| |||
791 | 792 | | |
792 | 793 | | |
793 | 794 | | |
| 795 | + | |
| 796 | + | |
794 | 797 | | |
795 | 798 | | |
796 | 799 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
735 | 735 | | |
736 | 736 | | |
737 | 737 | | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
738 | 755 | | |
739 | 756 | | |
740 | 757 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
382 | 382 | | |
383 | 383 | | |
384 | 384 | | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
385 | 394 | | |
386 | 395 | | |
387 | 396 | | |
| |||
394 | 403 | | |
395 | 404 | | |
396 | 405 | | |
397 | | - | |
398 | | - | |
399 | | - | |
400 | 406 | | |
401 | 407 | | |
402 | 408 | | |
| |||
0 commit comments