MetaMask / MetaMask/metamask-extension
[State Sync Redesign 1.5] Add port disconnect detection for UI contexts
- Dominant language
- TypeScript
- Stars
- 13.2k
- Forks
- 5.6k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 451
Description
The UI has no `onDisconnect` handler on its `chrome.runtime.Port`. When the MV3 service worker terminates (idle timeout, memory pressure, Chrome update), the port dies and the UI keeps running with a dead connection. All subsequent RPC calls fail with `DisconnectError`. Content scripts auto-reconnect (`provider-stream.ts`, 1s delay), but UI contexts (popup, notification, fullscreen, sidepanel) do not.
This is a pre-existing gap, but Epic 1 (Issue 1.3) amplifies it: the current 200ms debounce provides accidental resilience — the UI is already ~200ms stale, so a sub-200ms disconnect gap is invisible. Removing the debounce means a dead port results in zero state delivery; the UI diverges immediately.
## Steps
1. Add `port.onDisconnect` listener in the UI port setup (`ui/index.js` or `setupMultiplex`)
2. On disconnect, surface a user-visible indicator (e.g., "Reconnecting..." banner)
3. Distinguish between intentional close (popup/notification dismissed) and service worker termination
4. For popup/notification: reload the window (short-lived, no unsaved state)
5. For fullscreen/sidepanel: trigger reconnection (see Issue 1.6)
## Files
- `ui/index.js`
- `app/scripts/platforms/extension.js` (port setup)
## Risk
Medium — must correctly distinguish intentional close vs. service worker termination. Incorrect detection could trigger unnecessary reloads.
## Dependencies
Should ship alongside or shortly after Issue 1.3 (debounce removal).
## Context
Part of [State Sync Redesign — Epic 1: Transport optimization](#40059). See ADR risk assessment for full analysis.
Contributor guide
Research direction
Start with the UI port setup in ui/index.js and app/scripts/platforms/extension.js, then review the referenced provider-stream.ts reconnect behavior. Define how intentional popup or notification closes differ from service worker termination, and account for the fullscreen and sidepanel path described in Issue 1.6. Done means disconnects show the appropriate indicator and recover or reload without unnecessary reloads.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100