Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix gas fee polling not being stopped for popup UI instances in chrome #23594

Merged
merged 10 commits into from
Mar 26, 2024

Conversation

jiexi
Copy link
Contributor

@jiexi jiexi commented Mar 20, 2024

Description

Currently extension popups in chrome do not properly clean up react effects. Previously, gas fee polling started from UI components were also tracked by MetaMaskController and cleaned up when the controller lost connection with the popup (or fullscreen, or notification) UI instances. The new gas fee polling hook failed to account for this and did not properly track its polling tokens with background which introduced the possibility of gas fee polling not stopping correctly in certain instances. This PR fixes this by updating the usePolling hook to add and remove poll tokens from app state and also updates MetaMaskController to stop any active gas fee polling based on the polling tokens in app state.

Open in GitHub Codespaces

Related issues

Fixes: https://github.com/MetaMask/MetaMask-planning/issues/2014

Manual testing steps

In chrome

  1. Open a popup instance of extension (click the extension icon from the toolbar)
  2. Open background console network tab
  3. Get to the send transaction confirmation screen
  4. Verify gas fee requests are happening
  5. Close the popup instance by unfocusing it
  6. Verify gas fee requests have stopped

Screenshots/Recordings

Before

After

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've clearly explained what problem this PR is solving and how it is solved.
  • I've linked related issues
  • I've included manual testing steps
  • I've included screenshots/recordings if applicable
  • 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.
  • I’ve properly set the pull request status:
    • In case it's not yet "ready for review", I've set it to "draft".
    • In case it's "ready for review", I've changed it from "draft" to "non-draft".

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.

Copy link
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.

@jiexi jiexi marked this pull request as ready for review March 20, 2024 00:16
@jiexi jiexi requested a review from a team as a code owner March 20, 2024 00:16
@jiexi jiexi requested a review from shanejonas March 20, 2024 00:17
@@ -20,6 +24,7 @@ const usePolling = (usePollingOptions: UsePollingOptions) => {

const cleanup = () => {
if (pollTokenRef.current) {
removePollingTokenFromAppState(pollTokenRef.current);
Copy link
Member

Choose a reason for hiding this comment

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

Do we ever want to stop tracking a token without stopping polling? Or the other way around - stop polling without removing a token from the app state? This seems like it should be one single function, rather than two.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh i see. Yeah they should be coupled in another function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done here: 3b4fb8f

Copy link
Contributor Author

Choose a reason for hiding this comment

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

apologies. done here instead 029f53e

made more sense to couple in the actions

@@ -5687,6 +5688,7 @@ export default class MetamaskController extends EventEmitter {
this.appStateController.store.getState()[appStatePollingTokenType];
pollingTokensToDisconnect.forEach((pollingToken) => {
this.gasFeeController.disconnectPoller(pollingToken);
this.gasFeeController.stopPollingByPollingToken(pollingToken);
Copy link
Member

Choose a reason for hiding this comment

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

It looks like this is the new polling API, whereas disconnectPoller is the old API. Is that correct? Are we using both of these simultaneously?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there are a few references to the old API still

Copy link
Contributor

Choose a reason for hiding this comment

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

@jiexi can you remind me where these references are? These use the same polling tokens?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the poll token from the legacy polling and the networkClientId based polling are not the same

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok and so its just a noop when we pass one for the new polling to the old and vice versa?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

correct. calling disconnectPoller and stopPollingByPollingToken with polling tokens it cannot find is a noop without errors thrown

@jiexi jiexi force-pushed the jl/fix-gas-fee-polling-hook-background-connection branch from 602758f to 3b4fb8f Compare March 22, 2024 21:49
@jiexi jiexi requested review from a team and kumavis as code owners March 22, 2024 21:49
@jiexi jiexi removed request for a team and kumavis March 22, 2024 22:11
Copy link

codecov bot commented Mar 25, 2024

Codecov Report

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

Project coverage is 68.79%. Comparing base (b3d93fa) to head (0570881).

Files Patch % Lines
ui/store/actions.ts 28.57% 5 Missing ⚠️
app/scripts/metamask-controller.js 0.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #23594      +/-   ##
===========================================
- Coverage    68.82%   68.79%   -0.03%     
===========================================
  Files         1170     1170              
  Lines        44391    44406      +15     
  Branches     11879    11881       +2     
===========================================
- Hits         30549    30547       -2     
- Misses       13842    13859      +17     

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

@metamaskbot
Copy link
Collaborator

Builds ready [0570881]
Page Load Metrics (1420 ± 629 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint743011465828
domContentLoaded14151362914
load58312914201309629
domInteractive14151362914
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 90 Bytes (0.00%)
  • ui: 61 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

Copy link
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.

LGTM!

@jiexi jiexi merged commit 3523565 into develop Mar 26, 2024
66 checks passed
@jiexi jiexi deleted the jl/fix-gas-fee-polling-hook-background-connection branch March 26, 2024 17:48
@github-actions github-actions bot locked and limited conversation to collaborators Mar 26, 2024
@metamaskbot metamaskbot added the release-11.15.0 Issue or pull request that will be included in release 11.15.0 label Mar 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
release-11.15.0 Issue or pull request that will be included in release 11.15.0 team-wallet-api-platform
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants