fix(shell): prevent hang when ShellTool.Kill fails with Win32Exception#1021
Merged
Aaronontheweb merged 1 commit intoMay 16, 2026
Merged
Conversation
ShellTool's cancellation handler assumes process.Kill() always succeeds. When Kill() throws Win32Exception (OS refuses the kill), the code logged the error and immediately awaited ReadToEndAsync on both pipes. If the child process is still alive and holding stdout/stderr open, those reads never hit EOF — recreating the exact hung-tool-call failure mode this PR was meant to fix. Fix: close StandardOutput/StandardError in the Win32Exception path so pipe reads drain immediately instead of blocking forever. Also tighten the regression test to spawn a child-process tree (Unix: 'sleep 120 & wait') so it actually exercises the entireProcessTree kill path rather than a lone sleep.
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
PR #1019 fixed the
shell_executeorphaned-subprocess hang by moving toWaitForExitAsyncand handling outer cancellation. One gap remains: ifprocess.Kill(entireProcessTree: true)throwsWin32Exception(the OS refuses the kill), the handler logs the error and then immediatelyawaitsTask.WhenAll(stdoutTask, stderrTask). Since the process may still be alive and holding the pipes open, thoseReadToEndAsynccalls never hit EOF — recreating the exact hung-tool-call failure mode.Changes
ShellTool.cs— in theWin32Exceptionpath, disposeStandardOutput/StandardErrorso the pipe reads complete promptly instead of blocking forever; wrapTask.WhenAllin a catch for the expectedIOException/ObjectDisposedException.ShellToolTests.cs— tighten theCaller_cancellation_*test to spawn a child-process tree (sleep 120 & waiton Unix) so it actually exercises theentireProcessTreekill path and would hang if the tree-kill regressed.Test plan
dotnet test ShellTool— 23/23 passdotnet slopwatch analyze— 0 issues