Skip to content

fix: refresh access token on policy retry#8149

Draft
mikesposito wants to merge 2 commits intomainfrom
fix/regenerate-token-on-retry
Draft

fix: refresh access token on policy retry#8149
mikesposito wants to merge 2 commits intomainfrom
fix/regenerate-token-on-retry

Conversation

@mikesposito
Copy link
Member

@mikesposito mikesposito commented Mar 9, 2026

Explanation

The access token is being refreshed on policy retry, instead of reusing the same token for each API call.

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

Copy link
Contributor

@NicolasMassart NicolasMassart left a comment

Choose a reason for hiding this comment

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

One thing I noticed:

The service policy only retries on httpStatus >= 500 or non-HTTP errors (network failures, timeouts). So moving getBearerToken inside policy.execute will re-fetch the token on 5xx retries, where the token itself wasn't the issue.

If the API returns a 4xx (e.g. 401/403) for an expired token, that error isn't retried by the policy at all — the request just fails immediately regardless of where getBearerToken is called.

Could you clarify the scenario you're targeting? Is this about tokens expiring between retries during a 5xx sequence, or about recovering from auth failures?

Added also some other comments on specific parts


### Fixed

- `ProfileMetricsService` recreates access token on each API call attempt ([#8149](https://github.com/MetaMask/core/pull/8149))
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (non-blocking): Changelog entry wording could be clearer

The changelog could be misread as a new behavior on every first call. It's specifically about retries within the service policy only.

Comment on lines +202 to +205
const authToken = await this.#messenger.call(
'AuthenticationController:getBearerToken',
data.entropySourceId ?? undefined,
);
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (blocking): No test validates that the token is refreshed on retry

);
await this.#policy.execute(async () => {
const authToken = await this.#messenger.call(
'AuthenticationController:getBearerToken',
Copy link
Contributor

Choose a reason for hiding this comment

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

question (non-blocking): Is there a risk of getBearerToken itself triggering retries or delays inside the policy?

getBearerToken can itself perform a full login flow (nonce fetch, snap signing, OIDC exchange) if the session is expired. Now that this call is inside policy.execute, any latency or transient failure from the auth flow will count against the retry policy's timeout/backoff budget. If getBearerToken throws (e.g. network error during auth), the policy will retry the entire callback including auth. This is likely fine (and probably desirable), but worth confirming this is intentional.

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.

2 participants