fix(config): preserve preloaded rxconfig classes

Merged
FA
1d ago
+283-44

Summary

Classes defined in rxconfig.py (for example a rx.State subclass the app imports from its config module) lost their identity because the config module was executed more than once per process. The app module imports rxconfig, then App.__init__ called reload_config(), which re-executed rxconfig.py and produced a second copy of every class it defines. The result was a StateValueError for the duplicate substate, or _state pointing at a class the app never imported, and pickle round-trips that failed to resolve the original type.

The fix lives in the config loader and in App, so it applies to any ASGI server. No special backend entry point is needed; granian --interface asgi --factory myapp.myapp:app works unchanged.

  • App.__init__ now calls get_config() instead of reload_config(), reusing the config already loaded in the active RegistrationContext.
  • The first get_config() load of a context reuses an rxconfig module that project code already imported from the project root, instead of executing the file again. A module that _get_config() itself loaded is not reused, so separate contexts still get independent Config instances. reload_config() still always loads from disk.
  • reflex run drops its redundant reload_config() after persisting the host and port overrides; those values are already written through _set_persistent.
  • The unit-test autouse fixture now clears the Config slot on the context between tests as well as the App slot, so a mocked config from one test cannot leak into the next.

reflex/utils/exec.py is unchanged from main.

Tests

  • tests/units/test_app.py: classes defined in rxconfig.py keep their identity across App() construction, both when the config was loaded first and when the app module imported rxconfig first (the direct ASGI case).

Validation

  • Full unit suite, ruff and pyright pass.
  • A real app whose rxconfig.py defines an rx.State: direct granian --factory pkg.mod:app failed with StateValueError before and serves requests now; reflex run dev startup, State events, and hot reload of the app file and of rxconfig.py work.