-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
I have a remoting test from Linux to Windows Server 2016 that connects using PubKeyAuthentication, Copies a File to the target using Copy-Item, the tests for the file's existence using Invoke-Command.
I'm finding this test randomly fails (30-40% of the time) with an ObjectDisposedException when calling Remove-PSSession on the outbound session.
Message: "Cannot access a closed file."
The stack trace is very consistent:
at System.IO.FileStream.Flush(Boolean flushToDisk)
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
at System.IO.StreamWriter.Dispose(Boolean disposing)
at System.IO.TextWriter.Dispose()
at System.Management.Automation.Remoting.Client.SSHClientSessionTransportManager.CloseConnection()
at System.Management.Automation.Remoting.Client.SSHClientSessionTransportManager.Dispose(Boolean isDisposing)
at System.Management.Automation.Remoting.BaseTransportManager.Dispose()
at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl.Dispose(Boolean disposing)
at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl.Dispose()
at System.Management.Automation.Remoting.ClientRemoteSessionImpl.Dispose(Boolean disposing)
at System.Management.Automation.Remoting.ClientRemoteSessionImpl.Dispose()
at System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler.Dispose(Boolean disposing)
at System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal.Dispose(Boolean disposing)
at System.Management.Automation.Runspaces.RunspacePool.Dispose()
at System.Management.Automation.RemoteRunspace.Dispose(Boolean disposing)
at System.Management.Automation.Runspaces.Runspace.Dispose()
at Microsoft.PowerShell.Commands.RemovePSSessionCommand.ProcessRecord()
at System.Management.Automation.Cmdlet.DoProcessRecord()
at System.Management.Automation.CommandProcessor.ProcessRecord()
Looking at both the PowerShell code and the CoreFx code, there are three problems:
1: SSHClientSessionTransportManager.CloseConnection disposes three streams but does not clear the associated fields. The result is multiple unnecessary calls to StreamXXX.Dispose. A minor perf hit, at worse.
2: System.IO.StreamWriter does not correctly detect it has been disposed. It calls its Flush method which detects the underlying stream is null and throw the ObjectDisposedException.
3: The System.IO.Stream* instances have already been disposed before the test has called Remove-PSSession.
The cause of this is TBD; investigation in progress.
The attached file provides a repro. Running it in a loop will average about 3 failures out of 10. It does not fail when run on Windows but the repro percentage can be seen consistently on Ubuntu 16.04. In all cases, the target system is Windows Server 2016.
Steps to reproduce
SshObjectDisposedException.ps1.txt
Expected behavior
The script runs without errors
Actual behavior
~30% of the time, the script fails when closing the PSSession (Remove-PSSession)
Environment data
Client System: Ubuntu 16.04
Name Value
---- -----
PSVersion 6.1.0-rc.1
PSEdition Core
GitCommitId 6.1.0-rc.1
OS Linux 4.15.0-1022-azure #22~16.04.1-Ubuntu SMP Thu Aug 16 10:31:05 UTC 2018
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0Target System: Windows Server 2016
Name Value
---- -----
GitCommitId 6.1.0-rc.1-43-g70d601502dc05ef99f4950a67ccadf73b5338ce8
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSEdition Core
PSVersion 6.1.0-rc.1
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
Platform Win32NT
OS Microsoft Windows 10.0.14393
SerializationVersion 1.1.0.1