From ccee14dc652b756b3606278e9c0cb2872f4fc72b Mon Sep 17 00:00:00 2001 From: Dan Travison Date: Mon, 10 Sep 2018 12:31:56 -0700 Subject: [PATCH] Ensure the `SSHClientSessionTransportManager` stream writer and reader fields are cleared after dispose. (#7746) Fix possible race conditions accessing the fields during CloseConnection. --- .../engine/remoting/fanin/OutOfProcTransportManager.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs b/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs index 652ed1f9f..1cf899b48 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs @@ -1503,20 +1503,19 @@ namespace System.Management.Automation.Remoting.Client private void CloseConnection() { - var stdInWriter = _stdInWriter; + var stdInWriter = Interlocked.Exchange(ref _stdInWriter, null); if (stdInWriter != null) { stdInWriter.Dispose(); } - var stdOutReader = _stdOutReader; + var stdOutReader = Interlocked.Exchange(ref _stdOutReader, null); if (stdOutReader != null) { stdOutReader.Dispose(); } - var stdErrReader = _stdErrReader; + var stdErrReader = Interlocked.Exchange(ref _stdErrReader, null); if (stdErrReader != null) { stdErrReader.Dispose(); } // The CloseConnection() method can be called multiple times from multiple places. // Set the _sshProcessId to zero here so that we go through the work of finding // and terminating the SSH process just once. - var sshProcessId = _sshProcessId; - _sshProcessId = 0; + var sshProcessId = Interlocked.Exchange(ref _sshProcessId, 0); if (sshProcessId != 0) { try