[Regression] Fix for error on Enter-PSSession exit (#4693)

* Fix for error on Enter-PSSession exit

* Added comments per code review feedback

* Updated comment
This commit is contained in:
Paul Higinbotham
2017-09-11 17:49:28 -07:00
committed by Mike Richmond
parent 6b684bb7dd
commit f95b8aca86
2 changed files with 33 additions and 5 deletions
@@ -39,6 +39,7 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
public new Int32 ThrottleLimit { set { } get { return 0; } }
private ObjectStream _stream;
private RemoteRunspace _tempRunspace;
#endregion
@@ -533,7 +534,18 @@ namespace Microsoft.PowerShell.Commands
///
/// </summary>
protected override void StopProcessing()
{
{
var remoteRunspace = _tempRunspace;
if (remoteRunspace != null)
{
try
{
remoteRunspace.CloseAsync();
}
catch (InvalidRunspaceStateException) { }
return;
}
IHostSupportsInteractiveSession host = this.Host as IHostSupportsInteractiveSession;
if (host == null)
{
@@ -1272,10 +1284,16 @@ namespace Microsoft.PowerShell.Commands
{
var sshConnectionInfo = new SSHConnectionInfo(this.UserName, ResolveComputerName(HostName), this.KeyFilePath, this.Port);
var typeTable = TypeTable.LoadDefaultTypeFiles();
var remoteRunspace = RunspaceFactory.CreateRunspace(sshConnectionInfo, this.Host, typeTable) as RemoteRunspace;
remoteRunspace.Open();
remoteRunspace.ShouldCloseOnPop = true;
// Use the class _tempRunspace field while the runspace is being opened so that StopProcessing can be handled at that time.
// This is only needed for SSH sessions where a Ctrl+C during an SSH password prompt can abort the session before a connection
// is established.
_tempRunspace = RunspaceFactory.CreateRunspace(sshConnectionInfo, this.Host, typeTable) as RemoteRunspace;
_tempRunspace.Open();
_tempRunspace.ShouldCloseOnPop = true;
var remoteRunspace = _tempRunspace;
_tempRunspace = null;
return remoteRunspace;
}
@@ -1403,6 +1403,7 @@ namespace System.Management.Automation.Remoting.Client
private StreamWriter _stdInWriter;
private StreamReader _stdOutReader;
private StreamReader _stdErrReader;
private bool _connectionEstablished;
private const string _threadName = "SSHTransport Reader Thread";
#endregion
@@ -1464,7 +1465,12 @@ namespace System.Management.Automation.Remoting.Client
internal override void CloseAsync()
{
base.CloseAsync();
CloseConnection();
if (!_connectionEstablished)
{
// If the connection is not yet estalished then clean up any existing connection state.
CloseConnection();
}
}
#endregion
@@ -1641,6 +1647,9 @@ namespace System.Management.Automation.Remoting.Client
}
else
{
// The first received PSRP message from the server indicates that the connection is established and that PSRP is running.
if (!_connectionEstablished) { _connectionEstablished = true; }
// Normal output data.
HandleOutputDataReceived(data);
}
@@ -2410,3 +2419,4 @@ namespace System.Management.Automation.Remoting.Server
#endregion
}
}