Skip to content

fix(config): stop rx.asset in rxconfig.py from recursing - #7671

Open
vjymisal0 wants to merge 21 commits into
reflex-dev:mainfrom
vjymisal0:fix/asset-in-rxconfig-recursion
Open

vjymisal0 wants to merge 21 commits into
reflex-dev:mainfrom
vjymisal0:fix/asset-in-rxconfig-recursion

Conversation

@vjymisal0

@vjymisal0 vjymisal0 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Calling rx.asset(...) at the top level of rxconfig.py raises RecursionError.

AssetPathStr.__new__ calls get_config() to prepend frontend_path. During the first config load, the context has no config yet, so get_config() calls _get_config(). That pops rxconfig from sys.modules and imports it again (_load_config_lock is an RLock, so the same thread re-enters). The re-import calls rx.asset again, and the loop continues until the stack overflows.

A config read from a module that rxconfig.py imports does not loop. The nested import does not run that module again, so the nested import ends and returns the real config. Only a read from rxconfig.py's own top-level code runs again in every nested import.

Changes:

  • _get_config() counts nested rxconfig.py imports in _config_import_depth, under _load_config_lock. A config read during the import:

    • returns the config that rxconfig.py already assigned, if it did;

    • raises ConfigError if rxconfig.py's own top-level code reads before it assigns config (_rxconfig_body_is_reading() checks the innermost module-level frame). The message says to move the call below the assignment or into the app module;

    • else imports rxconfig.py once more, nested, as on main. Reads from imported modules keep the real config.

  • get_config() does not cache a config read while rxconfig.py imports. It caches only after the outer import succeeds, so a rxconfig.py that fails after a read is not hidden by a cached config.

  • No Config is built mid-import, so nothing has to restore state_auto_setters.

Behavior with frontend_path="/sub" in rx.Config:

| rxconfig.py | main | This PR |

|---|---|---|

| rx.asset(...) below config = rx.Config(...) | RecursionError | /sub/favicon.ico?v=… |

| rx.asset(...) above config = rx.Config(...) (the issue's code) | RecursionError | ConfigError that names the fix |

| A module imported above config = ... reads the config or calls rx.asset | real config | real config (unchanged) |

Above the assignment no config exists yet, so no value is correct there. An env/default stand-in would silently drop a frontend_path, plugins or custom attributes set in rx.Config(...).

Verification

Updated with actual main (cb9ee91c) in b9008977. All three original PR file blobs are unchanged; the diff retains the config fix, its regressions, and the news fragment.

  • Locked workspace install on Python 3.14.7 passed.
  • All 187 config tests passed.
  • Full Ruff lint and formatting checks passed (1,962 formatted files).
  • Linux-targeted Pyright over reflex tests: zero errors, two existing unused-expression warnings.
  • Git diff and source-preservation checks passed. Adversarial review found no additional issues in this base update.

Historical evidence on the preceding ee7355b7 head: all reported upstream checks completed with 165 successes and three skips; the coverage job reported 83.21% against the required 72%. Its exact-URL and complete-error regression controls also passed. These earlier matrix and coverage results are not claimed for the new merge head; fresh upstream checks must finish there.

This update does not claim a full native unit or integration run. No components changed, so no component stub/hash change is needed.

Closes #7669

rx.asset builds its URL with get_config(). Called at the top level of
rxconfig.py, that started a second config load, which imported
rxconfig.py again and recursed until RecursionError.

While rxconfig.py is importing, _get_config() now returns the env and
default config instead of importing it again, and get_config() does not
cache that stand-in on the context.

Signed-off-by: vjymisal0 <misalvijay153@gmail.com>
@vjymisal0
vjymisal0 requested a review from a team as a code owner October 27, 2026 13:18

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 3 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge; no new actionable issues were found.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Summary

Stops config reads in rxconfig.py from repeatedly importing the same file.

  • Config reads during rxconfig.py imports use the assigned config.
  • Failed config imports remain visible on the next read.

Previous review follow-up:

  • The temporary config is gone, so it no longer changes the setting for generated setters.
  • Asset and error assertions now check complete outputs.
  • FarhanAliRaza identified helper-import failures and lost import records as existing behavior on main and left them outside this PR. Greptile withdrew both findings.
  • The previously reported failed-load caching issue is addressed by the mid-import return without caching.

Reviews (21) · Last reviewed commit: "Merge main into fix/asset-in-rxconfig-re..." · Reviewed by Greptile

@codspeed

codspeed Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 18 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 146 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing vjymisal0:fix/asset-in-rxconfig-recursion (24ef768) with main (7ee72a1)

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. ↩

@masenf masenf added the bug Something isn't working label Oct 27, 2026
…nd-in

Constructing the stand-in Config published its default
state_auto_setters, replacing the value a Config created earlier in
rxconfig.py had set. State classes defined later lost their generated
setters. Restore the published value after building the stand-in.

Signed-off-by: vjymisal0 <misalvijay153@gmail.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

… fails

The mid-import stand-in Config publishes its state_auto_setters before
validation, so a ConfigError (e.g. redis mode without a redis URL) left
that value in place. Restore the saved value in a finally block.

Signed-off-by: vjymisal0 <misalvijay153@gmail.com>
@FarhanAliRaza FarhanAliRaza added the on deck PRs lined up to review / merge next label Nov 6, 2026
The env/default stand-in returned while rxconfig.py imports gave wrong
values. rx.asset below `config = rx.Config(frontend_path=...)` lost the
frontend path, so `reflex export` baked an asset URL that 404s. Modules
that rxconfig.py imports, which read the real config on main, got the
stand-in too: no custom attributes, no plugins, no db_url.

A config read during the rxconfig.py import now:
- returns the `config` that rxconfig.py already assigned;
- raises ConfigError when rxconfig.py's own top-level code reads before
  it assigns `config`, because every nested import would run that read
  again and recurse forever;
- else imports rxconfig.py once more, nested, as on main. A module that
  rxconfig.py imports does not run again, so the nested import ends.

No Config is built mid-import, so the state_auto_setters save/restore
and the get_config() no-cache branch go away.
@greptile-apps

greptile-apps Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Comments Outside Diff

These findings could not be posted inline.

  • P1 Failed loads stay cached packages/reflex-base/src/reflex_base/config.py:1129 ▶

    Removing the no-cache branch lets a read during import save module.config before rxconfig.py finishes. For example, config = rx.Config(...), then rx.asset(...), then a failing import leaves ctx._config set even though the outer load raises. The next get_config() skips the file and returns that cached value, hiding the failed load.

    Return mid-import reads without caching them, and cache only after the outer load succeeds.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 4 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

View guided diff | Re-trigger cubic

A read after `config = rx.Config(...)` returns the half-imported module's
config, and get_config() cached it on the context. When rxconfig.py then
raised, the context kept that config, and the next get_config() returned
it instead of loading the file again. Cache only after the outer import
succeeds.
@masenf masenf removed the on deck PRs lined up to review / merge next label Nov 8, 2026
@masenf

masenf commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator

moving off deck, implementation is fairly complex for fixing what amounts to an edge case in a config architecture we're planning to eventually replace.

@vjymisal0

Copy link
Copy Markdown
Contributor Author

Thanks for taking a look, @masenf, and for the context on the config architecture. That makes sense. Feel free to close this if it isn't useful with the planned replacement; I'm happy to help with that work instead if there's an issue to pick up.

Comment thread tests/units/test_config.py Resolved
@vjymisal0

Copy link
Copy Markdown
Contributor Author

I investigated the CodSpeed report for test_route_matching[100] on b9008977 against exact main cb9ee91c.

This focused comparison runs both trees on one Linux runner, in base/head/head/base order, using Python 3.14.7, the same frozen dependencies, CodSpeed runner 5.6.3, and PYTHONHASHSEED=0. All three route counts pass in each run. Raw profiles and hardware details are attached as the reflex-route-comparison artifact.

For [100], the mean raw Ct counter is 611,528.5 on base and 603,940 on head, a 1.24% lower cost. The route implementation and benchmark file are identical between the two commits.

This focused run did not reproduce the full suite's reported 5.14% regression. The full benchmark context may matter, so the cause remains unconfirmed.

Could a maintainer rerun the Run benchmarks job for b9008977 to check the full-suite result? My upstream permission is read-only. Thank you.

This branch has not been deployed

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Recursion when using rx.asset in rxconfig on global scope

3 participants