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 console errors upon switching networks #7278

Merged
merged 1 commit into from
Sep 20, 2023

Conversation

Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Sep 20, 2023

Description

On main two warnings were printed to the console repeatedly after switching networks. They were:

  • Error: Callback was already called. Possible Unhandled Promise Rejection
  • MaxListenersExceededWarning: Possible EventEmitter memory leak detected ...

This started happening after PR #6872. If you look at network traffic, you could see that this lines up with extra network calls being made to the previous network as well (the eth_getBlockByNumber method specifically).

These warnings were caused by a change made in
@metamask/network-controller@6.0.0, which was to proxy the provider and block tracker. This introduced an incompatibility between the NetworkController and web3-provider-engine, which was still using the unproxied block tracker. web3-provider-engine adds an event listener on the latest event from the block tracker, which calls eth_getBlockByNumber with a retry. This listener was migrated to the new network after switching, but would make that RPC call on the old provider.

The user effects of this are an increase in unnecessary network traffic, but is otherwise harmless.

The web3-provider-engine package has been patched to make the block tracker event listener into a no-op after the provider has been stopped. This isn't an ideal solution as it still leaves the listener attached, which is a small memory leak. But it resolves the warnings and prevents the unnecessary network calls. We will be updating to @metamask/network-controller v9 soon which will resolve this problem permanently by removing web3-provider-engine.

Manual testing steps

  • Switch networks
  • See that the listed warnings are no longer printed to the console (the yarn watch console output)

Screenshots/Recordings

Before

https://recordit.co/SXZf4Vhnbz

After

https://recordit.co/5CIOzb4srH

Related issues

Fixes #7082

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've clearly explained:
    • What problem this PR is solving.
    • How this problem was solved.
    • How reviewers can test my changes.
  • I’ve indicated what issue this PR is linked to: Fixes #???
  • I’ve included tests if applicable.
  • I’ve documented any added code.
  • I’ve applied the right labels on the PR (see labeling guidelines).
  • 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.

@Gudahtt Gudahtt force-pushed the fix-unhandled-promise-rejection-errors branch from 86670ea to 6f35519 Compare September 20, 2023 17:24
On `main` two warnings were printed to the console repeatedly after
switching networks. They were:

* Error: Callback was already called. Possible Unhandled Promise Rejection
* MaxListenersExceededWarning: Possible EventEmitter memory leak detected ...

This started happening after PR #6872. If you look at network traffic,
you could see that this lines up with extra network calls being made to
the previous network as well (the `eth_getBlockByNumber` method
specifically).

These warnings were caused by a change made in
`@metamask/network-controller@6.0.0`, which was to proxy the provider
and block tracker. This introduced an incompatibility between the
`NetworkController` and `web3-provider-engine`, which was still using
the unproxied block tracker. `web3-provider-engine` adds an event
listener on the `latest` event from the block tracker, which calls
`eth_getBlockByNumber` with a retry. This listener was migrated to the
new network after switching, but would make that RPC call on the old
provider.

The user effects of this are an increase in unnecessary network
traffic, but is otherwise harmless.

The `web3-provider-engine` package has been patched to make the block
tracker event listener into a no-op after the provider has been
stopped. This isn't an ideal solution as it still leaves the listener
attached, which is a small memory leak. But it resolves the warnings
and prevents the unnecessary network calls. We will be updating to
`@metamask/network-controller` v9 soon which will resolve this problem
permanently by removing `web3-provider-engine`.

Fixes #7082
@Gudahtt Gudahtt force-pushed the fix-unhandled-promise-rejection-errors branch from 6f35519 to 6ca5cda Compare September 20, 2023 17:25
@Gudahtt Gudahtt marked this pull request as ready for review September 20, 2023 17:25
@Gudahtt Gudahtt requested a review from a team as a code owner September 20, 2023 17:25
@Gudahtt
Copy link
Member Author

Gudahtt commented Sep 20, 2023

@Gudahtt Gudahtt added needs-dev-review PR needs reviews from other engineers (in order to receive required approvals) team-wallet-framework team-mobile-platform labels Sep 20, 2023
@codecov-commenter
Copy link

codecov-commenter commented Sep 20, 2023

Codecov Report

Patch and project coverage have no change.

Comparison is base (5c7e4ea) 34.43% compared to head (6ca5cda) 34.43%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7278   +/-   ##
=======================================
  Coverage   34.43%   34.43%           
=======================================
  Files        1016     1016           
  Lines       27087    27087           
  Branches     2206     2206           
=======================================
  Hits         9327     9327           
  Misses      17267    17267           
  Partials      493      493           

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

@sonarcloud
Copy link

sonarcloud bot commented Sep 20, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Copy link
Contributor

@Cal-L Cal-L left a comment

Choose a reason for hiding this comment

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

LGTM, just verified that there are no more warnings

@Gudahtt Gudahtt added Spot Check on the Release Build If a ticket doesn't require feature QA, but does require some form of manual spot checking and removed needs-dev-review PR needs reviews from other engineers (in order to receive required approvals) labels Sep 20, 2023
@Gudahtt Gudahtt merged commit 9dd76db into main Sep 20, 2023
30 checks passed
@Gudahtt Gudahtt deleted the fix-unhandled-promise-rejection-errors branch September 20, 2023 22:12
@github-actions github-actions bot locked and limited conversation to collaborators Sep 20, 2023
@Cal-L Cal-L removed Spot Check on the Release Build If a ticket doesn't require feature QA, but does require some form of manual spot checking release-7.9.0 labels Sep 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants