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 race condition where some token detections can get mistakenly added to the wrong account #956

Merged
merged 6 commits into from
Nov 22, 2022

Conversation

adonesky1
Copy link
Contributor

@adonesky1 adonesky1 commented Nov 14, 2022

Currently there exists an issue (exposed by this bug) where if a user changes accounts (account x to account y) or (changes from network a to network b) right as a detectTokens() call is occuring, the results of the getBalancesInSingleCall() call (withindetectTokens()) made against address x or network acan get incorrectly misattributed to accountyornetwork b. This can happen because currently detectTokens() simply calls this.addDetectedTokens() on the TokensController with the detected tokensToAdd assuming that when this call occurs in the TokensController the selectedAddress or chainId used to store these detected assets will be the same as the one used to detect them. With sufficiently unfortunate timing this expectation can be broken, and so this PR introduces a pattern (already used in the analogous flow between the NftDetectionController and the NftController, whereby the address and network data are passed along with the detectedToken data to ensure they are stored correctly.

Partially fixes: MetaMask/metamask-extension#16349

  • FIXED:
    • Fixes a possible race condition whereby detected tokens can be incorrectly stored as detected on the wrong account/network.

Checklist

  • Tests are included if applicable
  • Any added code is fully documented

@adonesky1
Copy link
Contributor Author

adonesky1 commented Nov 14, 2022

Added DO-NOT-MERGE lable until freeze on @metamask/controllers PRs is lifted

NiranjanaBinoy
NiranjanaBinoy previously approved these changes Nov 14, 2022
Copy link
Contributor

@NiranjanaBinoy NiranjanaBinoy left a comment

Choose a reason for hiding this comment

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

LGTM

darkwing
darkwing previously approved these changes Nov 15, 2022
Copy link
Contributor

@darkwing darkwing left a comment

Choose a reason for hiding this comment

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

LGTM. Should we add a test where we ensure the token is added to the correct spot based on chainID?

@adonesky1 adonesky1 marked this pull request as draft November 15, 2022 23:52
@adonesky1
Copy link
Contributor Author

adonesky1 commented Nov 15, 2022

Moved this back to WIP, I found an issue resolved

@adonesky1 adonesky1 force-pushed the fix-misattributed-token-detection branch from dadcc12 to d9347f4 Compare November 16, 2022 00:05
@adonesky1 adonesky1 force-pushed the fix-misattributed-token-detection branch from d9347f4 to a471da5 Compare November 16, 2022 00:05
@adonesky1 adonesky1 marked this pull request as ready for review November 16, 2022 00:08
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

@Gudahtt
Copy link
Member

Gudahtt commented Nov 22, 2022

The code freeze is over, so the DO-NOT-MERGE label has been removed

@adonesky1 adonesky1 merged commit 3454eeb into main Nov 22, 2022
@adonesky1 adonesky1 deleted the fix-misattributed-token-detection branch November 22, 2022 17:09
gantunesr pushed a commit that referenced this pull request Dec 8, 2022
…ed to the wrong account (#956)

Fix race condition causing misattribution for token-detection across networks and accounts
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
…ed to the wrong account (#956)

Fix race condition causing misattribution for token-detection across networks and accounts
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
…ed to the wrong account (#956)

Fix race condition causing misattribution for token-detection across networks and accounts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: [MV3] Token detection is caching results between accounts when terminating service worker
6 participants