Repository navigation
fix(event): send a failed handler's writes after its last yield - #7696
Conversation
A foreground handler that is cancelled or raises could leave changes it made after its last yield unsent. Memory and disk carried them in the next handler's delta, but a state manager that loads a fresh copy, as redis does, dropped the dirty marks, so the frontend never received them until the var changed again or the page reloaded. Flush the delta under the state lock before the exception propagates. A flush failure is logged so the handler's own exception still propagates.
[Medium impact] The PR appears safe to merge; the remaining cancellation-test gap is non-blocking. Findings
SummarySends pending foreground state changes before a failed or cancelled handler releases its lock.
Reviews (3) · Last reviewed commit: "fix(event): skip the flush when a failed..." · Reviewed by Greptile |
Merging this PR will not alter performance1 warning
Comparing |
…on-failed-handler # Conflicts: # tests/units/reflex_base/event/processor/test_base_state_processor.py
masenf
left a comment
There was a problem hiding this comment.
code change looks fine, but the tests are failing still
…on-failed-handler # Conflicts: # packages/reflex-base/src/reflex_base/event/processor/base_state_processor.py
A handler whose own delta fails has its state cleaned by that delta, so the flush only recomputed the failing delta and logged a second traceback for the same error. The flush now runs only when a write is pending. Also update the yield events docs: writes after the last yield now reach the frontend when the handler raises or is cancelled.
There was a problem hiding this comment.
All reported issues were addressed across 3 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
…rame (#7808) * fix(upload): answer a failing upload handler with a 500 or an error frame An upload handler that raised made the exception escape the streamed /_upload response: the server cut the chunked body mid-stream and logged the exception a second time. Since #7696 a failing handler's writes are streamed before its exception, so the body had always started. - DisconnectAwareStreamingResponse holds http.response.start until the first body message, so a body that fails before yielding can still be answered with an error status. - The upload stream catches the handler's exception (already reported by the event processor): before any update it raises HTTPException(500), after one it ends with an {"error": ...} frame. Nothing escapes to the server, so the exception is logged once. - upload.js reports an error frame by rejecting the upload instead of passing it to the state listener. - The backend exception handler gets the traceback the handler raised with: a caller awaiting the event's future (the upload stream) re-raised it first and replaced the handler's frames. FINDING-044 from the reflex 0.11.0a1 pre-release testing. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * fix(upload): keep the delayed response start in the upload module; let unreported errors through Review follow-ups for #7808: - Move the response-start hold from reflex-base's DisconnectAwareStreamingResponse into an upload-only subclass in _upload.py. reflex-components-core no longer relies on an unreleased reflex-base change: its reflex-base floor cannot exclude the published 0.11.0a1, since main builds reflex-base as 0.10.2.devN. - Narrow the upload stream's catch to the event processor's stream. Serializing an update and creating the stream now fail outside it, and a processor that is not running (QueueShutDown) is re-raised. Errors the processor never reported now reach the server log, as they did before. - Correct the traceback docstrings: CPython's C asyncio.Future re-raises an awaited exception with only the awaiting frames (the pure-Python future prepends them). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * fix(upload): don't parse a non-2xx upload response as update chunks An error response (the 500 for a handler that fails before any update, or a 400) has a plain-text body. upload.js parsed it as an ndjson chunk and logged "Error parsing chunk ..." before the real "Upload error". Skip chunk parsing unless the status is 2xx; onload reports the error. --------- Co-authored-by: Claude <[email protected]> Co-authored-by: Farhan <[email protected]>
…rame (#7808) * fix(upload): answer a failing upload handler with a 500 or an error frame An upload handler that raised made the exception escape the streamed /_upload response: the server cut the chunked body mid-stream and logged the exception a second time. Since #7696 a failing handler's writes are streamed before its exception, so the body had always started. - DisconnectAwareStreamingResponse holds http.response.start until the first body message, so a body that fails before yielding can still be answered with an error status. - The upload stream catches the handler's exception (already reported by the event processor): before any update it raises HTTPException(500), after one it ends with an {"error": ...} frame. Nothing escapes to the server, so the exception is logged once. - upload.js reports an error frame by rejecting the upload instead of passing it to the state listener. - The backend exception handler gets the traceback the handler raised with: a caller awaiting the event's future (the upload stream) re-raised it first and replaced the handler's frames. FINDING-044 from the reflex 0.11.0a1 pre-release testing. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * fix(upload): keep the delayed response start in the upload module; let unreported errors through Review follow-ups for #7808: - Move the response-start hold from reflex-base's DisconnectAwareStreamingResponse into an upload-only subclass in _upload.py. reflex-components-core no longer relies on an unreleased reflex-base change: its reflex-base floor cannot exclude the published 0.11.0a1, since main builds reflex-base as 0.10.2.devN. - Narrow the upload stream's catch to the event processor's stream. Serializing an update and creating the stream now fail outside it, and a processor that is not running (QueueShutDown) is re-raised. Errors the processor never reported now reach the server log, as they did before. - Correct the traceback docstrings: CPython's C asyncio.Future re-raises an awaited exception with only the awaiting frames (the pure-Python future prepends them). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * fix(upload): don't parse a non-2xx upload response as update chunks An error response (the 500 for a handler that fails before any update, or a 400) has a plain-text body. upload.js parsed it as an ndjson chunk and logged "Error parsing chunk ..." before the real "Upload error". Skip chunk parsing unless the status is 2xx; onload reports the error. --------- Co-authored-by: Claude <[email protected]> Co-authored-by: Farhan <[email protected]> (cherry picked from commit 20e9f1a)
Resolutions: - The frontend moved to packages/reflex-frontend (#7794): event_results.js lives in src/utils, its vitest suite in tests, and the state.js harnesses load it from there. The thenable ResultStream carries a biome-ignore for noThenProperty, and main's debounce test asserts the shared stream. - Stateless handlers were reverted (#7770): their execution path and the stateless result test go; the cancelled-while-sending test uses a background handler returning outside `async with self` instead. - _is_result builds on main's _is_invalid_result, so yields and returns share its rules for mistaken events (self.handler, an uncalled rx.redirect); the foreground path keeps main's flush on failure (#7696). - EventContext keeps both request_id/emit_result_impl and main's middleware. - The dropped-result warning (#7796) now applies only to an event sent without a request id; the chaining docs say where the value goes instead. - The awaitable-handlers news fragments were released with 0.11.0. - reflex now needs reflex-base >= 0.11.1.dev0 for the request id API. Claude-Session: https://claude.ai/code/session_01LwJUCSPGJ9cvdFM2KLFHoH
Related to #7248. Follows #7412.
Problem
A foreground handler can change state after its last
yield, for example in anexcept asyncio.CancelledErrorblock when asupersedes=Truecall cancels it, or just before it raises. Nothing sends those changes to the frontend._init_bookkeepingclears the dirty marks. The frontend shows the old value until the var changes again or the page reloads.Fix
When
process_eventraises or is cancelled,_execute_eventflushes the delta withchain_updates(None, root_state=...)before the exception propagates. The flush runs under the state lock, beforerelease_lock(), so it reaches the frontend before any delta from the next handler and before the error toast.finally.docs/events/yield_events.mdnow says that these writes reach the frontend when the handler raises or is cancelled.Tests
test_failed_handler_emits_writes_after_last_yield: memory, disk and redis, each cancelled and raising. All 6 cases fail on main and pass with the fix.test_failed_handler_flush_error_keeps_handler_exception: a computed var makes the flush fail. The handler'sRuntimeErroris reported and the flush error is logged.test_failed_delta_is_not_flushed_again: an uncached computed var makes the handler's own delta fail. The error is reported once and no flush runs.Checked in an example app on memory and real Redis: raise before and after a yield, cancelled
supersedes=Truehandler, background task, chained event, failingon_load, and a backend exception handler that returns events. The frontend, the backend state and a page reload agree in every case.