fix: refresh access token on policy retry#8149
Conversation
NicolasMassart
left a comment
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
| const authToken = await this.#messenger.call( | ||
| 'AuthenticationController:getBearerToken', | ||
| data.entropySourceId ?? undefined, | ||
| ); |
There was a problem hiding this comment.
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', |
There was a problem hiding this comment.
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.
Explanation
The access token is being refreshed on policy retry, instead of reusing the same token for each API call.
References
N/A
Checklist