Skip to content

docs(auth): add spec for auth.session.auto_refresh with backoff after failed refreshes - #348

Open
spydon wants to merge 2 commits into
cm/add-refresh-session-specfrom
lukasklingsbo/sdk-2947-write-the-authsessionauto_refresh
Open

spydon wants to merge 2 commits into
cm/add-refresh-session-specfrom
lukasklingsbo/sdk-2947-write-the-authsessionauto_refresh

Conversation

@spydon

@spydon spydon commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Adds the spec for auth.session.auto_refresh: when the background loop refreshes, how it backs off after a failed refresh, and how the app and the platform lifecycle start and stop it.

This PR is stacked on #316, since it builds on refresh_session.md, get_session.md, and retry_options.md from that PR. #316 defines a single refresh, including its own retries within a 30 second deadline. This spec defines the loop that starts those refreshes.

Why

Without a defined cadence, every SDK picks its own. When a project's /token endpoint keeps failing, for example a broken auth hook answering 500, some SDKs send a failing refresh every few seconds per device, indefinitely. One project with a broken token endpoint produced about 1 million failing requests a day from a few hundred devices.

What the spec defines

  • When a refresh is due: 90 seconds before exp, three times the 30 second safety margin, so a refresh and its retries finish before Get Session has to refresh on its own. A refresh is never due sooner than half the token's lifetime (exp minus iat) after this instance received the session from the server, which stops refresh storms with short-lived tokens or a device clock running ahead. The loop starts a due refresh within 10 seconds, so either a timer or a periodic tick works.
  • Backoff after a failed refresh: 10 seconds doubling, capped at 10 minutes, with jitter between 0.5 and 1.0, and Retry-After honoured up to the cap. A steady 5xx costs a device about one request a minute once the wait reaches its cap.
  • After a rejection: when Refresh Session keeps the session, the loop's next refresh is due when the access token enters the safety margin, so the remembered rejection removes the session and emits the sign-out event on time without another request.
  • Giving up: never while a session is stored. Only a rejection removes the session.
  • A session changed during the refresh: when the stored session no longer holds the refresh token sent, the outcome is ignored and the loop schedules from the stored session, whatever the answer was.
  • Shared state: the loop joins a refresh in flight, and the outcome of any refresh of the stored session updates its schedule and failure count. The backoff applies to the loop only, so refreshes the app or Get Session start are not delayed.
  • Start and stop: a construction-time option, on by default, and public start and stop methods. Start waits for the initial load of the stored session and does not cut a running backoff wait short, so moving an app to the foreground and back during an outage does not defeat the backoff. Once the app calls either, the SDK stops driving the loop from the lifecycle. Sign-out does not stop the loop.
  • Background and foreground: where the platform has a lifecycle, the loop stops in the background and starts on return, refreshing at once if a refresh fell due.
  • Errors: loop failures reach no caller, so they go to Diagnostic Logging above debug level and are never raised as unhandled errors.

get_session.md and refresh_session.md from #316 now link to the new spec instead of naming the capability ID.

Linear: SDK-2947

@spydon
spydon requested a review from a team as a code owner November 10, 2026 13:44
@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

🗂️ Base branches to auto review: 1

  • develop

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 735b72cb-ced5-46a0-83a4-e57f92876dc2

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

example because the app was closed for longer than the token's lifetime, is
refreshed within 10 seconds of the load.

### After a failed refresh

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

#316 ended up making the refresh settings configurable since there could be different use cases a hard-coded default cannot cover.

Not sure if we want to do the same here? Keeping the values here as a default, but allow them to be configured through the AuthClient.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if such use-cases exist for the session?

This branch has not been deployed

No deployments
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