Repository navigation
fix: handle the complete upload response once it loads - #7744
SrkianFardin25 wants to merge 2 commits into
Conversation
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
[Medium impact] The change is limited to browser file uploads and their streamed updates. The required regression tests should be added before merging. Findings
SummaryThe PR runs the upload response parser once more after a successful response finishes.
Reviews (3) · Last reviewed commit: "fix: skip the final upload parse when th..." · Reviewed by Greptile |
There was a problem hiding this comment.
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({ |
There was a problem hiding this comment.
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.
|
Pushed 4f4a6a9: the final parse in Checked with a small Node simulation of |
| if (xhr.responseText.trim()) { | ||
| eventHandler({ | ||
| event: { target: { responseText: xhr.responseText } }, | ||
| progress: 1, | ||
| }); |
There was a problem hiding this comment.
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!
Merging this PR will improve performance by 6%3 warnings
Performance Changes
Tip Curious why performance improved? Comment Comparing |
Closes #7736
All Submissions:
Type of change
Changes To Core Features:
upload.jsin the repo; checked with a small script, see below)What and why
The upload endpoint streams
application/x-ndjsonwith noContent-Length, soevent.lengthComputableisfalseon everyxhr.onprogressandprogressis always0. InuploadFiles, the"Error parsing chunk"log is gated onprogressEvent.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): inxhr.onload, for a 2xx response, calleventHandlerone last time with the fullresponseTextandprogress: 1, before resolving.resp_idxalready 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-basenews fragment (+upload-final-chunks.bugfix.md).Testing
Ran
uploadFilesin Node with a mockedXMLHttpRequestand socket:{"a":1}\n{"b":(noContent-Length), then the body completes as{"a":1}\n{"b":2}\n{bad json}\nandonloadfires.{"a":1}was applied and nothing was logged.{"a":1}and{"b":2}are applied once each, andError parsing chunk {bad json} …is logged.Not tested end to end in a browser with
reflex run. Prettier check passes on the changed file.