Skip to content

fix: reject pending requests when the popup is closed without PopupUnload - #1942

Draft
wcrozier12 wants to merge 1 commit into
masterfrom
fix/communicator-closed-popup
Draft

wcrozier12 wants to merge 1 commit into
masterfrom
fix/communicator-closed-popup

Conversation

@wcrozier12

Copy link
Copy Markdown
Contributor

Title: fix: reject pending requests when the popup is closed without PopupUnload

Summary

If the user closes the wallet popup before the wallet page loads, the page never posts PopupLoadedV2 or PopupUnload. The Communicator only learns that a popup went away from PopupUnload, so in that case the handshake and any pending request wait forever, and their message listeners stay attached.

There is a second effect. onMessage filters by origin only, and the next request opens a new popup because the old one reports closed. That new window's PopupLoadedV2 also resolves the stale waiter from the closed popup.

This change makes the Communicator poll popup.closed every 500 ms while it holds a popup. When the popup reports closed, it runs the existing disconnect() path, so every pending request rejects with the same userRejectedRequest('Request rejected') error users get today when the wallet posts PopupUnload.

  • The poll starts as soon as openPopup returns a window. That covers windows from a custom openFn and from the popup-blocked dialog's retry.
  • disconnect() clears the poll, so it stops on PopupUnload, on a detected close, and is replaced if a new popup is opened.
  • Reading closed is wrapped in a try/catch. If it throws, the poll skips that tick and checks again on the next one.
  • Nothing changes while the popup stays open or when PopupUnload arrives as it does today.

Not in this PR: checking event.source on incoming messages, and a public way to cancel a pending request. Those could be follow-ups.

How did you test your changes?

Added unit tests in Communicator.test.ts that use fake timers:

  • Popup closed before it loads: the handshake rejects with the user-rejected error, all listeners are removed, and no timer is left running.
  • Popup closed before it loads, then a new request: the old waiter does not post into the new popup.
  • Popup closed after a request was posted, with no PopupUnload: the request rejects with the user-rejected error.
  • Popup stays open: the request keeps waiting through 10 s of polling and resolves when the response arrives.
  • PopupUnload arrives as usual: one disconnect, the poll is cleared, and a later closed does not disconnect a second time.

Without the fix, the three closed-popup tests fail (two time out, one posts into the new popup).

Commands:

  • yarn build (packages/wallet-sdk): passes
  • yarn test --run (packages/wallet-sdk): 526 tests pass
  • yarn typecheck, yarn lint, yarn format:check: pass

…load

A popup closed before the wallet page loads never posts PopupLoadedV2 or
PopupUnload, so the handshake and any pending request waited forever and
their listeners stayed attached. A later popup's PopupLoadedV2 could then
resolve the stale waiter.

Poll popup.closed while the communicator holds a popup and run the existing
disconnect path when it reports closed, so pending requests reject with the
same user-rejected error as an unload. The poll is cleared on disconnect.
@cb-heimdall

Copy link
Copy Markdown
Collaborator

🟡 Heimdall Review Status

Requirement Status More Info
Reviews 🟡 0/2
Denominator calculation
Show calculation
1 if user is bot 0
1 if user is external 0
2 if repo is sensitive 0
From .codeflow.yml 1
Additional review requirements
Show calculation
Max 0
0
From CODEOWNERS 0
Global minimum 0
Max 1
1
1 if commit is unverified 1
1 if authored by an app that is not allowlisted and lacks verified human attribution 0
Sum 2

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants