Skip to content

Conversation

@salimtb
Copy link
Contributor

@salimtb salimtb commented Jun 4, 2025

Description

This PR improves the resilience of the createCaipStream utility by gracefully handling stream shutdowns triggered by tab or iframe closures. Previously, when a user clicked an external link (e.g., to a help article) from within a MetaMask confirmation popup, the resulting tab load would create a CAIP stream, and when that tab closed, it triggered "Premature close" errors.

The fix listens for low-level portStream termination events (close, end, and onDisconnect) and explicitly ends the CAIP sub-stream, allowing the pipeline to close cleanly. This prevents noisy, non-actionable errors from polluting logs or Sentry reports, without affecting real error detection or functionality.

Open in GitHub Codespaces

Related issues

Fixes: #26284

Manual testing steps

  1. Choose any Solana account
  2. Open the MetaMask confirmation popup (e.g., for adding a network).
  3. Click a link in the link text "network scams and security risks"
  4. A new tab will open.
  5. Close the new tab.
  6. ✅ Observe: no “Premature close” error appears in the console or Sentry.

Screenshots/Recordings

Before

before.mov

After

after.mov

Pre-merge author checklist

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.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 4, 2025

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@salimtb salimtb requested a review from jiexi June 4, 2025 12:10
@metamaskbot
Copy link
Collaborator

Builds ready [eb7c3ae]
UI Startup Metrics (1236 ± 63 ms)
PlatformBuildTypePageMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P 75 (ms)P 95 (ms)
ChromeBrowserifyHomeuiStartup1236112114336312751339
load107196712535911151168
domContentLoaded106495512455911071163
domInteractive17144351730
firstPaint778141125041410931168
backgroundConnect84274815
firstReactRender20173832125
getState1462972027
initialActions001001
loadScripts81470997458864911
setupStore85343811
WebpackHomeuiStartup21741657273927723592642
load16931295227423418242171
domContentLoaded16871291226923318172160
domInteractive161179111442
firstPaint1796055886215343
backgroundConnect2396692741
firstReactRender14244408107140360
getState1357291529
initialActions317135
loadScripts16841290226723218152145
setupStore3563326919286
FirefoxBrowserifyHomeuiStartup13631174174913114121697
load12001051154210012531428
domContentLoaded11991050154110012531428
domInteractive973520727108146
firstPaintNaNNaNNaNNaNNaNNaN
backgroundConnect211381102244
firstReactRender24204952432
getState13422630838
initialActions001001
loadScripts1181103815289912351404
setupStore9417619628
WebpackHomeuiStartup15591379203214916321910
load13551196181313814361668
domContentLoaded13541195181213814361668
domInteractive77511411981121
firstPaintNaNNaNNaNNaNNaNNaN
backgroundConnect22156872339
firstReactRender42317564450
getState10516316929
initialActions001011
loadScripts13351175179613714201652
setupStore10525024811
Benchmark value 1237 exceeds gate value 1234 for chrome browserify home mean uiStartup
Benchmark value 1072 exceeds gate value 1070 for chrome browserify home mean load
Benchmark value 1065 exceeds gate value 1061 for chrome browserify home mean domContentLoaded
Benchmark value 35 exceeds gate value 32 for chrome webpack home mean setupStore
Benchmark value 2643 exceeds gate value 2454 for chrome webpack home p95 uiStartup
Benchmark value 2171 exceeds gate value 2030 for chrome webpack home p95 load
Benchmark value 2161 exceeds gate value 2005 for chrome webpack home p95 domContentLoaded
Benchmark value 344 exceeds gate value 334 for chrome webpack home p95 firstPaint
Benchmark value 2146 exceeds gate value 1970 for chrome webpack home p95 loadScripts
Benchmark value 286 exceeds gate value 65 for chrome webpack home p95 setupStore
Benchmark value 14 exceeds gate value 11 for firefox browserify home mean getState
Benchmark value 10 exceeds gate value 9 for firefox browserify home mean setupStore
Benchmark value 1697 exceeds gate value 1660 for firefox browserify home p95 uiStartup
Benchmark value 38 exceeds gate value 24 for firefox browserify home p95 getState
Benchmark value 28 exceeds gate value 27 for firefox browserify home p95 setupStore
Benchmark value 42 exceeds gate value 38 for firefox webpack home mean firstReactRender
Benchmark value 1668 exceeds gate value 1660 for firefox webpack home p95 load
Benchmark value 1668 exceeds gate value 1660 for firefox webpack home p95 domContentLoaded
Benchmark value 1652 exceeds gate value 1630 for firefox webpack home p95 loadScripts
Sum of mean exceeds: 20ms | Sum of p95 exceeds: 983ms
Sum of all benchmark exceeds: 1003ms

Bundle size diffs [🚀 Bundle size reduced!]
  • background: -23.29 KiB (-0.44%)
  • ui: 4 KiB (0.06%)
  • common: -7.74 KiB (-0.09%)

@salimtb salimtb marked this pull request as ready for review June 4, 2025 12:45
@metamaskbot
Copy link
Collaborator

Builds ready [2d2c797]
UI Startup Metrics (1215 ± 62 ms)
PlatformBuildTypePageMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P 75 (ms)P 95 (ms)
ChromeBrowserifyHomeuiStartup1215111714146212501336
load105094112075610851160
domContentLoaded104293712035410761140
domInteractive17136661629
firstPaint744131118240610661148
backgroundConnect95738827
firstReactRender20174442128
getState15659102032
initialActions001001
loadScripts79269395154826882
setupStore85172813
WebpackHomeuiStartup21291575265823922682561
load16601225200619117861979
domContentLoaded16531221199318917821963
domInteractive161263111353
firstPaint1586932259193274
backgroundConnect22134172537
firstReactRender1314437099116357
getState1143141317
initialActions315134
loadScripts16511220198218817811952
setupStore3873197818299
FirefoxBrowserifyHomeuiStartup13491177191413214031624
load11901043178711412431433
domContentLoaded11901042178611412431433
domInteractive963631333102158
firstPaintNaNNaNNaNNaNNaNNaN
backgroundConnect2213103132155
firstReactRender23205042329
getState11419324820
initialActions002001
loadScripts11721027177211112261414
setupStore8419319711
WebpackHomeuiStartup15831359215515716551952
load13751188188113214481635
domContentLoaded13751187188113214481635
domInteractive76321561783107
firstPaintNaNNaNNaNNaNNaNNaN
backgroundConnect2615288282348
firstReactRender41286054448
getState1058812922
initialActions102112
loadScripts13551171186213114311606
setupStore95909818
Benchmark value 28 exceeds gate value 18 for chrome browserify home p95 backgroundConnect
Benchmark value 38 exceeds gate value 32 for chrome webpack home mean setupStore
Benchmark value 2561 exceeds gate value 2454 for chrome webpack home p95 uiStartup
Benchmark value 299 exceeds gate value 65 for chrome webpack home p95 setupStore
Benchmark value 12 exceeds gate value 11 for firefox browserify home mean getState
Benchmark value 42 exceeds gate value 38 for firefox webpack home mean firstReactRender
Benchmark value 1952 exceeds gate value 1935 for firefox webpack home p95 uiStartup
Sum of mean exceeds: 11ms | Sum of p95 exceeds: 368ms
Sum of all benchmark exceeds: 379ms

Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 326 Bytes (0.01%)
  • ui: 0 Bytes (0%)
  • common: 0 Bytes (0%)

@metamaskbot
Copy link
Collaborator

Builds ready [29a7955]
UI Startup Metrics (1220 ± 73 ms)
PlatformBuildTypePageMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P 75 (ms)P 95 (ms)
ChromeBrowserifyHomeuiStartup1220107114047312681369
load105994012316611061167
domContentLoaded105393512246611011160
domInteractive16133141628
firstPaint748129123941810781142
backgroundConnect84244818
firstReactRender21166872034
getState1464081930
initialActions001001
loadScripts80668896464858908
setupStore85253811
WebpackHomeuiStartup22301793274421223402617
load17591371213519818902059
domContentLoaded17521367213019518852048
domInteractive171258111453
firstPaint1746472981198295
backgroundConnect2611190232841
firstReactRender14943368111294353
getState1256471425
initialActions317135
loadScripts17471365212819218832036
setupStore3473527220310
FirefoxBrowserifyHomeuiStartup13741184212416314271761
load12101050177212612561533
domContentLoaded12101050177212612561533
domInteractive1003424230113151
firstPaintNaNNaNNaNNaNNaNNaN
backgroundConnect2813474562239
firstReactRender24205752430
getState13424033810
initialActions001001
loadScripts11891036175012012361379
setupStore83729720
WebpackHomeuiStartup16071391208914916781924
load13981192182914014731700
domContentLoaded13981192182914014721700
domInteractive78381532083125
firstPaintNaNNaNNaNNaNNaNNaN
backgroundConnect21144762233
firstReactRender42295854549
getState1053361028
initialActions002111
loadScripts13791165180814014561682
setupStore95657821
Benchmark value 1370 exceeds gate value 1365 for chrome browserify home p95 uiStartup
Benchmark value 2231 exceeds gate value 2192 for chrome webpack home mean uiStartup
Benchmark value 1760 exceeds gate value 1711 for chrome webpack home mean load
Benchmark value 1752 exceeds gate value 1704 for chrome webpack home mean domContentLoaded
Benchmark value 1747 exceeds gate value 1699 for chrome webpack home mean loadScripts
Benchmark value 34 exceeds gate value 32 for chrome webpack home mean setupStore
Benchmark value 2617 exceeds gate value 2454 for chrome webpack home p95 uiStartup
Benchmark value 2059 exceeds gate value 2030 for chrome webpack home p95 load
Benchmark value 2048 exceeds gate value 2005 for chrome webpack home p95 domContentLoaded
Benchmark value 2037 exceeds gate value 1970 for chrome webpack home p95 loadScripts
Benchmark value 310 exceeds gate value 65 for chrome webpack home p95 setupStore
Benchmark value 28 exceeds gate value 25 for firefox browserify home mean backgroundConnect
Benchmark value 13 exceeds gate value 11 for firefox browserify home mean getState
Benchmark value 1761 exceeds gate value 1660 for firefox browserify home p95 uiStartup
Benchmark value 1533 exceeds gate value 1495 for firefox browserify home p95 load
Benchmark value 1533 exceeds gate value 1495 for firefox browserify home p95 domContentLoaded
Benchmark value 1398 exceeds gate value 1380 for firefox webpack home mean load
Benchmark value 1398 exceeds gate value 1380 for firefox webpack home mean domContentLoaded
Benchmark value 42 exceeds gate value 38 for firefox webpack home mean firstReactRender
Benchmark value 1379 exceeds gate value 1360 for firefox webpack home mean loadScripts
Benchmark value 1700 exceeds gate value 1660 for firefox webpack home p95 load
Benchmark value 1700 exceeds gate value 1660 for firefox webpack home p95 domContentLoaded
Benchmark value 1682 exceeds gate value 1630 for firefox webpack home p95 loadScripts
Sum of mean exceeds: 250ms | Sum of p95 exceeds: 861ms
Sum of all benchmark exceeds: 1111ms

Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 326 Bytes (0.01%)
  • ui: 0 Bytes (0%)
  • common: 0 Bytes (0%)

@metamaskbot
Copy link
Collaborator

Builds ready [936c2a4]
UI Startup Metrics (1220 ± 69 ms)
PlatformBuildTypePageMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P 75 (ms)P 95 (ms)
ChromeBrowserifyHomeuiStartup1220107815196912561332
load105894813276310871165
domContentLoaded105294413236310811156
domInteractive16134151629
firstPaint76375123141710751160
backgroundConnect84325822
firstReactRender21165162137
getState1564182031
initialActions001001
loadScripts805702106462833917
setupStore85263915
WebpackHomeuiStartup20741593255022922162484
load16361255199417817561939
domContentLoaded16301251198417717511920
domInteractive15115481339
firstPaint1536631652180261
backgroundConnect249327312438
firstReactRender114433509293344
getState124155151119
initialActions613093134
loadScripts16281250198317517481909
setupStore3163126817299
FirefoxBrowserifyHomeuiStartup14361192221018515101873
load12621049177914013191562
domContentLoaded12621049177814013191562
domInteractive993617126114148
firstPaintNaNNaNNaNNaNNaNNaN
backgroundConnect2414340332133
firstReactRender24213532532
getState12421023937
initialActions001001
loadScripts12421038176313613021538
setupStore11426731713
WebpackHomeuiStartup15701358218217416681979
load13691177196616214611708
domContentLoaded13681177196616214611708
domInteractive79435685181111
firstPaintNaNNaNNaNNaNNaNNaN
backgroundConnect20145662230
firstReactRender40285344348
getState84294816
initialActions002111
loadScripts13511159194616214441689
setupStore10428628713
Benchmark value 22 exceeds gate value 18 for chrome browserify home p95 backgroundConnect
Benchmark value 2484 exceeds gate value 2454 for chrome webpack home p95 uiStartup
Benchmark value 299 exceeds gate value 65 for chrome webpack home p95 setupStore
Benchmark value 1437 exceeds gate value 1405 for firefox browserify home mean uiStartup
Benchmark value 1262 exceeds gate value 1245 for firefox browserify home mean load
Benchmark value 1262 exceeds gate value 1239 for firefox browserify home mean domContentLoaded
Benchmark value 12 exceeds gate value 11 for firefox browserify home mean getState
Benchmark value 1242 exceeds gate value 1230 for firefox browserify home mean loadScripts
Benchmark value 12 exceeds gate value 9 for firefox browserify home mean setupStore
Benchmark value 1873 exceeds gate value 1660 for firefox browserify home p95 uiStartup
Benchmark value 1562 exceeds gate value 1495 for firefox browserify home p95 load
Benchmark value 1562 exceeds gate value 1495 for firefox browserify home p95 domContentLoaded
Benchmark value 37 exceeds gate value 24 for firefox browserify home p95 getState
Benchmark value 1538 exceeds gate value 1475 for firefox browserify home p95 loadScripts
Benchmark value 41 exceeds gate value 38 for firefox webpack home mean firstReactRender
Benchmark value 1979 exceeds gate value 1935 for firefox webpack home p95 uiStartup
Benchmark value 1708 exceeds gate value 1660 for firefox webpack home p95 load
Benchmark value 1708 exceeds gate value 1660 for firefox webpack home p95 domContentLoaded
Benchmark value 1689 exceeds gate value 1630 for firefox webpack home p95 loadScripts
Sum of mean exceeds: 91ms | Sum of p95 exceeds: 890ms
Sum of all benchmark exceeds: 981ms

Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 326 Bytes (0.01%)
  • ui: 0 Bytes (0%)
  • common: 0 Bytes (0%)

