Skip to content

fix(state): let a background handler call the one it overrides through super() - #7964

Open
benedikt-bartscher wants to merge 2 commits into
reflex-dev:mainfrom
benedikt-bartscher:fix/super-background-handler
Open

benedikt-bartscher wants to merge 2 commits into
reflex-dev:mainfrom
benedikt-bartscher:fix/super-background-handler

Conversation

@benedikt-bartscher

@benedikt-bartscher benedikt-bartscher commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

A background event handler that overrides one from a mixin or a parent state cannot extend it through super():

class SaveMixin(rx.State, mixin=True):
    @rx.event(background=True)
    async def save(self):
        async with self:
            ...


class Editor(SaveMixin, rx.State):
    @rx.event(background=True)
    async def save(self):
        async with self:
            ...
        await super().save()  # ImmutableStateError

In a background task self is the task's StateProxy. super().save hands that proxy to EventHandler.__get__, which walks parent_state through it to find the state the handler is bound to, and the proxy refuses any attribute read outside async with self:

ImmutableStateError: Background task StateProxy is immutable outside of a context manager. Use `async with self` to modify state.

For a mixin this worked in 0.10, when @rx.event left a plain function on the mixin; since 0.11 it is an EventHandler there too. For a handler of a parent state it has failed since #7312. The workaround is calling the underlying function, await super(Editor, type(self)).save.fn(self).

EventHandler.__get__ now hands a StateProxy instance to StateProxy._bind_overridden_handler, which binds the overridden function the way the proxy's own __getattr__ binds handlers: to the proxy itself for a mixin or the proxy's own state, or through _call_on_ancestor to the ancestor declaring it. It skips __getattr__'s guard against calling a background task directly, since the overriding handler extends the other one in its own task. reflex-base cannot import reflex.istate.proxy, so the proxy type is installed on EventHandler from there, the way Field._proxy gets MutableProxy. The check runs only when owner is not the instance's own type: attribute access always passes that type, while super() through a proxy passes the wrapped state's class, so looking up a handler on a state costs what it did before (the first commit's isinstance slowed test_state_access[get_handler] by 8.9%).

test_background_handler_extends_its_base_through_super covers a mixin and a parent state on every state manager; both raised ImmutableStateError before the fix.

…h super()

`super().handler` in a background handler hands the task's StateProxy to
EventHandler.__get__, which walks `parent_state` through it to find the
state the handler is bound to. The proxy refuses that outside
`async with self`, so the call raised ImmutableStateError.

Overriding a mixin's background handler and extending it through super()
worked in 0.10, when `@rx.event` left a plain function on the mixin; since
0.11 it returns an EventHandler there too. For a handler of a parent state
it failed since reflex-dev#7312.

The proxy now binds what super() reaches like the handlers its own
__getattr__ returns: to itself, or to the ancestor declaring it through
_call_on_ancestor. Without the guard against chaining a background task:
the overriding handler extends the other one in its own task.
@benedikt-bartscher
benedikt-bartscher requested a review from a team as a code owner November 22, 2026 10:14

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

No issues found across 5 files

View guided diff | Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

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

Summary

Lets background handlers call the handler they override through super() while keeping state writes behind StateProxy.

  • Background handlers can call the handler they override with super().

Reviews (2) · Last reviewed commit: "perf(event): keep the super() proxy chec..." · Reviewed by Greptile

@codspeed

codspeed Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 4.81%

2 warnings

⚠️ 13 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

⚠️ 1 benchmark with a significant change ran in a different software environment

Its base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the change might come from this difference rather than from the code. Open the benchmark to see what changed.

See affected benchmarks

❌ 1 regressed benchmark
✅ 145 untouched benchmarks
⏩ 8 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_state_access[get_handler] 69.6 ms 73.2 ms -4.81%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing benedikt-bartscher:fix/super-background-handler (5f1a74a) with main (df7f157)

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

The isinstance check against StateProxy ran on every handler lookup, and
for a state it reads `__class__` through BaseState.__getattribute__:
test_state_access[get_handler] got 8.9% slower. Attribute access passes
the instance's own type as `owner`, while super() through a proxy passes
the wrapped state's class, so the type check now runs only when the two
differ, and on the type rather than the instance.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant