Skip to content

Invoke-Command and New-PSSession hang indefinitely when ssh.exe unexpectedly terminates #25203

@tmolenh

Description

@tmolenh

Prerequisites

Steps to reproduce

This is trivially easy to reproduce by just trying to connect to a system that does not exist (e.g. test@test). However, it can also randomly happen when trying to connect to system that is rebooting, which is how I discovered it.

To prove that it's ssh process termination that triggers this bug, try running Invoke-Command -HostName ... with this running in the background:

while ($true) {
    Start-Sleep -Milliseconds 500
    Stop-Process -Name "ssh" -ErrorAction SilentlyContinue -PassThru
}

The only workaround seems to be to use the -ConnectingTimeout parameter, but it just forces pwsh to wait the entire timeout duration, even though ssh.exe isn't even running.

Expected behavior

# It should behave exactly like ssh.exe:
PS C:\> ssh.exe test@test "echo 1"
ssh: Could not resolve hostname test: No such host is known.
PS C:\>

Actual behavior

PS C:\> Invoke-Command -HostName "test" -UserName "test" -ScriptBlock { echo 1 }
ssh: Could not resolve hostname test: No such host is known.

<Indefinite hang, need to press Ctrl+C to regain prompt, breaking non-interactive sessions>

# New-PSSession is also impacted:
PS C:\> New-PSSession -HostName "test" -UserName "test"
ssh: Could not resolve hostname test: No such host is known.

<hang>
# Also, -ErrorAction doesn't help:
PS C:\> Invoke-Command -HostName "test" -UserName "test" -ScriptBlock { echo 1 } -ErrorAction Stop
ssh: Could not resolve hostname test: No such host is known.

<hang>

Error details

Environment data

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.5.0
PSEdition                      Core
GitCommitId                    7.5.0
OS                             Microsoft Windows 10.0.26100
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-RemotingPSRP issues with any transport layer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions