Skip to content

fix(assets-controller): increase stale and gc time for accounts api request - #9591

Merged
Kriys94 merged 2 commits into
mainfrom
fix/AccountsAPIcache
Jul 23, 2026
Merged

fix(assets-controller): increase stale and gc time for accounts api request#9591
Kriys94 merged 2 commits into
mainfrom
fix/AccountsAPIcache

Conversation

@Kriys94

@Kriys94 Kriys94 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Explanation

Current behavior:
image

After PR:
image

Balance refreshes triggered on-demand go through AssetsController.getAssets(..., { forceUpdate: true }), which flows down to AccountsApiDataSource.fetch as TanStack Query fetchOptions. Previously the forceUpdate path 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: 0 marks the query as stale immediately, so every forced refresh refetches — even two identical requests fired microseconds apart.
  • gcTime: 0 evicts the cache entry the instant the query goes inactive (which, for imperative fetchQuery, 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 getAssets with forceUpdate: true (unlock, account switch, network change, tx confirmation, price refresh, etc.), and several of these fire near-simultaneously. With 0/0 each trigger produced its own Accounts API request, causing bursts of duplicate multiaccount/balances calls.

This PR changes the forceUpdate path 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). Keeping gcTime >= staleTime also ensures the entry survives its freshness window instead of being evicted immediately.

References

N/A

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
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

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 (getAssets with forceUpdate: trueAccountsApiDataSource.fetch) no longer pass TanStack Query { staleTime: 0, gcTime: 0 }. They now use staleTime and gcTime of 100ms, so near-simultaneous triggers (unlock, account/network switch, tx confirmation, etc.) collapse to one multiaccount/balances request 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.

@Kriys94
Kriys94 force-pushed the fix/AccountsAPIcache branch from 2b040cb to 2202087 Compare July 22, 2026 09:31
@Kriys94
Kriys94 marked this pull request as ready for review July 22, 2026 09:33
@Kriys94
Kriys94 requested review from a team as code owners July 22, 2026 09:33
@Kriys94
Kriys94 temporarily deployed to default-branch July 22, 2026 09:33 — with Image GitHub Actions Inactive
@Kriys94 Kriys94 changed the title fix(assets-controller): increase stale and g time for accounts api request fix(assets-controller): increase stale and gc time for accounts api request Jul 22, 2026
@Kriys94
Kriys94 force-pushed the fix/AccountsAPIcache branch 3 times, most recently from 0ed5ee7 to e0e7950 Compare July 22, 2026 09:59
@salimtb

salimtb commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

@metamaskbot publish-preview

bergarces
bergarces previously approved these changes Jul 23, 2026
@bergarces

Copy link
Copy Markdown
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
salimtb previously approved these changes Jul 23, 2026
@salimtb

salimtb commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

i just tested this on the client and works as expected

@github-actions

Copy link
Copy Markdown
Contributor

Preview builds have been published. Learn how to use preview builds in other projects.

Expand for full list of packages and versions.
@metamask-previews/account-tree-controller@7.5.5-preview-e0e795054
@metamask-previews/accounts-controller@39.0.5-preview-e0e795054
@metamask-previews/address-book-controller@7.1.2-preview-e0e795054
@metamask-previews/ai-controllers@0.8.0-preview-e0e795054
@metamask-previews/analytics-controller@1.2.1-preview-e0e795054
@metamask-previews/analytics-data-regulation-controller@0.0.0-preview-e0e795054
@metamask-previews/announcement-controller@8.1.0-preview-e0e795054
@metamask-previews/app-metadata-controller@2.0.1-preview-e0e795054
@metamask-previews/approval-controller@9.0.2-preview-e0e795054
@metamask-previews/assets-controller@11.1.0-preview-e0e795054
@metamask-previews/assets-controllers@109.4.1-preview-e0e795054
@metamask-previews/authenticated-user-storage@3.0.1-preview-e0e795054
@metamask-previews/base-controller@9.1.0-preview-e0e795054
@metamask-previews/base-data-service@0.1.3-preview-e0e795054
@metamask-previews/bitcoin-regtest-up@1.0.0-preview-e0e795054
@metamask-previews/bridge-controller@77.6.0-preview-e0e795054
@metamask-previews/bridge-status-controller@74.3.0-preview-e0e795054
@metamask-previews/build-utils@3.0.4-preview-e0e795054
@metamask-previews/chain-agnostic-permission@1.7.0-preview-e0e795054
@metamask-previews/chomp-api-service@3.1.0-preview-e0e795054
@metamask-previews/claims-controller@0.5.3-preview-e0e795054
@metamask-previews/client-controller@1.0.1-preview-e0e795054
@metamask-previews/client-utils@1.2.0-preview-e0e795054
@metamask-previews/compliance-controller@2.1.0-preview-e0e795054
@metamask-previews/composable-controller@12.0.1-preview-e0e795054
@metamask-previews/config-registry-controller@0.4.1-preview-e0e795054
@metamask-previews/connectivity-controller@0.3.0-preview-e0e795054
@metamask-previews/controller-utils@12.3.0-preview-e0e795054
@metamask-previews/core-backend@6.5.0-preview-e0e795054
@metamask-previews/delegation-controller@3.0.2-preview-e0e795054
@metamask-previews/earn-controller@12.2.2-preview-e0e795054
@metamask-previews/eip-5792-middleware@3.0.5-preview-e0e795054
@metamask-previews/eip-7702-internal-rpc-middleware@0.1.1-preview-e0e795054
@metamask-previews/eip1193-permission-middleware@2.0.1-preview-e0e795054
@metamask-previews/ens-controller@19.1.5-preview-e0e795054
@metamask-previews/eth-block-tracker@15.0.1-preview-e0e795054
@metamask-previews/eth-json-rpc-middleware@23.1.3-preview-e0e795054
@metamask-previews/eth-json-rpc-provider@6.0.1-preview-e0e795054
@metamask-previews/foundryup@1.0.1-preview-e0e795054
@metamask-previews/gas-fee-controller@26.2.4-preview-e0e795054
@metamask-previews/gator-permissions-controller@4.2.3-preview-e0e795054
@metamask-previews/geolocation-controller@0.1.3-preview-e0e795054
@metamask-previews/java-tron-up@1.0.0-preview-e0e795054
@metamask-previews/json-rpc-engine@10.5.0-preview-e0e795054
@metamask-previews/json-rpc-middleware-stream@8.0.8-preview-e0e795054
@metamask-previews/keyring-controller@27.1.0-preview-e0e795054
@metamask-previews/local-node-utils@1.0.0-preview-e0e795054
@metamask-previews/logging-controller@8.0.2-preview-e0e795054
@metamask-previews/message-manager@14.1.2-preview-e0e795054
@metamask-previews/messenger@2.0.0-preview-e0e795054
@metamask-previews/messenger-cli@0.2.0-preview-e0e795054
@metamask-previews/money-account-api-data-service@0.2.0-preview-e0e795054
@metamask-previews/money-account-balance-service@2.3.0-preview-e0e795054
@metamask-previews/money-account-controller@0.3.3-preview-e0e795054
@metamask-previews/money-account-upgrade-controller@2.2.1-preview-e0e795054
@metamask-previews/multichain-account-service@13.0.0-preview-e0e795054
@metamask-previews/multichain-api-middleware@4.0.1-preview-e0e795054
@metamask-previews/multichain-network-controller@3.2.1-preview-e0e795054
@metamask-previews/multichain-transactions-controller@7.1.1-preview-e0e795054
@metamask-previews/name-controller@9.1.2-preview-e0e795054
@metamask-previews/network-connection-banner-controller@0.1.0-preview-e0e795054
@metamask-previews/network-controller@34.0.0-preview-e0e795054
@metamask-previews/network-enablement-controller@5.6.0-preview-e0e795054
@metamask-previews/notification-services-controller@26.0.0-preview-e0e795054
@metamask-previews/passkey-controller@3.0.0-preview-e0e795054
@metamask-previews/permission-controller@13.1.1-preview-e0e795054
@metamask-previews/permission-log-controller@5.1.0-preview-e0e795054
@metamask-previews/perps-controller@9.3.0-preview-e0e795054
@metamask-previews/phishing-controller@17.3.0-preview-e0e795054
@metamask-previews/platform-api-docs@0.0.0-preview-e0e795054
@metamask-previews/polling-controller@16.0.8-preview-e0e795054
@metamask-previews/preferences-controller@23.1.0-preview-e0e795054
@metamask-previews/profile-metrics-controller@4.0.2-preview-e0e795054
@metamask-previews/profile-sync-controller@28.3.0-preview-e0e795054
@metamask-previews/ramps-controller@17.0.0-preview-e0e795054
@metamask-previews/rate-limit-controller@7.0.1-preview-e0e795054
@metamask-previews/react-data-query@0.2.2-preview-e0e795054
@metamask-previews/remote-feature-flag-controller@4.2.2-preview-e0e795054
@metamask-previews/sample-controllers@5.0.3-preview-e0e795054
@metamask-previews/seedless-onboarding-controller@10.0.3-preview-e0e795054
@metamask-previews/selected-network-controller@26.1.5-preview-e0e795054
@metamask-previews/sentinel-api-service@1.0.0-preview-e0e795054
@metamask-previews/shield-controller@5.1.3-preview-e0e795054
@metamask-previews/signature-controller@39.2.7-preview-e0e795054
@metamask-previews/smart-transactions-controller@25.0.1-preview-e0e795054
@metamask-previews/snap-account-service@2.0.0-preview-e0e795054
@metamask-previews/social-controllers@2.5.0-preview-e0e795054
@metamask-previews/solana-test-validator-up@1.0.0-preview-e0e795054
@metamask-previews/stellar-quickstart-up@0.0.0-preview-e0e795054
@metamask-previews/storage-service@1.0.2-preview-e0e795054
@metamask-previews/subscription-controller@6.2.1-preview-e0e795054
@metamask-previews/transaction-controller@69.2.0-preview-e0e795054
@metamask-previews/transaction-pay-controller@25.1.0-preview-e0e795054
@metamask-previews/user-operation-controller@41.2.7-preview-e0e795054
@metamask-previews/wallet@7.0.1-preview-e0e795054
@metamask-previews/wallet-cli@0.0.0-preview-e0e795054

@Kriys94

Kriys94 commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

This is already the case for the v6 endpoint right? I have noticed that subsequent calls to it won't trigger a network request.

Yeah that was the case for v6, event for v5. Actually, since we put { staleTime: 0, gcTime: 0 }

@Kriys94
Kriys94 dismissed stale reviews from salimtb and bergarces via 1659296 July 23, 2026 08:45
@Kriys94
Kriys94 force-pushed the fix/AccountsAPIcache branch from e0e7950 to 1659296 Compare July 23, 2026 08:45
@Kriys94
Kriys94 added this pull request to the merge queue Jul 23, 2026
Merged via the queue into main with commit 1b082f7 Jul 23, 2026
427 checks passed
@Kriys94
Kriys94 deleted the fix/AccountsAPIcache branch July 23, 2026 09:16
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>
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.

3 participants