Keep hydrating when a computed var raises

FA
2h ago
+535-23

closes #8002

Why

hydrate_and_load built its snapshot with BaseState.dict(), which computes every frontend computed var of every state, not only the current page's. One var that raised (for example a KeyError after a backend login expired) aborted the whole snapshot. is_hydrated never became true and the page's on_load handlers were never queued. The client token lives in sessionStorage, so every full page load in that tab failed the same way, including pages that don't use the broken state.

What changed

  • While hydrate_and_load builds its deltas (the snapshot, the delta carrying the is_hydrated guard, and the client storage delta), a frontend computed var (sync or async) that raises is left out and its error is recorded (_snapshot_errors / _snapshot_value in reflex/istate/delta.py). dict() and build_delta read computed vars through that helper while errors are collected. The frontend merges deltas per key, so it keeps whatever value it already had for that var.
  • After the deltas went out, hydrate_and_load passes the first recorded error to the app's backend_exception_handler and returns the events it gives back (the default toast, or e.g. a redirect to login) ahead of on_load_internal. A var reading a raising var fails for the same cause, so one error is reported, as for an event that raises.
  • Outside hydration, dict() and build_delta behave as before.
  • Docs: docs/vars/computed_vars.md now says a page load computes every frontend computed var, which vars it leaves out, and that computed vars should return a fallback instead of raising.

Verification

  • Regression tests, committed before each fix: test_hydrate_and_load_survives_raising_computed_vars (sync, async and dependent vars, one report, processor and app sharing one handler as in an app), test_hydrate_and_load_survives_raising_cookie_dependent_var (a var on an rx.Cookie the browser sends as an expired value), and build_delta / _snapshot_value tests in tests/units/istate/test_delta.py.
  • The issue's repro in a dev app in Chromium, with a counting backend exception handler: expire on /, then load and reload /b in the same tab. /b hydrates, its on_load runs on every load, a healthy rx.LocalStorage value is replayed, and the handler is called once per load. With the auth cookie set to expired in the browser, /b also hydrates and runs on_load, with one report.
  • ruff check, ruff format --check and pyright on the changed files are clean.

Known limits

  • A raising var that the events finishing the page load compute again still stops it: a cache=False var (every event's delta recomputes it, including hydrate_and_load's own final delta), or one that reads is_hydrated (on_load_internal and set_is_hydrated change it). Fixing these needs the event processor to tolerate a raising var in an event's delta.
  • The default snapshot _diff_against_initial_state computes when no compile ran in the process still raises for a var that raises on default values.
  • State.hydrate (used when shared state re-links) and HydrateMiddleware still call dict() without this protection.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • This change requires a documentation update

https://claude.ai/code/session_01LwJUCTLa6ssVoX8zHH7QyW


Generated by Claude Code