Keep disk state reads, sidecar stats and redis version lookup off the event loop
All Submissions:
- Have you followed the guidelines stated in CONTRIBUTING.md file?
- Have you checked to ensure there aren't any other open Pull Requests for the desired changed?
Type of change
- Bug fix (non-breaking change which fixes an issue)
Changes To Core Features:
- Have you added an explanation of what your changes do and why you'd like us to include them?
- Have you written new tests for your core changes, as applicable?
- Have you successfully ran tests with your changes locally?
What
I ran a representative app under blockbuster (patched into a recording mode so one run collects every blocking call instead of raising on the first) through AppHarness, driving hydrate/on_load, plain events, computed vars, yield streaming, background tasks, uploads, client storage, rx.session/rx.asession, rx.call_script, redirects and re-hydration, once per state manager and once in prod mode with the backend serving the exported frontend. The memory state manager is clean on the event path. Three Reflex-internal blocking calls showed up in the other configurations, fixed here:
-
Disk state manager read state files on the loop.
StateManagerDisk.load_statedidexists()+open()+pickle.loadsynchronously on every cache miss, once per substate. The read now runs in a worker thread like the write already did. The per-writestates_directory.exists()on the loop is gone too; the thread handlesFileNotFoundErrorby recreating the directory. -
Precompressed static serving stat'd sidecars on the loop.
PrecompressedStaticFiles._select_sidecarcalledPath.stat()inside the syncfile_responsehook, once per accepted encoding per request. Starlette runs its own path lookup in a thread; the sidecar selection now runs in a thread fromget_response, which then finishes the response (Vary/Content-Encoding, conditional 304 check). This also replaces the previous special-case re-route for the 404.html fallback, since the same path covers it. -
redis-py resolved its version per connection. redis-py >= 7 calls
importlib.metadata.version("redis")in every newConnection.__init__unless the version is passed in, which walkssys.pathand readsMETADATAon the loop. It fired 10 times in one short session (pool growth, lock pubsub, keyspace notifications, token manager).get_redis()now pins it viadriver_infoon 7.x (lib_versionon older releases, imported dynamically so the min-version pyright check stays clean).
Each fix has a regression test that fails on the previous code by recording filesystem/metadata calls made on the event loop thread.
scripts/blockbuster_audit.py is the audit itself (dev per state manager, --prod, and a pytest-plugin mode) so this can be re-run after touching istate/, the upload path or static serving. Blockbuster only sees syscall-shaped blocking; CPU work on the loop (pickle, deepcopy of defaults, get_type_hints per event) is out of its scope.
Not changed: rx.get_upload_dir() does a mkdir(exist_ok=True) per call from handlers (microseconds; caching it would change behaviour if the directory is removed at runtime), and the docs still show with rx.session() inside event handlers far more often than rx.asession(), which for user apps is the biggest real blocker.
Testing
tests/units/istate,tests/units/test_state.py,tests/units/utils,tests/units/test_health_endpoint.py: all pass excepttests/units/istate/test_data.py::test_reflex_url_serializes_when_nested_in_router_data, which fails identically onmainin this environment.ruff check/ruff formatclean;pyrightclean on the changed files.
🤖 Generated with Claude Code
https://claude.ai/code/session_01CMzuxXkRe8q3t4sUxjrggy
Generated by Claude Code