Skip to content

ENG-9830 fix: avoid config re-entry when a State is defined in rxconfig.py#6662

Open
FarhanAliRaza wants to merge 2 commits into
reflex-dev:mainfrom
FarhanAliRaza:state-in-config
Open

ENG-9830 fix: avoid config re-entry when a State is defined in rxconfig.py#6662
FarhanAliRaza wants to merge 2 commits into
reflex-dev:mainfrom
FarhanAliRaza:state-in-config

Conversation

@FarhanAliRaza

Copy link
Copy Markdown
Contributor

State-class creation read get_config().state_auto_setters at class-creation time, which re-entered config loading while rxconfig.py was still importing and raised AttributeError. Cache the flag when the Config is built and read it via get_state_auto_setters(), falling back to the env var before any Config exists.

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

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

  • This change requires a documentation update

New Feature Submission:

  • Does your submission pass the tests?
  • Have you linted your code locally prior to submission?

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?

closes #6661

State-class creation read get_config().state_auto_setters at class-creation
time, which re-entered config loading while rxconfig.py was still importing
and raised AttributeError. Cache the flag when the Config is built and read
it via get_state_auto_setters(), falling back to the env var before any
Config exists.
@FarhanAliRaza FarhanAliRaza requested a review from a team as a code owner June 15, 2026 21:05
@greptile-apps

greptile-apps Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes an AttributeError raised when a rx.State subclass is defined in rxconfig.py before the Config(...) call, by breaking the re-entrant call to get_config() that occurred at class-creation time.

  • Adds _state_auto_setters: bool | None module-level cache in config.py, populated inside Config.__init__, and a new get_state_auto_setters() helper that reads the cache (or falls back to the REFLEX_STATE_AUTO_SETTERS env var, then False) without ever touching get_config().
  • Replaces the get_config().state_auto_setters call in BaseState._init_var with get_state_auto_setters(), eliminating the re-entrant import.
  • Adds four regression tests covering crash prevention, env-var fallback, default behavior, and cache update after reload.

Confidence Score: 5/5

Safe to merge β€” the change is minimal and targeted, replacing one call site in state.py and adding a cache variable with a well-defined fallback chain in config.py.

The fix is narrowly scoped: one call-site change in state.py and a new cached accessor in config.py. The fallback chain (cache β†’ env var β†’ False) mirrors existing env-var handling. Four regression tests are added.

No files require special attention.

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/config.py Adds module-level _state_auto_setters cache populated in Config.__init__, and a new get_state_auto_setters() that reads the cache (or falls back to env var / False) without calling get_config().
reflex/state.py Replaces get_config().state_auto_setters with get_state_auto_setters() in _init_var, eliminating the re-entrant config load that caused AttributeError.
tests/units/test_state.py Adds four tests covering the crash regression, env-var fallback, default (no auto-setters), and cache-tracking across reloads β€” though the reload test only covers States defined outside rxconfig.py, not the full reload-stale scenario.
news/6662.bugfix.md Changelog entry for the bug fix.
packages/reflex-base/news/6662.bugfix.md Changelog entry for reflex-base package.

Reviews (2): Last reviewed commit: "docs: add changelog entries for config r..." | Re-trigger Greptile

@codspeed-hq

codspeed-hq Bot commented Jun 15, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

βœ… 26 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing FarhanAliRaza:state-in-config (26b2f48) with main (932f20f)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[regression] circular import when rxconfig.py attempts to import State

1 participant