On Unix, only explicitly terminate the native process if not in background (#18215)

This commit is contained in:
Steve Lee
2022-10-07 08:07:22 +05:00
committed by GitHub
parent 7073525059
commit 667e75559d
@@ -422,7 +422,7 @@ namespace System.Management.Automation
catch (Exception)
{
// Do cleanup in case of exception
CleanUp();
CleanUp(killBackgroundProcess: true);
throw;
}
}
@@ -444,7 +444,7 @@ namespace System.Management.Automation
catch (Exception)
{
// Do cleanup in case of exception
CleanUp();
CleanUp(killBackgroundProcess: true);
throw;
}
}
@@ -904,7 +904,7 @@ namespace System.Management.Automation
finally
{
// Do some cleanup
CleanUp();
CleanUp(killBackgroundProcess: false);
}
// An exception was thrown while attempting to run the program
@@ -1161,7 +1161,8 @@ namespace System.Management.Automation
/// <summary>
/// Aggressively clean everything up...
/// </summary>
private void CleanUp()
/// <param name="killBackgroundProcess">If set, also terminate background process.</param>
private void CleanUp(bool killBackgroundProcess)
{
// We need to call 'NotifyEndApplication' as appropriate during cleanup
if (_hasNotifiedBeginApplication)
@@ -1171,17 +1172,20 @@ namespace System.Management.Automation
try
{
// 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.
// on Unix, we need to kill the process (if not running in background) to ensure it terminates,
// as Dispose() merely closes the redirected streams and the process does not exit.
// However, on Windows, a winexe like notepad should continue running so we don't want to kill it.
#if UNIX
try
if (killBackgroundProcess || !_isRunningInBackground)
{
_nativeProcess?.Kill();
}
catch
{
// Ignore all exception since it is cleanup.
try
{
_nativeProcess?.Kill();
}
catch
{
// Ignore all exceptions since it is cleanup.
}
}
#endif
_nativeProcess?.Dispose();