Keep hydrating when a computed var raises
FA
2h ago+535+535+012345678901234567890123456789-23-23-01234567890123456789
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_loadbuilds its deltas (the snapshot, the delta carrying theis_hydratedguard, 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_valueinreflex/istate/delta.py).dict()andbuild_deltaread 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_loadpasses the first recorded error to the app'sbackend_exception_handlerand returns the events it gives back (the default toast, or e.g. a redirect to login) ahead ofon_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()andbuild_deltabehave as before. - Docs:
docs/vars/computed_vars.mdnow 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 anrx.Cookiethe browser sends as an expired value), andbuild_delta/_snapshot_valuetests intests/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/bin the same tab./bhydrates, itson_loadruns on every load, a healthyrx.LocalStoragevalue is replayed, and the handler is called once per load. With the auth cookie set toexpiredin the browser,/balso hydrates and runson_load, with one report. ruff check,ruff format --checkandpyrighton the changed files are clean.
Known limits
- A raising var that the events finishing the page load compute again still stops it: a
cache=Falsevar (every event's delta recomputes it, includinghydrate_and_load's own final delta), or one that readsis_hydrated(on_load_internalandset_is_hydratedchange it). Fixing these needs the event processor to tolerate a raising var in an event's delta. - The default snapshot
_diff_against_initial_statecomputes 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) andHydrateMiddlewarestill calldict()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