Skip to content

fix: Retry requests from provider after connection is ready, on all builds#24142

Merged
jiexi merged 25 commits intodevelopfrom
jl/retry-requests-provider-all-builds
Jun 12, 2024
Merged

fix: Retry requests from provider after connection is ready, on all builds#24142
jiexi merged 25 commits intodevelopfrom
jl/retry-requests-provider-all-builds

Conversation

@jiexi
Copy link
Copy Markdown
Member

@jiexi jiexi commented Apr 19, 2024

Description

Currently, requests from the dapp can hang if:

  1. they are sent before the extension is fully initialized / before the extension is ready to process requests from that specific dapp connection
  2. they get swallowed do to an issue in chromium with streams on prerendered pages breaking when they become visible to the user

The inpage provider does have a retry mechanism, but currently that only works on MV3 builds. Additionally, is not a 100% reliable trigger as the underlying metamask_chainChanged message that it relies on is tied to MetaMaskController mem state changes, and is not fired off each time a dapp connection is established.

This PR attempts to fix request retries by:

  • Making the retry message triggered by metamask_chainChanged work on all builds, not just MV3 builds
  • Firing metamask_chainChanged every time a new dapp connection is established

Open concern:
There may be an edge case when a request with a confirmation is sent by the dapp, then received and shown to the user in the wallet, but swallowed by prerender. I believe my proposed changes in this PR would cause the confirmation to get resent even though there may still be a confirmation being shown to the user. I haven't been able to test this properly as debugging the prerender issue in general is difficult.

This would require a dapp to send a request requiring confirmation pretty much immediately after page load, implying without user interaction.

Open in GitHub Codespaces

Related issues

See: #11069
See: #23329
See: #24025

Manual testing steps

NOTE: these testing steps require that you add an artificial 5s delay to the background connection handling. See the comments below to find the commit where this delay was included in this PR before being removed for merging

Verify this fixes the metamask_sendDomainMetadata issue

  1. Restart extension
  2. Open any page
  3. Quickly open the wallet popup
  4. Notice that there is no favicon
  5. After 5s have elapsed since the page was opened, the favicon should populate in the wallet popup

Verify this fixes retrying messages before extension is ready

  1. Open any page (restarting extension not required)
  2. Quickly open console and type await window.ethereum.request({method: 'eth_chainId'})
  3. Notice that the response doesn't come immediately
  4. After 5s have elapsed since the page was opened, the response should be shown in console

Verify this fixes retrying messages after broken prerender stream

Not easy to verify this unfortunately, you would need to break the prerender stream while a request was being made

Screenshots/Recordings

Before

After

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • 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 format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). 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.

@github-actions
Copy link
Copy Markdown
Contributor

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.

@sentry
Copy link
Copy Markdown

sentry bot commented Apr 19, 2024

🔍 Existing Issues For Review

Your pull request is modifying functions with the following pre-existing issues:

📄 File: app/scripts/background.js

Function Unhandled Issue
setupController TypeError: Cannot read properties of undefined (reading 'getCurrentBlock') new exports.default(app/scrip...
Event Count: 13 Affected Users: 0
setupController new MetamaskController(app/scri...
Event Count: 4 Affected Users: 0

Did you find this useful? React with a 👍 or 👎

Comment on lines +710 to +712
console.log(`delaying connection to ${remotePort.sender.url} for 5s`);
await new Promise((resolve) => setTimeout(resolve, 5000));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is here to make testing easier. It delays the handling of the dapp responses over the port for 5s.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Remove this before approval, of course

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been removed, but must be added back locally if you are testing manually

@jiexi jiexi added team-extension-platform Extension Platform team team-wallet-api-platform-deprecated DEPRECATED: please use "team-wallet-integrations" instead labels Apr 19, 2024
@jiexi jiexi changed the title WIP: Retry requests on provider for all builds fix: Retry requests from provider after connection is ready, on all builds Apr 19, 2024
@jiexi jiexi marked this pull request as ready for review April 19, 2024 22:03
@jiexi jiexi requested a review from a team as a code owner April 19, 2024 22:03
jpuri
jpuri previously approved these changes Apr 25, 2024
@jiexi jiexi requested a review from Gudahtt May 29, 2024 21:17
@jiexi jiexi requested review from adonesky1 and shanejonas May 29, 2024 21:17
@jiexi
Copy link
Copy Markdown
Member Author

jiexi commented May 30, 2024

Following change in web3-stream-provider needed MetaMask/web3-stream-provider#16

@jiexi jiexi force-pushed the jl/retry-requests-provider-all-builds branch from b503c8d to 7ae80ba Compare May 30, 2024 22:17
@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [9e29f0e]
Page Load Metrics (2874 ± 885 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint1354752208340
domContentLoaded18273616531
load115506428741843885
domInteractive18273616531
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 477 Bytes (0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@codecov
Copy link
Copy Markdown

codecov bot commented May 30, 2024

Codecov Report

Attention: Patch coverage is 76.92308% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 65.69%. Comparing base (0b0bf11) to head (6a8083b).

Files Patch % Lines
app/scripts/metamask-controller.js 76.92% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #24142      +/-   ##
===========================================
- Coverage    65.70%   65.69%   -0.01%     
===========================================
  Files         1366     1366              
  Lines        54361    54391      +30     
  Branches     14141    14144       +3     
===========================================
+ Hits         35716    35728      +12     
- Misses       18645    18663      +18     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [6513795]
Page Load Metrics (206 ± 223 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint7110986116
domContentLoaded9271242
load421609206464223
domInteractive9271242

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [6921a09]
Page Load Metrics (51 ± 4 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint69988284
domContentLoaded8221031
load42665184
domInteractive8221031

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [6a8083b]
Page Load Metrics (52 ± 3 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint7011086105
domContentLoaded9251242
load43735273
domInteractive9251242

@adonesky1
Copy link
Copy Markdown
Contributor

I'm not sure what this is about but I'm seeing some strange inexplicable inconsistency with favicons:
https://github.com/MetaMask/metamask-extension/assets/34557516/87cf7091-fd41-4c42-ab8c-d1c9082a0a6e

Copy link
Copy Markdown
Contributor

@adonesky1 adonesky1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing the issue with the favicon anymore. I've tested all scenarios (with and without the added delay) and they all look good. Code looks good to me!

Copy link
Copy Markdown
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@jiexi jiexi merged commit dc5247f into develop Jun 12, 2024
@jiexi jiexi deleted the jl/retry-requests-provider-all-builds branch June 12, 2024 17:28
@github-actions github-actions bot locked and limited conversation to collaborators Jun 12, 2024
@metamaskbot metamaskbot added the release-12.1.0 Issue or pull request that will be included in release 12.1.0 label Jun 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-12.1.0 Issue or pull request that will be included in release 12.1.0 team-extension-platform Extension Platform team team-wallet-api-platform-deprecated DEPRECATED: please use "team-wallet-integrations" instead

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants