Fix: CancellationToken not propagating to provideLanguageModelChatResponse on chat stop#319098
Open
tamuratak wants to merge 1 commit into
Open
Fix: CancellationToken not propagating to provideLanguageModelChatResponse on chat stop#319098tamuratak wants to merge 1 commit into
provideLanguageModelChatResponse on chat stop#319098tamuratak wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds an explicit cross-process cancellation channel for language model chat requests so cancellation can still be signalled after the RPC's built-in token cancel handler has been removed (e.g., once the request promise resolves but streaming continues).
Changes:
- Introduces a new
$cancelLanguageModelChatRequestRPC on bothMainThreadLanguageModelsShapeandExtHostLanguageModelsShape. - In ext host and main thread, wraps the incoming
CancellationTokenin a localCancellationTokenSourcekeyed byrequestId, and registers a cancel listener on the caller-side token to forward cancellation via the new RPC. - Ensures the local CTS and cancel listeners are disposed on completion, error, and global dispose; reports a
CancellationErrorwhen the provider finished but the token was cancelled.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/vs/workbench/api/common/extHost.protocol.ts | Adds $cancelLanguageModelChatRequest to both main-thread and ext-host language model shapes. |
| src/vs/workbench/api/browser/mainThreadLanguageModels.ts | Tracks a per-request CTS, forwards cancellation via the new RPC, and disposes CTS/listeners on completion/error/dispose. |
| src/vs/workbench/api/common/extHostLanguageModels.ts | Mirrors the same per-request CTS + cancel-listener pattern in the ext host side, and surfaces cancellation as a CancellationError when applicable. |
Comment on lines
+152
to
+154
| $cancelLanguageModelChatRequest(requestId: number): void { | ||
| this._pendingCancelCTS.get(requestId)?.cancel(); | ||
| } |
Comment on lines
142
to
+148
| this._onDidChangeModelAccess.dispose(); | ||
| this._onDidChangeProviders.dispose(); | ||
| this._onDidChangeModelProxyAvailability.dispose(); | ||
| for (const [, cts] of this._pendingCancelTokens) { | ||
| cts.dispose(); | ||
| } | ||
| this._pendingCancelTokens.clear(); |
Comment on lines
355
to
+360
| Promise.resolve(value).then(() => { | ||
| sendNow(); | ||
| this._proxy.$reportResponseDone(requestId, undefined); | ||
| const wasCancelled = cts.token.isCancellationRequested; | ||
| cts.dispose(); | ||
| this._pendingCancelTokens.delete(requestId); | ||
| this._proxy.$reportResponseDone(requestId, wasCancelled ? transformErrorForSerialization(new CancellationError()) : undefined); |
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.
Close #291713 #277872
The stop button does not propagate cancellation to the provider's
CancellationTokeninprovideLanguageModelChatRequest. This is caused by two issues: (1) the RPC cancel handler for$startChatRequestis deleted immediately because the promise resolves before the provider returns, and (2) request IDs differ across MainThread and ExtensionHost processes, so a single CTS cannot bridge the gap. Fix this by introducing local CancellationTokenSources at both the MainThread and ExtensionHost levels, connected via a new$cancelLanguageModelChatRequestRPC method, and ensure proper cleanup of listeners on disposal.How
rpcProtocol.tscancellation works and why it breaks:In
rpcProtocol.ts, the_cancelInvokedHandlersmap stores a cancel callback keyed bycallId(the RPC request number). When a caller sends a cancel message,_receiveCancellooks up the callback and fires it. However, the callback is deleted inpromise.then()as soon as the RPC call resolves:$startChatRequestis intentionally designed to return immediately (before streaming completes) so the RPC handler stays alive long enough to forward streaming data. But the promise still resolves right away, which deletes_cancelInvokedHandlers[callId]before the user can press the stop button. After that, any incoming cancel message finds no handler and is silently ignored — this is the root cause of the bug.Created with GitHub Copilot + MiMO-V2.5-Pro
CC: @connor4312 @lramos15