fix(fetch): throw AbortError from streamResponse instead of swallowing#12976
Open
Kontinuation wants to merge 1 commit into
Open
fix(fetch): throw AbortError from streamResponse instead of swallowing#12976Kontinuation wants to merge 1 commit into
Kontinuation wants to merge 1 commit into
Conversation
Contributor
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
Author
|
The jetbrains test failure does not seem to be related to our patch. Everything else looks good. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Problem
When using the OpenAI provider for autocomplete, moving the cursor during active streaming causes "Malformed JSON sent from server" errors (#7282).
Even though the original issue was closed, we still have a strong rationale for this fix: #7282 (comment).
Root cause
In
packages/fetch/src/stream.ts,streamResponsecatchesAbortErrorand silently returns. This causesstreamSseto fall through to its post-loop buffer flush, which tries toJSON.parsea partial JSON line that was mid-stream when the abort happened:Fix
One-line change in
streamResponse(line 52): throw theAbortErrorinstead of swallowing it:if (e.name.startsWith("AbortError")) { - return; // In case of client-side cancellation, just return + throw e; // Let callers handle abort – they must not process partial buffers }The exception propagates through
streamSse'sfor awaitloop, skipping the post-loop buffer flush entirely. The partial JSON is never parsed.Impact
streamSse(29 call sites across providers): protected – post-loop buffer flush is skipped via exception.streamJSON(1 call site, Cohere.ts:118): safe – has no post-loop buffer flush to begin with.streamResponsecallers: none have post-loop buffer processing.Note: I know that changing the behavior from early returning to throwing an exception here has a huge blast radius, but I have checked that the AbortError exception will be properly handled in all direct/indirect call sites, so the change should be safe. I have also manually verified aborting all sorts of operations in Visual Studio Code extension to make sure that I'm not introducing any regression.
AI Code Review
@continue-reviewChecklist
Screen recording or screenshot
No screen recording. This patch fixes a problem and made the annoying "Malformed JSON sent from server" popup going away.
Tests
Added a test to verify that AbortError was propagated and buffered incomplete data won't be parsed.
We have also tested the built VSIX on Visual Studio Code manually to verify the fix.