Kill native processes in pipeline when pipeline is disposed on Unix (#15287)

This commit is contained in:
Steve Lee
2021-04-26 15:10:35 -07:00
committed by GitHub
parent aae705a3ac
commit 1df4236e6c
2 changed files with 19 additions and 1 deletions
@@ -1018,9 +1018,21 @@ namespace System.Management.Automation
try
{
// Dispose the process if it's already created
if (_nativeProcess != null)
{
// on Unix, we need to kill the process to ensure it terminates as Dispose() merely
// closes the redirected streams and the processs does not exit on macOS. However,
// on Windows, a winexe like notepad should continue running so we don't want to kill it.
#if UNIX
try
{
_nativeProcess.Kill();
}
catch
{
// Ignore all exception since it is cleanup.
}
#endif
_nativeProcess.Dispose();
}
}
@@ -43,6 +43,12 @@ Describe 'native commands with pipeline' -tags 'Feature' {
$result[0] | Should -Match "pwsh"
}
}
It 'native command should be killed when pipeline is disposed' -Skip:($IsWindows) {
$yes = (Get-Process 'yes' -ErrorAction Ignore).Count
yes | Select-Object -First 2
(Get-Process 'yes' -ErrorAction Ignore).Count | Should -Be $yes
}
}
Describe "Native Command Processor" -tags "Feature" {