Persist browser sessions without delaying hydration

Merged
MA
10d ago
+4010-221

Let hydration and ordinary events run while the socket-delivered session credential is exchanged for an HttpOnly cookie. Credentials are never written to browser storage; routing-token and freshness keys are scoped to the backend URL. Web Locks coordinate simultaneous cold tabs, and duplicate tabs adopt server-issued replacement tokens.

Idle timers request fresh credentials over the authenticated socket; a connect or a tab that becomes visible requests one only when the saved refresh time passed. Failed HTTP exchanges retry with bounded backoff while readiness stays pending. Success releases waiting uploads and Enterprise HTTP helpers; teardown aborts the exchange and rejects readiness. When an existing cookie replaces the provisional token, readiness waits until the replacement socket connects.

Uploads reserve their cancellation controller before waiting, so cancellation works immediately. Waiting uploads do not block ordinary WebSocket events. Transport warmup for established sessions and CONNECT-packet hydration remain intact.

Depends on #7362; real browser and deployment coverage is in #7364. Enterprise helper compatibility is in reflex-dev/reflex-enterprise#240. Merged with current main, including its reconnect retry and CONNECT auth changes in state.js.

Validation: all 13 frontend runtime/lifecycle wrapper tests pass, including the actual upload helper, idle renewal, retry recovery, cancellation, late responses after teardown, and cookie-replacement reconnects. The new failure/reconnect regressions fail before their fixes. Full assembled stack: 12,147 unit tests passed with 80.15% coverage.

Review fixes:

  • A 403 from the exchange ends it. The client logs the untrusted origin once, stops the retry and releases waiting uploads; the page no longer shows a connection error for a working socket.
  • HTTP calls wait only for an in-band credential, so a failed renewal of an existing cookie does not hold uploads.
  • The refresh timer is capped at 2**31 - 1 ms.
  • getToken() returns a legacy tab token until the server replaces it.
  • An oversized hydrate releases the cold-tab lock.

Second review round:

  • Browser tests read the client token through CLIENT_TOKEN_JS in tests/integration/utils.py: the token is under a sessionStorage key scoped to the backend URL, so tests from main that waited for the token key timed out.
  • An exchange that fails three times in a row stops holding uploads and keeps retrying (a proxy that does not forward /_reflex/session held them forever).
  • A successful exchange clears an earlier 403 denial.

An upload that the server rejects with a 4xx status, for example a 403 when the browser has no valid session cookie, now shows an error toast (an alert when the app has no toast provider). Production says "Your session has expired. Reload the page and try again." for a 403 and "The server rejected the upload." for another 4xx; development shows the status and the server's reason. Before, it only wrote to the browser console. Cancelled uploads, network errors and handler failures are unchanged.

Related to ENG-12919, ENG-9088.