Repository navigation
fix(upload): answer a failing upload handler with a 500 or an error frame - #7808
Conversation
…rame 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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
[Medium impact] The PR appears safe to merge; no new blocking issues were found. SummaryUploads now return a clean error response when a handler fails and keep the handler’s traceback in backend error reports.
The earlier compatibility concern is fixed by keeping the delayed response start in the upload module. masenf intentionally kept the upload tests in their existing files. Reviews (5) · Last reviewed commit: "fix(upload): don't parse a non-2xx uploa..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 10 files
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
Merging this PR will regress 1 benchmark2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing |
|
CodSpeed is red here on This PR's one change on the event-processing path, Generated by Claude Code |
…t 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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
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 Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
…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 <noreply@anthropic.com> 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 <noreply@anthropic.com> 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 <noreply@anthropic.com> Co-authored-by: Farhan <www.mfarvirus@gmail.com> (cherry picked from commit 20e9f1a)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
All Submissions:
Type of change
Changes To Core Features:
Release fix-up for FINDING-044 from the reflex 0.11.0a1 pre-release testing (artifacts on branch
claude/vibrant-pasteur-3921g3,prerelease-testing/runs/2026-11-11-v0.11.0a1/).Defect
If an upload handler raises, the exception escapes the streamed
/_uploadresponse. The ASGI server cuts the chunked body mid-stream (ERR_INCOMPLETE_CHUNKED_ENCODING, httpxincomplete chunked read) and logs the exception a second time ("Application callable raised an exception", after the backend exception handler's[Reflex Backend Exception]). Neither log names the handler: the upload stream awaits the event's future and re-raises the exception before the backend exception handler task runs, and that re-raise replaces the traceback, leaving only_wait_for_chain/wait_allframes. (CPython's Casyncio.Futuregives an awaited exception only the awaiting frames.)Behaviour compared on PyPI 0.10.1 and 0.11.0a1 (granian, dev mode, warm token, raw
curl --raw):The behaviour change comes from #7696 (merged after 0.10.1). It streams a failing handler's writes before the exception propagates, so the body has already started when the exception arrives. #7721 changed only how
upload.jsparses chunks. Granian holdshttp.response.startuntil the first body message, so 0.10.1 got a 500 only when nothing had been streamed. That 500 came from granian itself and had no CORS headers.In the browser (
rx.upload+rx.upload_files, Chromium) the UX was the same in both versions. The backend exception handler's error toast arrives over the websocket, the written state is applied andon_upload_progressreaches 1.upload.jsthen logsUpload error: Network errorto the console: for 0.10.1 because CORS blocks the 500, for 0.11.0a1 because the chunked body is incomplete. So users don't see a regression; the problems are on the wire and in the logs.Fix
_upload.pyhas an upload-only_UploadStreamingResponse, aDisconnectAwareStreamingResponsesubclass that holdshttp.response.startuntil the first body message. A body that fails before yielding can then still be answered with an error status. Because it lives in reflex-components-core, the fix also works with the published reflex-base 0.11.0a1; I checked that by hand._ndjson_updatescatches the handler's exception from the event processor's stream, which the processor has already reported:HTTPException(500). Starlette answers it inside the CORS middleware, so the browser can read the status;{"error": "The upload handler raised an exception."}. The message is generic because the details go to the backend exception handler, and in prod that handler says only "contact the administrator".anext()of the stream. Serializing an update,app.event_processor, and a processor that isn't running (QueueShutDown) fail outside it, so the server still logs those.upload.js: when it sees an error frame it rejects the upload (Upload error: <message>) and does not pass the frame to the state listener. It parses chunks only from a 2xx response, so the plain-text body of the 500 (or of a 400 such as "Unknown upload event handler") no longer logsError parsing chunk ...beforeUpload error: HTTP error! status: <code>.EventProcessor._resolve_futurekeeps the traceback the handler raised with and restores it in the handler task, so[Reflex Backend Exception]shows the handler's frame (upapp.py, line 21, in fail_nowrite).Test plan
Failing before the fix (run with the source files checked out from
origin/main), passing after:tests/units/test_app.py::test_upload_handler_error_before_any_update_returns_500: a real Starlette route throughhttpx.ASGITransport, which re-raises anything that escapes the app. Main:OSError: File name too longescapes. Fixed: status 500, one backend exception handler call, and its traceback containsfailing_upload.tests/units/test_app.py::test_upload_handler_error_after_update_ends_stream_with_error_frame. Main: theOSErrorescapes. Fixed: status 200, the update frame, then a final{"error": ...}frame, one handler call that namesyield_then_fail_upload.tests/units/test_app.py::test_upload_error_the_event_processor_did_not_report_reaches_the_server(QueueShutDown, an update that fails to serialize): both fail on 5d91c16, where they were swallowed. Both now escape to the server.tests/units/reflex_base/event/processor/test_event_processor.py::test_backend_exception_handler_sees_handler_frames_when_future_awaited. Main:'_error_handler' in ['test_backend_exception_handler_sees_...']fails. It also fails with only thewith_traceback(tb)restore reverted.tests/units/reflex_base/client_upload.mjs(viatest_client_event_queue.py), "an error frame ending the stream fails the upload, not the listener". Main: the upload resolves with the response and the listener gets the error frame.tests/units/reflex_base/client_upload.mjs, "an error response fails the upload by its status, not as a chunk" (a 500 with a plain-text body). Before the 2xx check:console.logalso getsError parsing chunkwith aSyntaxError.Also run:
uv run pytest tests/units --cov(excludingtests/units/reflex_bench/drivers/test_browser.py: no Chromium for pytest-playwright in this environment): 15639 passed, 66 skipped, coverage 81.46%.ruff check,ruff format,pyright reflex tests(0 errors) and the pre-commit hooks on the changed files (biome, ty) all pass.npm --prefix tests/js test: 29 passed.Upload error: HTTP error! status: 500;Upload error: The upload handler raised an exception., no failed request;[Reflex Backend Exception]that names the handler, zero granian "Application callable raised".Notes
RuntimeErroris still caught, though the processor never reported it. It can't occur while the route is served, sinceconfigureruns in the same lifespan step that creates the processor.tests/units/test_app.py, and theclient_upload.mjsharness that already loadsupload.jswith a stubbedenv.json), not in new module-named files ortests/js/.Generated by Claude Code
<source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="Turn on auto-fix" src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg">