8000 Fix for error on Enter-PSSession exit · PowerShell/PowerShell@5924f77 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5924f77

Browse files
committed
Fix for error on Enter-PSSession exit
1 parent 12002e7 commit 5924f77

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/System.Management.Automation/engine/remoting/commands/PushRunspaceCommand.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class EnterPSSessionCommand : PSRemotingBaseCmdlet
3939
/// </summary>
4040
public new Int32 ThrottleLimit { set { } get { return 0; } }
4141
private ObjectStream _stream;
42+
private RemoteRunspace _tempRunspace;
4243

4344
#endregion
4445

@@ -533,7 +534,18 @@ protected override void EndProcessing()
533534
///
534535
/// </summary>
535536
protected override void StopProcessing()
536-
{
537+
{
538+
var remoteRunspace = _tempRunspace;
539+
if (remoteRunspace != null)
540+
{
541+
try
542+
{
543+
remoteRunspace.CloseAsync();
544+
}
545+
catch (InvalidRunspaceStateException) { }
546+
return;
547+
}
548+
537549
IHostSupportsInteractiveSession host = this.Host as IHostSupportsInteractiveSession;
538550
if (host == null)
539551
{
@@ -1272,10 +1284,12 @@ private RemoteRunspace GetRunspaceForSSHSession()
12721284
{
12731285
var sshConnectionInfo = new SSHConnectionInfo(this.UserName, ResolveComputerName(HostName), this.KeyFilePath, this.Port);
12741286
var typeTable = TypeTable.LoadDefaultTypeFiles();
1275-
var remoteRunspace = RunspaceFactory.CreateRunspace(sshConnectionInfo, this.Host, typeTable) as RemoteRunspace;
1276-
remoteRunspace.Open();
1277-
remoteRunspace.ShouldCloseOnPop = true;
1278-
1287+
_tempRunspace = RunspaceFactory.CreateRunspace(sshConnectionInfo, this.Host, typeTable) as RemoteRunspace;
1288+
_tempRunspace.Open();
1289+
_tempRunspace.ShouldCloseOnPop = true;
1290+
var remoteRunspace = _tempRunspace;
1291+
_tempRunspace = null;
1292+
12791293
return remoteRunspace;
12801294
}
12811295

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,7 @@ internal sealed class SSHClientSessionTransportManager : OutOfProcessClientSessi
14031403
private StreamWriter _stdInWriter;
14041404
private StreamReader _stdOutReader;
14051405
private StreamReader _stdErrReader;
1406+
private bool _connectionEstablished;
14061407
private const string _threadName = "SSHTransport Reader Thread";
14071408

14081409
#endregion
@@ -1464,7 +1465,12 @@ internal override void CreateAsync()
14641465
internal override void CloseAsync()
14651466
{
14661467
base.CloseAsync();
1467-
CloseConnection();
1468+
1469+
if (!_connectionEstablished)
1470+
{
1471+
// If the connection is not yet estalished then clean up any existing connection state.
1472+
CloseConnection();
1473+
}
14681474
}
14691475

14701476
#endregion
@@ -1642,6 +1648,7 @@ private void ProcessReaderThread(object state)
16421648
else
16431649
{
16441650
// Normal output data.
1651+
if (!_connectionEstablished) { _connectionEstablished = true; }
16451652
HandleOutputDataReceived(data);
16461653
}
16471654
}

0 commit comments

Comments
 (0)
0