Repository navigation
fix(state): let a background handler call the one it overrides through super() - #7964
benedikt-bartscher wants to merge 2 commits into
Conversation
…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.
[Medium impact] The PR appears safe to merge; no actionable issues were found. SummaryLets background handlers call the handler they override through
Reviews (2) · Last reviewed commit: "perf(event): keep the super() proxy chec..." · Reviewed by Greptile |
Merging this PR will degrade performance by 4.81%2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
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.
A background event handler that overrides one from a mixin or a parent state cannot extend it through
super():In a background task
selfis the task'sStateProxy.super().savehands that proxy toEventHandler.__get__, which walksparent_statethrough it to find the state the handler is bound to, and the proxy refuses any attribute read outsideasync with self:For a mixin this worked in 0.10, when
@rx.eventleft a plain function on the mixin; since 0.11 it is anEventHandlerthere 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 aStateProxyinstance toStateProxy._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_ancestorto 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 importreflex.istate.proxy, so the proxy type is installed onEventHandlerfrom there, the wayField._proxygetsMutableProxy. The check runs only whenowneris not the instance's own type: attribute access always passes that type, whilesuper()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'sisinstanceslowedtest_state_access[get_handler]by 8.9%).test_background_handler_extends_its_base_through_supercovers a mixin and a parent state on every state manager; both raisedImmutableStateErrorbefore the fix.