Skip to content

fix(fetch): throw AbortError from streamResponse instead of swallowing#12976

Open
Kontinuation wants to merge 1 commit into
continuedev:mainfrom
Kontinuation:fix/propagate-sse-abort
Open

fix(fetch): throw AbortError from streamResponse instead of swallowing#12976
Kontinuation wants to merge 1 commit into
continuedev:mainfrom
Kontinuation:fix/propagate-sse-abort

Conversation

@Kontinuation

@Kontinuation Kontinuation commented Jul 11, 2026

Copy link
Copy Markdown

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, streamResponse catches AbortError and silently returns. This causes streamSse to fall through to its post-loop buffer flush, which tries to JSON.parse a partial JSON line that was mid-stream when the abort happened:

// streamSse – post-loop code (lines 144-149)
if (buffer.length > 0) {
  const { done, data } = parseSseLine(buffer); // ← partial JSON!
  if (!done && data) yield data;
}

Fix

One-line change in streamResponse (line 52): throw the AbortError instead 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's for await loop, 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.
  • 7 direct streamResponse callers: 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

  • Team members only: AI review runs automatically when PR is opened or marked ready for review
  • Team members can also trigger a review by commenting @continue-review

Checklist

  • I've read the contributing guide
  • The relevant docs, if any, have been updated or created
  • The relevant tests, if any, have been updated or created

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.

@github-actions

github-actions Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@Kontinuation

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@Kontinuation Kontinuation marked this pull request as ready for review July 11, 2026 14:23
@Kontinuation Kontinuation requested a review from a team as a code owner July 11, 2026 14:23
@Kontinuation Kontinuation requested review from sestinj and removed request for a team July 11, 2026 14:23
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jul 11, 2026
@Kontinuation

Copy link
Copy Markdown
Author

The jetbrains test failure does not seem to be related to our patch. Everything else looks good.

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

Labels

size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Malformed JSON error when cursor movement cancels autocomplete streaming with OpenAI provider

1 participant