fix(assets-controller): increase stale and gc time for accounts api request - #9591
Merged
Conversation
Kriys94
force-pushed
the
fix/AccountsAPIcache
branch
from
July 22, 2026 09:31
2b040cb to
2202087
Compare
Kriys94
marked this pull request as ready for review
July 22, 2026 09:33
Kriys94
force-pushed
the
fix/AccountsAPIcache
branch
3 times, most recently
from
July 22, 2026 09:59
0ed5ee7 to
e0e7950
Compare
Contributor
|
@metamaskbot publish-preview |
bergarces
previously approved these changes
Jul 23, 2026
Contributor
|
This is already the case for the v6 endpoint right? I have noticed that subsequent calls to it won't trigger a network request. |
salimtb
previously approved these changes
Jul 23, 2026
Contributor
|
i just tested this on the client and works as expected |
Contributor
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
Contributor
Author
Yeah that was the case for v6, event for v5. Actually, since we put |
Kriys94
force-pushed
the
fix/AccountsAPIcache
branch
from
July 23, 2026 08:45
e0e7950 to
1659296
Compare
salimtb
approved these changes
Jul 23, 2026
4 tasks
Merged
4 tasks
pull Bot
pushed a commit
to dmrazzy/core
that referenced
this pull request
Aug 14, 2026
…ts api request (MetaMask#9591) (MetaMask#9870) This reverts commit 1b082f7. Extension PR: MetaMask/metamask-extension#45494 Mobile PR: MetaMask/metamask-mobile#34748 ## Explanation <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## Checklist - [ ] I've updated the test suite for new or updated code as appropriate - [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/processes/updating-changelogs.md) - [ ] I've introduced [breaking changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md) in this PR and have prepared draft pull requests for clients and consumer packages to resolve them <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Touches balance refresh behavior on user-visible paths (force refresh); tradeoff is fresher balances vs slightly more Accounts API traffic, with low security impact. > > **Overview** > Reverts [MetaMask#9591](MetaMask#9591): when `AccountsApiDataSource` handles a balance fetch with **`forceUpdate: true`**, TanStack Query options go back to **`staleTime: 0` and `gcTime: 0`** instead of a 100ms window. > > Forced refreshes (e.g. after unlock, account/network changes, or explicit refresh) are meant to hit the Accounts API with a fresh request; the short cache window could reuse a just-fetched entry and leave balances stale. The unit test is renamed/updated to assert cache bypass on `forceUpdate`. > > The **Unreleased** changelog documents this revert; it also consolidates the Arc native USDC default-tracking note under Unreleased and drops a duplicate entry from the 13.1.3 section. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit ed2395f. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Salim TOUBAL <salimtb@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Explanation
Current behavior:

After PR:

Balance refreshes triggered on-demand go through
AssetsController.getAssets(..., { forceUpdate: true }), which flows down toAccountsApiDataSource.fetchas TanStack QueryfetchOptions. Previously theforceUpdatepath used{ staleTime: 0, gcTime: 0 }. These values has been added via #9265 but it was mainly to fix a websocket issue.That was too aggressive:
staleTime: 0marks the query as stale immediately, so every forced refresh refetches — even two identical requests fired microseconds apart.gcTime: 0evicts the cache entry the instant the query goes inactive (which, for imperativefetchQuery, is right after it resolves). Because the forced fetch uses the same queryKey as the 30s polling subscription, it not only bypasses the cache but also tears down the entry the poll would have reused.In practice almost every internal trigger calls
getAssetswithforceUpdate: true(unlock, account switch, network change, tx confirmation, price refresh, etc.), and several of these fire near-simultaneously. With0/0each trigger produced its own Accounts API request, causing bursts of duplicatemultiaccount/balancescalls.This PR changes the
forceUpdatepath to{ staleTime: 200, gcTime: 200 }. The small 200ms window lets a burst of near-simultaneous forced refreshes de-duplicate into a single Accounts API request while keeping the data effectively fresh (well within a user-perceptible refresh). KeepinggcTime >= staleTimealso ensures the entry survives its freshness window instead of being evicted immediately.References
N/A
Checklist
Note
Low Risk
Narrow change to query cache options for forced balance fetches; balances may share a single response within ~100ms, which is an intentional tradeoff for fewer API calls.
Overview
Forced balance refreshes (
getAssetswithforceUpdate: true→AccountsApiDataSource.fetch) no longer pass TanStack Query{ staleTime: 0, gcTime: 0 }. They now usestaleTimeandgcTimeof 100ms, so near-simultaneous triggers (unlock, account/network switch, tx confirmation, etc.) collapse to onemultiaccount/balancesrequest instead of a burst of duplicates, while still avoiding the default long-lived cache.The unit test and changelog are updated to match the new short-lived cache window (replacing the “fully bypass cache” expectation from #9265).
Reviewed by Cursor Bugbot for commit 1659296. Bugbot is set up for automated code reviews on this repo. Configure here.