Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion packages/core/auth-js/src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
EXPIRY_MARGIN_MS,
GOTRUE_URL,
JWKS_TTL,
REFRESH_FAILURE_COOLDOWN_MS,
STORAGE_KEY,
} from './lib/constants'
import {
Expand Down Expand Up @@ -288,6 +289,17 @@ export default class GoTrueClient {
protected autoRefreshTickTimeout: ReturnType<typeof setTimeout> | null = null
protected visibilityChangedCallback: (() => Promise<any>) | null = null
protected refreshingDeferred: Deferred<CallRefreshTokenResult> | null = null
/**
* Cache of the most recent refresh failure. Serial callers arriving within
* `REFRESH_FAILURE_COOLDOWN_MS` (including subsequent auto-refresh ticks)
* receive this cached result instead of firing another /token request.
* Cleared on any successful refresh (locally or via BroadcastChannel from
* another tab) and on `_removeSession`.
*
* Pairs with `refreshingDeferred`: concurrent callers share the in-flight
* promise, serial callers within the cooldown share the failure result.
*/
protected lastRefreshFailure: { result: CallRefreshTokenResult; expiresAt: number } | null = null
/**
* Monotonic counter incremented at the top of `_removeSession`, before any
* `await`. The commit guard inside `_callRefreshToken` captures this value
Expand Down Expand Up @@ -482,6 +494,12 @@ export default class GoTrueClient {
this.broadcastChannel?.addEventListener('message', async (event) => {
this._debug('received broadcast notification from other tab or client', event)

// Another tab successfully refreshed the session — any cached failure
// in this tab is stale and should not block the next refresh attempt.
if (event.data.event === 'TOKEN_REFRESHED' || event.data.event === 'SIGNED_IN') {
this.lastRefreshFailure = null
}

try {
await this._notifyAllSubscribers(event.data.event, event.data.session, false) // broadcast = false so we don't get an endless loop of messages
} catch (error) {
Expand Down Expand Up @@ -4787,6 +4805,18 @@ export default class GoTrueClient {
return this.refreshingDeferred.promise
}

// Serial failure cooldown: every caller that arrived after a recent
// non-retryable failure receives the cached result instead of firing
// another /token request. This is what stops the proactive-refresh
// storm pattern where every `getSession()` call inside the 90s
// EXPIRY_MARGIN_MS window kept re-firing against the same broken
// refresh token. Concurrent callers already share `refreshingDeferred`;
// this cache covers serial callers spaced across cooldown windows.
if (this.lastRefreshFailure && Date.now() < this.lastRefreshFailure.expiresAt) {
this._debug('#_callRefreshToken()', 'returning cached failure (cooldown active)')
return this.lastRefreshFailure.result
}

// Refresh tokens are long-lived bearer credentials; do NOT include any
// fragment of the token in the debug tag, even when `debug: true` is
// enabled (logs may be forwarded to third-party services).
Expand Down Expand Up @@ -4870,6 +4900,10 @@ export default class GoTrueClient {

const result = { data: data.session, error: null }

// Refresh succeeded — clear any cached failure so the next caller
// (including the auto-refresh ticker) attempts a real refresh again.
this.lastRefreshFailure = null

this.refreshingDeferred.resolve(result)

return result
Expand All @@ -4879,8 +4913,40 @@ export default class GoTrueClient {
if (isAuthError(error)) {
const result = { data: null, error }

// Cache any AuthError so serial callers (and the next auto-refresh
// tick) within the cooldown window receive the cached failure
// synchronously instead of firing another /token call. This is what
// stops the proactive-refresh storm pattern where every getSession()
// call inside the 90s EXPIRY_MARGIN_MS window kept hammering the
// server with the same broken refresh token.
this.lastRefreshFailure = {
result,
expiresAt: Date.now() + REFRESH_FAILURE_COOLDOWN_MS,
}

if (!isAuthRetryableFetchError(error)) {
await this._removeSession()
// Non-retryable error from GoTrue. Proactive vs reactive
// distinction: a refresh fires whenever the access token is
// within EXPIRY_MARGIN_MS of expiry. If the access token is
// *still valid* at this moment, the refresh was proactive and
// the existing session is still usable until its real expiry.
// Destroying it now would log out a user whose access token
// works. If the access token has actually expired, the refresh
// token is the only credential left and it just got rejected —
// the session is genuinely dead.
const storedNow = (await getItemAsync(this.storage, this.storageKey)) as Session | null
const accessTokenStillValid = !!(
storedNow?.expires_at && storedNow.expires_at * 1000 > Date.now()
)

if (accessTokenStillValid) {
this._debug(
debugName,
'proactive refresh failed, access token still valid — preserving session'
)
} else {
await this._removeSession()
}
}

this.refreshingDeferred?.resolve(result)
Expand Down Expand Up @@ -4984,6 +5050,10 @@ export default class GoTrueClient {
this._sessionRemovalEpoch += 1
this._debug('#_removeSession()')

// The session is gone — no point holding on to a cached refresh failure
// for a token that no longer exists.
this.lastRefreshFailure = null

this.suppressGetSessionWarning = false

await removeItemAsync(this.storage, this.storageKey)
Expand Down
11 changes: 11 additions & 0 deletions packages/core/auth-js/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ export const AUTO_REFRESH_TICK_THRESHOLD = 3
*/
export const EXPIRY_MARGIN_MS = AUTO_REFRESH_TICK_THRESHOLD * AUTO_REFRESH_TICK_DURATION_MS

/**
* After a refresh fails, serial callers (including the next auto-refresh
* tick) within this window receive the cached failure instead of firing
* another /token request. Set to two ticks so each outage burns at most one
* /token call every other tick — halves the storm volume vs today without
* introducing a separate exponential-backoff mechanism. Cleared on any
* successful refresh (locally or via BroadcastChannel from another tab) and
* on `_removeSession`.
*/
export const REFRESH_FAILURE_COOLDOWN_MS = 2 * AUTO_REFRESH_TICK_DURATION_MS

export const GOTRUE_URL = 'http://localhost:9999'
export const STORAGE_KEY = 'supabase.auth.token'
export const AUDIENCE = ''
Expand Down
8 changes: 5 additions & 3 deletions packages/core/auth-js/src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ const _getErrorMessage = (err: unknown): string => {
return JSON.stringify(err)
}

// 502, 503, 504: Standard server/gateway errors
// 520-524, 530: Cloudflare-specific error codes (web server down, connection timed out, etc.)
// 500, 501, 502, 503, 504: Standard server/gateway errors
// 520-529, 530: Cloudflare-specific error codes (web server down, connection timed out, etc.)
// These are infrastructure errors and should not cause session invalidation.
const NETWORK_ERROR_CODES = [502, 503, 504, 520, 521, 522, 523, 524, 530]
const NETWORK_ERROR_CODES = [
500, 501, 502, 503, 504, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530,
]

export async function handleError(error: unknown) {
if (!looksLikeFetchResponse(error)) {
Expand Down
Loading
Loading