mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Kill native processes in pipeline when pipeline is disposed on Unix (#15287)
This commit is contained in:
@@ -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" {
|
||||
|
||||
Reference in New Issue
Block a user