8000 [Regression] Fix for error on Enter-PSSession exit by PaulHigin · Pull Request #4693 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 12, 2017
Merged

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

merged 3 commits into from
Sep 12, 2017

Conversation

PaulHigin
Copy link
Contributor

This is a fix for issue #4686.

I inadvertently created this regression with PR #4475. The problem is that connection state is terminated before the session close sequence completes. The fix is to only clean up connection state on session close if the session was never established.

I also added code to Enter-PSSession to handle a similar case where the Cmdlet operation is stopped before the connection or session is created. This is important for SSH remoting because PowerShell interacts with SSH and it is possible to cancel before a connection is even started.

PS > Enter-PSSession -hostname <hostname> -User <username>
<username>@<hostname>'s password:
Ctrl+C
PS >

@PaulHigin PaulHigin added WG-Remoting PSRP issues with any transport layer Issue-Bug Issue has been identified as a bug i 8000 n the product labels Aug 28, 2017
@PaulHigin PaulHigin added this to the 6.0.0-HighPriority milestone Aug 28, 2017
@PaulHigin PaulHigin requested a review from JamesWTruher August 28, 2017 23:22
@@ -1403,6 +1403,7 @@ internal sealed class SSHClientSessionTransportManager : OutOfProcessClientSessi
private StreamWriter _stdInWriter;
private StreamReader _stdOutReader;
private StreamReader _stdErrReader;
private bool _connectionEstablished;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this have an explicit initialization to false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need as CLR guarantees all type fields are initialized to zero.

@@ -1642,6 +1648,7 @@ private void ProcessReaderThread(object state)
else
{
// Normal output data.
if (!_connectionEstablished) { _connectionEstablished = true; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this just be simply _connectionEstablished = true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to do the assignment only once in the loop.

@@ -1403,6 +1403,7 @@ internal sealed class SSHClientSessionTransportManager : OutOfProcessClientSessi
private StreamWriter _stdInWriter;
private StreamReader _stdOutReader;
private StreamReader _stdErrReader;
private bool _connectionEstablished;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be an explicit initialization to false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need as CLR guarantees all type fields are initialized to zero.

@PaulHigin
Copy link
Contributor Author

Thanks!

@dantraMSFT
Copy link
Contributor
                    string errorData = data.Substring(System.Management.Automation.Remoting.Server.NamedPipeErrorTextWriter.ErrorPrepend.Length);

You continue processing data so why isn't this also considered a _connectionEstablished state? As it stands, it appears that there is a nebulas state where errors are processed but the connection cannot be closed via CloseAsync.
If I'm misreading it, a comment would help.


Refers to: src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs:1645 in 5924f77. [](commit_id = 5924f77, deletion_comment = False)

remoteRunspace.Open();
remoteRunspace.ShouldCloseOnPop = true;

_tempRunspace = RunspaceFactory.CreateRunspace(sshConnectionInfo, this.Host, typeTable) as RemoteRunspace;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should comment why _tempRunspace is being used. On the surface, the usage either implies reentrancy or threading issue; if so, StopProcessing's use of _tempRunspace is problematic. Comments explaining it's expected usage would help.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a common pattern we use for Cmdlet async StopProcessing. I'll add a comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding above comment about connection established. I think it is possible for an error to occur before the first PSRP message is processed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, we want to do immediate connection clean up only if PSRP messages are being processed because then we can rely on the protocol to clean up the client connection state. This is just for the case where the session is abruptly severed and we can't count on PSRP for clean up. I'll add comment to clarify.

Copy link
Member
@SteveL-MSFT SteveL-MSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second source file isn't showing a diff and usually this is caused by one of them having LF and the other CRLF endings. Usually what I do is explicitly save it with LF endings and push it and the diff will show up correctly.

@PaulHigin
Copy link
Contributor Author

Thanks! Will fix.

@PaulHigin
Copy link
Contributor Author

@mirichmo
I have responded to all CR comments. Can this be merged?

@PaulHigin
Copy link
Contributor Author

@dantraMSFT
Do you have any more comments or questions?

// is established.
_tempRunspace = RunspaceFactory.CreateRunspace(sshConnectionInfo, this.Host, typeTable) as RemoteRunspace;
_tempRunspace.Open();
_tempRunspace.ShouldCloseOnPop = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this an order of operations issue? It seems like the flag should be set before the call to Open().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the ShouldCloseOnPop property? No. A runspace pop cannot occur before this method completes.

@mirichmo mirichmo merged commit f95b8ac into PowerShell:master Sep 12, 2017
@PaulHigin PaulHigin deleted the fix-ssh-erroronexit branch September 12, 2017 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug Issue has been identified as a bug in the product WG-Remoting PSRP issues with any transport layer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0