Skip to content

fix(upload): answer a failing upload handler with a 500 or an error frame - #7808

Merged
FarhanAliRaza merged 3 commits into
mainfrom
fix/upload-handler-error-response
Nov 11, 2026
Merged

FarhanAliRaza merged 3 commits into
mainfrom
fix/upload-handler-error-response

Conversation

@masenf

@masenf masenf commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

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

  • Bug fix (non-breaking change which fixes an issue)

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?

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 /_upload response. The ASGI server cuts the chunked body mid-stream (ERR_INCOMPLETE_CHUNKED_ENCODING, httpx incomplete 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_all frames. (CPython's C asyncio.Future gives 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):

handler 0.10.1 0.11.0a1
writes state, then raises (the finding's case) granian's bare 500 200, delta, truncated
yields, then raises 200, delta, truncated 200, delta, truncated
raises with no writes granian's bare 500 granian's bare 500
any failure, token's first request (hydrate delta first) truncated truncated

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.js parses chunks. Granian holds http.response.start until 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 and on_upload_progress reaches 1. upload.js then logs Upload error: Network error to 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.py has an upload-only _UploadStreamingResponse, a DisconnectAwareStreamingResponse subclass that holds http.response.start until 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.
  • The upload stream _ndjson_updates catches the handler's exception from the event processor's stream, which the processor has already reported:
    • before any update was sent: HTTPException(500). Starlette answers it inside the CORS middleware, so the browser can read the status;
    • after an update: the stream ends cleanly with {"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".
    • Nothing escapes to the server, so the exception is logged once.
    • The catch covers only 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 logs Error parsing chunk ... before Upload error: HTTP error! status: <code>.
  • EventProcessor._resolve_future keeps 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 through httpx.ASGITransport, which re-raises anything that escapes the app. Main: OSError: File name too long escapes. Fixed: status 500, one backend exception handler call, and its traceback contains failing_upload.
  • tests/units/test_app.py::test_upload_handler_error_after_update_ends_stream_with_error_frame. Main: the OSError escapes. Fixed: status 200, the update frame, then a final {"error": ...} frame, one handler call that names yield_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 the with_traceback(tb) restore reverted.
  • tests/units/reflex_base/client_upload.mjs (via test_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.log also gets Error parsing chunk with a SyntaxError.

Also run:

  • uv run pytest tests/units --cov (excluding tests/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.
  • Browser check of the fix, with the worktree installed in a 0.11.0a1-style app:
    • no-write failure: readable 500 with CORS headers, console Upload error: HTTP error! status: 500;
    • write-then-raise and yield-then-raise: 200 that ends with the error frame, console Upload error: The upload handler raised an exception., no failed request;
    • toast and state the same as before;
    • server log: one [Reflex Backend Exception] that names the handler, zero granian "Application callable raised".
  • Same raw check with this branch's reflex-components-core over the published reflex-base 0.11.0a1: 500 / error frame, no granian log.

Notes

  • A handler that writes state before raising now gets 200 plus an error frame, not 500, because its writes are streamed first (fix(event): send a failed handler's writes after its last yield #7696). A non-browser client has to check the last frame.
  • enqueue_stream_delta's "not configured" RuntimeError is still caught, though the processor never reported it. It can't occur while the route is served, since configure runs in the same lifespan step that creates the processor.
  • Tests sit next to the existing upload tests (tests/units/test_app.py, and the client_upload.mjs harness that already loads upload.js with a stubbed env.json), not in new module-named files or tests/js/.
  • Background (chunk) upload handlers answer 202 at once and are unchanged.

Generated by Claude Code

&lt;source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"&gt;&lt;source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"&gt;&lt;img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"&gt; &lt;source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"&gt;&lt;source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"&gt;&lt;img alt="Turn on auto-fix" src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"&gt;

…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
@masenf
masenf requested a review from a team as a code owner November 11, 2026 09:01
@masenf masenf added this to the v0.11.0 milestone Nov 11, 2026
@masenf masenf added the on deck PRs lined up to review / merge next label Nov 11, 2026
masenf pushed a commit that referenced this pull request Nov 11, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
@greptile-apps

greptile-apps Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

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

Summary

Uploads now return a clean error response when a handler fails and keep the handler’s traceback in backend error reports.

  • Upload failures now end with a readable error response.
  • Backend exception reports keep the handler’s original traceback.

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

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

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

@codspeed

codspeed Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Merging this PR will regress 1 benchmark

2 warnings

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

⚠️ 4 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 3 improved benchmarks
❌ 1 regressed benchmark
✅ 147 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_process_event_burst_independent_tokens[1] 2.7 ms 2.9 ms -4.63%
⚡ test_route_matching[1000] 50.6 µs 47.7 µs +6.17%
⚡ test_route_matching[10] 53.4 µs 50.6 µs +5.48%
⚡ test_state_collection_assignment[100000] 1.1 s 1.1 s +4.04%

Tip

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


Comparing fix/upload-handler-error-response (894904e) with main (b4d4cc8)

Open in CodSpeed

@masenf

masenf commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator Author

CodSpeed is red here on test_process_event_burst_independent_tokens[1], but this PR doesn't cause it. The same 4–5% drop shows on that benchmark on every PR in this batch (#7795, #7796, #7797, #7798, #7799, #7800, #7802, #7803, #7804), and CodSpeed flags its base and head as measured on different hardware.

This PR's one change on the event-processing path, _handle_raised_exception in event_processor.py, runs only after a handler has raised; the benchmark's handlers never raise. The other changes are in the /_upload route and upload.js, which no benchmark runs. CodSpeed is not a required check; leaving it for acknowledgement on CodSpeed.


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
masenf pushed a commit that referenced this pull request Nov 11, 2026
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.
masenf pushed a commit that referenced this pull request Nov 11, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
@FarhanAliRaza
FarhanAliRaza merged commit 20e9f1a into main Nov 11, 2026
167 of 168 checks passed
@FarhanAliRaza
FarhanAliRaza deleted the fix/upload-handler-error-response branch November 11, 2026 15:03
masenf added a commit that referenced this pull request Nov 11, 2026
…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)
masenf pushed a commit that referenced this pull request Nov 11, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
@masenf masenf added the release-train Issues found during agentic /prerelease-testing fanout, not necessarily validated bugs. label Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

on deck PRs lined up to review / merge next release-train Issues found during agentic /prerelease-testing fanout, not necessarily validated bugs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants