Skip to content

fix: handle the complete upload response once it loads - #7744

Closed
SrkianFardin25 wants to merge 2 commits into
reflex-dev:mainfrom
SrkianFardin25:fix/upload-final-chunk-parse
Closed

SrkianFardin25 wants to merge 2 commits into
reflex-dev:mainfrom
SrkianFardin25:fix/upload-final-chunk-parse

Conversation

@SrkianFardin25

@SrkianFardin25 SrkianFardin25 commented Nov 5, 2026 •

Copy link
Copy Markdown

Closes #7736

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? (no JS test harness for upload.js in the repo; checked with a small script, see below)
  • Have you successfully ran tests with your changes locally?

What and why

The upload endpoint streams application/x-ndjson with no Content-Length, so event.lengthComputable is false on every xhr.onprogress and progress is always 0. In uploadFiles, the "Error parsing chunk" log is gated on progressEvent.progress === 1, so it never runs; a chunk that fails to parse is skipped silently (this is how #7720 stayed hidden).

The same gate means a chunk that was still incomplete at the last progress event is never handled again, because nothing re-runs the parser once the response is complete.

Change (packages/reflex-base/.../web/utils/helpers/upload.js): in xhr.onload, for a 2xx response, call eventHandler one last time with the full responseText and progress: 1, before resolving. resp_idx already tracks handled chunks, so nothing is applied twice; chunks not yet handled are applied, and a chunk that does not parse is now logged. Error responses (non-2xx) are not parsed, as before.

Added a reflex-base news fragment (+upload-final-chunks.bugfix.md).

Testing

Ran uploadFiles in Node with a mocked XMLHttpRequest and socket:

  • progress event with {"a":1}\n{"b": (no Content-Length), then the body completes as {"a":1}\n{"b":2}\n{bad json}\n and onload fires.
  • Before: only {"a":1} was applied and nothing was logged.
  • After: {"a":1} and {"b":2} are applied once each, and Error parsing chunk {bad json} … is logged.

Not tested end to end in a browser with reflex run. Prettier check passes on the changed file.

View guided diff

The upload endpoint streams NDJSON without a Content-Length, so
lengthComputable is false on every progress event and progress is
always 0. The "Error parsing chunk" log therefore never ran, and
chunks still unparsed at the last progress event were never applied.

Run the chunk handler one final time in xhr.onload with progress 1,
when the response is known to be complete.

Closes reflex-dev#7736
@SrkianFardin25
SrkianFardin25 marked this pull request as ready for review November 5, 2026 06:39
@SrkianFardin25
SrkianFardin25 requested a review from a team as a code owner November 5, 2026 06:39
@greptile-apps

greptile-apps Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change is limited to browser file uploads and their streamed updates.

The required regression tests should be added before merging.

Findings

  1. P2 Required regression tests are missing ▶

Summary

The PR runs the upload response parser once more after a successful response finishes.

  • Successful uploads apply the last updates from the streamed response.

Reviews (3) · Last reviewed commit: "fix: skip the final upload parse when th..." · 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.

1 issue found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/reflex-base/src/reflex_base/.templates/web/utils/helpers/upload.js">

<violation number="1" location="packages/reflex-base/src/reflex_base/.templates/web/utils/helpers/upload.js:99">
P2: On the final pass, one malformed mid-stream line stops the loop, so every later complete chunk is still dropped. At progress 1 the response is complete, so a malformed line in the middle should be logged and skipped rather than returning from eventHandler.</violation>
</file>

Reply to a comment to ask cubic a question or push back. It learns from your replies.

View guided diff | Re-trigger cubic

// The streamed response has no Content-Length, so no progress event
// reports it as complete. Handle it once more now that it is, so the
// last chunks are applied and any chunk that does not parse is logged.
eventHandler({

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.

P2: On the final pass, one malformed mid-stream line stops the loop, so every later complete chunk is still dropped. At progress 1 the response is complete, so a malformed line in the middle should be logged and skipped rather than returning from eventHandler.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/reflex-base/src/reflex_base/.templates/web/utils/helpers/upload.js, line 99:

<comment>On the final pass, one malformed mid-stream line stops the loop, so every later complete chunk is still dropped. At progress 1 the response is complete, so a malformed line in the middle should be logged and skipped rather than returning from eventHandler.</comment>

<file context>
@@ -93,6 +93,13 @@ export const uploadFiles = async (
+        // The streamed response has no Content-Length, so no progress event
+        // reports it as complete. Handle it once more now that it is, so the
+        // last chunks are applied and any chunk that does not parse is logged.
+        eventHandler({
+          event: { target: { responseText: xhr.responseText } },
+          progress: 1,
</file context>

A successful upload can return an empty body. Splitting it yields one
empty chunk that fails to parse, so the final pass in xhr.onload logged
a false "Error parsing chunk". Only run it when the body has content.

Also reword the changelog fragment: only the first chunk that fails to
parse is logged.
@SrkianFardin25

Copy link
Copy Markdown
Author

Pushed 4f4a6a9: the final parse in xhr.onload now only runs when xhr.responseText.trim() is non-empty, so a successful upload with an empty body no longer logs a false "Error parsing chunk". I also reworded the changelog fragment to say it logs the first update chunk that fails to parse.

Checked with a small Node simulation of uploadFiles and a mocked XHR: empty and whitespace-only bodies log nothing; a malformed final chunk logs one parse error after the valid chunk before it is handled; a valid final chunk is handled with no error. At 96d5073 the empty-body cases logged the error. biome format reports no changes.

Comment on lines +100 to +104
if (xhr.responseText.trim()) {
eventHandler({
event: { target: { responseText: xhr.responseText } },
progress: 1,
});

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.

P2 Required regression tests are missing

This PR changes how uploads finish but adds no tests. CLAUDE.md requires a regression test for bug fixes; the manual Node check is not saved for future runs.

Before merging, add tests in tests/js/ covering the final response pass, updates being applied only once, malformed chunks being logged, and empty responses being skipped.

Context Used: CLAUDE.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@FarhanAliRaza FarhanAliRaza added the bug Something isn't working label Nov 12, 2026 — with ChatGPT Codex Connector
@FarhanAliRaza FarhanAliRaza added on deck PRs lined up to review / merge next and removed on deck PRs lined up to review / merge next labels Nov 12, 2026
@codspeed

codspeed Bot commented Nov 12, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 6%

3 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

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

⚠️ 123 benchmarks with a significant change ran in a different software environment

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

See affected benchmarks

⚡ 123 improved benchmarks
✅ 25 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_isinstance_container[list_optional] 293.4 ms 261.1 ms +12.4%
⚡ test_route_argument_extraction 118.5 µs 106.1 µs +11.62%
⚡ test_state_update_wire_serialization[dataclass_1000] 16 ms 14.4 ms +11.09%
⚡ test_state_update_wire_serialization[mapping_100] 285.9 µs 257.4 µs +11.09%
⚡ test_state_collection_assignment[100000] 1.2 s 1.1 s +9.59%
⚡ test_decode_event_headers[64] 180.3 µs 164.8 µs +9.44%
⚡ test_event_payload_plain_collection 69.8 ms 64.1 ms +8.83%
⚡ test_isinstance_scalar 109.2 ms 100.6 ms +8.49%
⚡ test_process_event_burst_same_token[100] 1.3 s 1.2 s +8.42%
⚡ test_import_reflex 9.7 ms 9 ms +8.12%
⚡ test_decode_event_headers[4] 41.9 µs 38.8 µs +7.94%
⚡ test_get_state[redis] 31 ms 28.8 ms +7.87%
⚡ test_process_event_burst_independent_tokens[100] 176 ms 163.3 ms +7.74%
⚡ test_process_event_burst_independent_tokens[10] 18.6 ms 17.3 ms +7.55%
⚡ test_get_state_uncached[disk] 56.4 ms 52.4 ms +7.47%
⚡ test_compile_all_artifacts[_complicated_page] 131.5 ms 122.4 ms +7.44%
⚡ test_format_var_outside_operation 31.9 ms 29.7 ms +7.4%
⚡ test_compile_page[_complicated_page] 195.6 ms 182.6 ms +7.12%
⚡ test_proxy_list_mutation 9.9 ms 9.3 ms +7.08%
⚡ test_proxy_mapping_mutation 12.9 ms 12 ms +7.06%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing Fardin7798:fix/upload-final-chunk-parse (4f4a6a9) with main (058fa98)

Open in CodSpeed

@FarhanAliRaza FarhanAliRaza added the on deck PRs lined up to review / merge next label Nov 19, 2026
@FarhanAliRaza

Copy link
Copy Markdown
Collaborator

Closing in favor of #7759, which merged as 832bfed and fixes the same issue, #7736: an upload chunk that fails to parse is now reported when the upload completes. Thank you for the fix and for the follow-up on empty response bodies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working on deck PRs lined up to review / merge next

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Upload chunk parse errors are never logged: progress === 1 is never true for the streamed upload response

2 participants