fix: guard postToolUse hook against closed response stream (fixes #319003)#319011
Open
vs-code-engineering[bot] wants to merge 2 commits into
Open
fix: guard postToolUse hook against closed response stream (fixes #319003)#319011vs-code-engineering[bot] wants to merge 2 commits into
vs-code-engineering[bot] wants to merge 2 commits into
Conversation
…9003) When the user cancels a chat request while tools are executing, the response stream is closed. The postToolUse hook would then attempt to write warnings via hookProgress to the closed stream, causing an unhandled 'Response stream has been closed' error. Add a cancellation token check before executing the postToolUse hook. If the request is already cancelled, skip the hook entirely since the stream cannot accept further output. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The Compile & Hygiene check failed due to a non-ASCII character (U+2014 em-dash) in a code comment. Replace with a standard hyphen. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Summary
The Copilot Chat extension throws an unhandled error "Response stream has been closed" when the
postToolUsehook completes after the response stream has already been closed (e.g., due to user cancellation). This affects 158 users with 1,096 hits on extension version 0.50.0 (Windows). The fix adds a cancellation token guard before executing the postToolUse hook.Fixes #319003
Recommended reviewer:
@pwang347Culprit Commit
Code Flow
sequenceDiagram participant TC as toolCalling.tsx participant CHS as chatHookService.ts participant HRP as hookResultProcessor.ts participant Stream as extHostChatAgents2.ts TC->>CHS: executePostToolUseHook(stream) Note over Stream: User cancels request Stream-->>Stream: _isClosed = true CHS->>HRP: processHookResults(outputStream) HRP->>Stream: hookProgress(warnings) Stream-->>HRP: throws Error Response stream has been closedAffected Files
extensions/copilot/src/extension/prompts/node/panel/toolCalling.tsxextensions/copilot/src/extension/intents/node/hookResultProcessor.tsextensions/copilot/src/extension/chat/vscode-node/chatHookService.tssrc/vs/workbench/api/common/extHostChatAgents2.tsRepro Steps
How the Fix Works
Chosen approach (
extensions/copilot/src/extension/prompts/node/panel/toolCalling.tsx): Added a guard clauseif (props.token.isCancellationRequested) { return; }before callingexecutePostToolUseHook. This prevents the hook from executing when the request is already cancelled, which means the stream is closed and cannot accept output. This fixes at the data producer level — the producer of the "stream write after close" scenario is the code that initiates the hook call without checking whether the stream is still writable.Alternatives considered: Adding try/catch around
hookProgresscalls inhookResultProcessor.ts— rejected because it masks errors at the crash site rather than preventing the invalid operation upstream, and violates the principle of never silencing errors with try/catch.Recommended Owner
@pwang347— owns Chat Hooks area perworking-areas.md, active contributor (committed today).errors-fix-driver — cycle 1
Trigger: cron_check_failed · Head:
6c24c8028b6Compile & Hygiene(real)Push: yes —
6c24c8028b6· Copilot rerequested: okReady gate: ci pending (new push) → not marking ready this cycle