Skip to content

fix(dash0): keep valid sessions when a token refresh fails transiently - #782

Merged
fclairamb merged 2 commits into
mainfrom
fix/session-expired-refresh
Nov 6, 2026
Merged

fclairamb merged 2 commits into
mainfrom
fix/session-expired-refresh

Conversation

@fclairamb

@fclairamb fclairamb commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

Why

Over 30 days, 16 of 87 PostHog replays of logged-in users include a /login?session_expired=true redirect, across 11 users. Several of those users still held a valid refresh token at the time of the redirect.

  • User 4c18f13a (org a8cb119a), 2026-10-10 17:02: their open dashboard tab unloaded itself with no user action and went to session_expired=true. That was 8 minutes after a successful refresh. The refresh token was valid until 10-17, with an active owner membership.
  • User d8dfb730 (org a6b8a965), 2026-10-30 07:07: a cold load went to session_expired=true with a refresh token valid until 11-06, and no refresh was recorded on the server.
  • A dev replay (2026-10-25) shows the mechanism end to end: /auth/me 401, then /auth/refresh 500, console [auth] token refresh failed: rejected, tokens wiped, redirect to login?session_expired=true.

Token lifetimes are not the cause. Access tokens last 1h, and refresh tokens use a 7-day sliding idle window that the server slides correctly. Two of the redirects (org 87ca1191, 10-16 and 10-27) were real 7-day idle expiries, which is intended.

Cause

  • token-refresh.ts treated any non-OK answer from /auth/refresh as "rejected": a 500 from a DB hiccup, a 502 from the ingress during a pod restart, or a 429. It then cleared both tokens and redirected. The server only answers 401 for a dead session (handleRefreshError).
  • apiFetch handled a refresh that failed on a network error the same way. refreshAccessToken() returned null, the original 401 went to handleResponse, and that cleared the session and redirected. This contradicted the module's own rule that a network blip must never clear the session.

Fix

  • Only a 401 from /auth/refresh is rejected. Any other failure is now unavailable (renamed from network-error, which now also covers non-401 server answers). It keeps the tokens and logs a warning.
  • apiFetch throws a NetworkError when the refresh is unavailable, instead of logging the user out.
  • On a cold page load, /auth/me is retried twice (after 1s, then 3s) on a transient failure. Before, such a failure left the user signed out on the login page.

Tests

  • token-refresh.test.ts: 500, 502, 503 and 429 give unavailable with no clear and no redirect. A 401 still clears and redirects.
  • client.test.ts: an apiFetch whose refresh hits a 500 or a network error rejects with NetworkError, keeps both tokens and does not navigate.
  • All 6 new tests fail on main and pass here. Full unit suite: 182 files, 2795 tests passing. tsc -b passes.

🤖 Generated with Claude Code

A 500, 502 or 429 from /auth/refresh counted as "rejected": the dashboard
cleared both tokens and sent the user to login?session_expired=true. A
network error during the refresh did the same through apiFetch's 401 path.
The server answers 401 for every dead session, so only a 401 now ends it.
Any other failure is "unavailable": the request fails as a NetworkError
and the tokens stay. The cold-load /auth/me check retries twice before
giving up.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@codecov

codecov Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.22%. Comparing base (8684c71) to head (51494af).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #782      +/-   ##
==========================================
- Coverage   89.23%   89.22%   -0.02%     
==========================================
  Files        1320     1320              
  Lines      135152   135152              
==========================================
- Hits       120609   120595      -14     
- Misses      14534    14548      +14     
  Partials        9        9              

see 20 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@fclairamb
fclairamb merged commit 8e77846 into main Nov 6, 2026
33 checks passed
@fclairamb
fclairamb deleted the fix/session-expired-refresh branch November 6, 2026 18:17
@fclairamb fclairamb mentioned this pull request Nov 6, 2026
fclairamb added a commit that referenced this pull request Nov 7, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.44.1](v0.44.0...v0.44.1)
(2026-11-07)


### Bug Fixes

* **dash0:** keep valid sessions when a token refresh fails transiently
([#782](#782))
([8e77846](8e77846))
* **deps:** update module github.com/crewjam/saml to v0.5.2
([#786](#786))
([9d370fe](9d370fe))
* **deps:** update module github.com/posthog/posthog-go to v1.40.1
([#775](#775))
([37ada84](37ada84))
* **deps:** update module github.com/posthog/posthog-go to v1.41.1
([#785](#785))
([8684c71](8684c71))
* **docs:** plain markdown for the API reference .md pages
([#784](#784))
([3270ac6](3270ac6))
* **security:** normalize trailing-dot custom status domains
([#778](#778))
([8b2a7ca](8b2a7ca))
* **slack:** resolve check and incident link unfurls
([#779](#779))
([930f18e](930f18e))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant