Skip to content

fix: route frontend events from middleware preprocess updates to the client#6644

Merged
masenf merged 2 commits into
reflex-dev:mainfrom
FarhanAliRaza:fix-routing-events
Jun 10, 2026
Merged

fix: route frontend events from middleware preprocess updates to the client#6644
masenf merged 2 commits into
reflex-dev:mainfrom
FarhanAliRaza:fix-routing-events

Conversation

@FarhanAliRaza

@FarhanAliRaza FarhanAliRaza commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

A blocking middleware (e.g. an auth gate) can return a StateUpdate whose events are frontend-only specs like rx.toast or rx.redirect. These have no registered backend handler, so enqueueing them on the backend queue raised KeyError. Extract the frontend/backend routing from chain_updates into _route_events and reuse it when applying preprocess updates.

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)

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?

…client

A blocking middleware (e.g. an auth gate) can return a StateUpdate whose
events are frontend-only specs like rx.toast or rx.redirect. These have no
registered backend handler, so enqueueing them on the backend queue raised
KeyError. Extract the frontend/backend routing from chain_updates into
_route_events and reuse it when applying preprocess updates.
@FarhanAliRaza FarhanAliRaza requested a review from a team as a code owner June 10, 2026 18:40
@FarhanAliRaza FarhanAliRaza changed the title fix: route frontend events from middleware preprocess updates to the … fix: route frontend events from middleware preprocess updates to the client Jun 10, 2026
@greptile-apps

greptile-apps Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a bug where frontend-only events (e.g. rx.toast, rx.redirect) returned from a middleware's preprocess were passed directly to the backend event queue, causing a KeyError because those specs have no registered backend handler. The fix extracts the existing frontend/backend routing logic from chain_updates into a shared _route_events helper, then applies it to the middleware-preprocess path.

  • _route_events splits events by whether their name starts with _, emitting frontend events to the client and enqueueing backend events, mirroring the routing already done inside chain_updates.
  • The middleware preprocess block in _execute_event now calls _route_events instead of the bare ctx.enqueue(*update.events).
  • A new integration-style regression test verifies _call_function and _redirect events reach emitted_events (the client) rather than the backend queue.

Confidence Score: 5/5

Safe to merge — the change is a minimal, targeted extraction of existing routing logic into a shared helper, and the new middleware-preprocess path is covered by a regression test.

The fix is a straightforward refactor: the same name.startswith("_") split that already worked correctly in chain_updates is now reused for the middleware-preprocess path. The only runtime behaviour change in chain_updates itself is that ctx.enqueue() is no longer called with zero arguments when all yielded events are frontend-only — a clean improvement with no observable downside. The regression test exercises the exact failure scenario described in the PR.

No files require special attention.

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/event/processor/base_state_processor.py Introduces _route_events helper and applies it in both chain_updates and the middleware-preprocess branch; logic is correct and well-guarded.
tests/units/reflex_base/event/processor/test_base_state_processor.py Extracts wired_app fixture for reuse and adds a targeted regression test for the middleware-preprocess frontend-event routing bug.
packages/reflex-base/news/6644.bugfix.md Changelog entry accurately describes the bug and its fix.

Reviews (2): Last reviewed commit: "refactor: single-pass event partition an..." | Re-trigger Greptile

@codspeed-hq

codspeed-hq Bot commented Jun 10, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 26 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing FarhanAliRaza:fix-routing-events (95fe2fb) with main (2f95065)

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.

Split events into frontend/backend lists in one pass instead of two
comprehensions, and extract the app/state-manager wiring duplicated
across processor tests into a wired_app fixture.
@masenf masenf merged commit bb7a834 into reflex-dev:main Jun 10, 2026
106 checks passed
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.

2 participants