8000 Fix for Linux platform PowerShell exit on error during SSH remoting c… · PowerShell/PowerShell@5b54e4d · GitHub
[go: up one dir, main page]

Skip to content

Commit 5b54e4d

Browse files
PaulHiginmirichmo
authored andcommitted
Fix for Linux platform PowerShell exit on error during SSH remoting connection (#4993)
* Fix for Linux platform PowerShell exit on error during SSH remoting connection * Added comment for clarification per code review request
1 parent 718a672 commit 5b54e4d

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,18 +1488,26 @@ private void CloseConnection()
14881488
var stdErrReader = _stdErrReader;
14891489
if (stdErrReader != null) { stdErrReader.Dispose(); }
14901490

1491-
try
1491+
// The CloseConnection() method can be called multiple times from multiple places.
1492+
// Set the _sshProcessId to zero here so that we go through the work of finding
1493+
// and terminating the SSH process just once.
1494+
var sshProcessId = _sshProcessId;
1495+
_sshProcessId = 0;
1496+
if (sshProcessId != 0)
14921497
{
1493-
var sshProcess = System.Diagnostics.Process.GetProcessById(_sshProcessId);
1494-
if ((sshProcess != null) && !sshProcess.HasExited)
1498+
try
14951499
{
1496-
sshProcess.Kill();
1500+
var sshProcess = System.Diagnostics.Process.GetProcessById(sshProcessId);
1501+
if ((sshProcess != null) && (sshProcess.Handle != IntPtr.Zero) && !sshProcess.HasExited)
1502+
{
1503+
sshProcess.Kill();
1504+
}
14971505
}
1506+
catch (ArgumentException) { }
1507+
catch (InvalidOperationException) { }
1508+
catch (NotSupportedException) { }
1509+
catch (System.ComponentModel.Win32Exception) { }
14981510
}
1499-
catch (ArgumentException) { }
1500-
catch (InvalidOperationException) { }
1501-
catch (NotSupportedException) { }
1502-
catch (System.ComponentModel.Win32Exception) { }
15031511
}
15041512

15051513
private void StartErrorThread(

0 commit comments

Comments
 (0)
0