Repository navigation
fix: guard postToolUse hook against closed response stream (fixes #319003) - #319011
Merged
vs-code-engineering[bot] merged 2 commits intoJun 9, 2026
Merged
vs-code-engineering[bot] merged 2 commits into
vs-code-engineering[bot] merged 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 <[email protected]>
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 <[email protected]>
Paul (pwang347)
approved these changes
Jun 9, 2026
Raymond Zhao (rzhao271)
approved these changes
Jun 9, 2026
vs-code-engineering
Bot
deleted the
fix/stream-closed-post-hook-319003-f59c7075aade6723
branch
June 9, 2026 20:46
vs-code-engineering Bot
added a commit
that referenced
this pull request
Jun 18, 2026
…eam write (fixes #321951) The postToolUse hook can run for a long time because it spawns external, user-configured commands. If the chat request is cancelled while the hook runs, the response stream is closed by the time the hook resolves, and writing hook progress to it throws an unhandled "Response stream has been closed" error. The prior fix (cdf17eb, #319011) only checked cancellation before invoking the hook, leaving the cancellation-during-hook window open. Re-check the cancellation token in executePostToolUseHook after the await and skip result processing when cancelled, since a cancelled turn never consumes the result. Co-authored-by: Copilot <[email protected]>
vs-code-engineering Bot
added a commit
that referenced
this pull request
Jun 19, 2026
…eam write (fixes #321951) (#321959) fix: re-check cancellation after postToolUse hook to avoid closed stream write (fixes #321951) The postToolUse hook can run for a long time because it spawns external, user-configured commands. If the chat request is cancelled while the hook runs, the response stream is closed by the time the hook resolves, and writing hook progress to it throws an unhandled "Response stream has been closed" error. The prior fix (cdf17eb, #319011) only checked cancellation before invoking the hook, leaving the cancellation-during-hook window open. Re-check the cancellation token in executePostToolUseHook after the await and skip result processing when cancelled, since a cancelled turn never consumes the result. Co-authored-by: vs-code-engineering[bot] <122617954+vs-code-engineering[bot]@users.noreply.github.com> Co-authored-by: Copilot <[email protected]>
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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