@metamaskbot
Copy link
Collaborator

Builds ready [2c3e0d8]
UI Startup Metrics (1250 ± 75 ms)
PlatformBuildTypePageMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P 75 (ms)P 95 (ms)
ChromeBrowserifyHomeuiStartup1250110115367512911388
load108095413247011141206
domContentLoaded107294913187011061189
domInteractive1913114131731
firstPaint76378122342811011195
backgroundConnect94285922
firstReactRender21173642228
getState1465481931
initialActions001000
loadScripts823704106970863939
setupStore85152912
WebpackHomeuiStartup21251586249122422902453
load16511237197917517711886
domContentLoaded16441233197417317621873
domInteractive14124061336
firstPaint1736436062210291
backgroundConnect239105142450
firstReactRender13743359103145341
getState195334451248
initialActions317134
loadScripts16411231197217217611862
setupStore2863115917174
FirefoxBrowserifyHomeuiStartup13721202173711114251678
load1212106515559712561409
domContentLoaded1212106415559712561408
domInteractive1003529232114147
firstPaintNaNNaNNaNNaNNaNNaN
backgroundConnect21136782141
firstReactRender24205142429
getState10418318818
initialActions006101
loadScripts1195104715389712381390
setupStore74749610
WebpackHomeuiStartup15931386223017616942073
load13851192201816514731837
domContentLoaded13841192201716514721836
domInteractive76331821983118
firstPaintNaNNaNNaNNaNNaNNaN
backgroundConnect22168382334
firstReactRender42315544550
getState105316928
initialActions002111
loadScripts13651173199516514561816
setupStore10522722811
Benchmark value 1250 exceeds gate value 1234 for chrome browserify home mean uiStartup
Benchmark value 1080 exceeds gate value 1070 for chrome browserify home mean load
Benchmark value 1073 exceeds gate value 1061 for chrome browserify home mean domContentLoaded
Benchmark value 1389 exceeds gate value 1365 for chrome browserify home p95 uiStartup
Benchmark value 1207 exceeds gate value 1190 for chrome browserify home p95 load
Benchmark value 1190 exceeds gate value 1180 for chrome browserify home p95 domContentLoaded
Benchmark value 1196 exceeds gate value 1180 for chrome browserify home p95 firstPaint
Benchmark value 23 exceeds gate value 18 for chrome browserify home p95 backgroundConnect
Benchmark value 174 exceeds gate value 65 for chrome webpack home p95 setupStore
Benchmark value 1678 exceeds gate value 1660 for firefox browserify home p95 uiStartup
Benchmark value 1385 exceeds gate value 1380 for firefox webpack home mean load
Benchmark value 1385 exceeds gate value 1380 for firefox webpack home mean domContentLoaded
Benchmark value 43 exceeds gate value 38 for firefox webpack home mean firstReactRender
Benchmark value 1366 exceeds gate value 1360 for firefox webpack home mean loadScripts
Benchmark value 2073 exceeds gate value 1935 for firefox webpack home p95 uiStartup
Benchmark value 1837 exceeds gate value 1660 for firefox webpack home p95 load
Benchmark value 1836 exceeds gate value 1660 for firefox webpack home p95 domContentLoaded
Benchmark value 1816 exceeds gate value 1630 for firefox webpack home p95 loadScripts
Sum of mean exceeds: 59ms | Sum of p95 exceeds: 876ms
Sum of all benchmark exceeds: 935ms

Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 326 Bytes (0.01%)
  • ui: 0 Bytes (0%)
  • common: 0 Bytes (0%)

@salimtb salimtb added this pull request to the merge queue Jun 5, 2025
Merged via the queue into main with commit 6191fa9 Jun 5, 2025
145 checks passed
@salimtb salimtb deleted the fix/fix-premature-close-error branch June 5, 2025 13:29
@github-actions github-actions bot locked and limited conversation to collaborators Jun 5, 2025
@metamaskbot metamaskbot added the release-12.21.0 Issue or pull request that will be included in release 12.21.0 label Jun 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-12.21.0 Issue or pull request that will be included in release 12.21.0 team-assets

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Sentry] Error: Premature close

5 participants