sshnet / sshnet/SSH.NET

ForwardedPortLocal occasionally leads to CLOSE_WAIT connection

Open
#974 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
4.4k
Forks
993
Avg merge
9d 21h
Merged PRs (30d)
1

Description

I'm using ssh.net (previously plink) in order to establish a secure tunnel between a Linux machine running sshd and my client Windows server.
The tunnel is required to redirect VNC traffic from\to a not exposed VNC server (x11vnc bound to localhost:5900) in the Linux server to\from my Windows server where a VNC client connects to the ssh.net tunnel.

I randomly end up in half-open connections in the Linux server, the connection goes into CLOSE_WAIT forever and the x11vnc stays running and I have to manually kill the x11vnc process to release the CLOSE_WAIT server connection.
I would like to know how I can prevent it and get the disconnection fully acknowledged.

This is an schema of the code:

try
{
....
    var sshClient = new SshClient(sshConnectionInfo);
    sshClient.ErrorOccurred += SshError;
    sshClient.Connect();       
    port = new ForwardedPortLocal("127.0.0.1", 0, "127.0.0.1", 5900);
    port.Exception += PortException;
    sshClient.AddForwardedPort(port);
    port.Start();
    shell = sshClient.CreateShellStream(string.Empty, 0, 0, 0, 0, 0); // shell is required for other purposes
    shell.ErrorOccurred += ShellError;
....
    tcpClient = new TcpClient();
    tcpClient.Connect(port.BoundHost, (int)port.BoundPort);
    // At this point I'm able to get the stream out of the local connection to Read & Write Remote Frame Buffer                   
....                
    tcpClient.Close();
    tcpClient.Dispose();                
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}
finally
{
    port.Exception -= PortException;
    sshClient.ErrorOccurred -= SshError;
    sshClient.RemoveForwardedPort(port);                
    port.Stop();
    port.Dispose();    
    shell.ErrorOccurred -= ShellError;
    shell.Close();                
    sshClient.Disconnect();
}   

Any idea how to overcome this issue?
Is the order of executing port.Stop(), sshClient.Disconnect() and tcpClient.Close() important?

=========================================
ssh.net version: 2020.0.2
target framework: .NET 6.0
client OS: Win Server 2019
server OS: Embedded Linux
sshd version: OpenSSH 5.2p1, OpenSSL 0.97i

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the intermittent CLOSE_WAIT state with ForwardedPortLocal, TcpClient, and the finally-block sequence shown in the report, focusing on port.Stop(), sshClient.Disconnect(), and tcpClient.Close(). Compare the observed Linux-side connection and x11vnc behavior across disconnect orders; done means the relevant lifecycle order or failure condition is identified and the tunnel releases the connection without requiring manual x11vnc termination.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.