I was testing ssh.net with 2 ForwardedPortLocals. When a connection is established, a port is forwarded properly but the channel is sometimes not closed, depending on how the local port was closed.
It can get stuck waiting for an EOF that never comes in ChannelDirectTcpip.cs Bind()
System.Threading.WaitHandle.WaitAny(new WaitHandle[] { this._channelEof });
this is because the while loop above can exit gracefully if _socket.CanRead() returns false. In this scenario no CloseMessage was ever sent so it will sit waiting forever. I suggest adding:
this.SendMessage( new ChannelCloseMessage( this.RemoteChannelNumber ) );
after the while loop or moving the WaitAny line into Close();
Comments: ** Comment from web user: nlewis **
It can get stuck waiting for an EOF that never comes in ChannelDirectTcpip.cs Bind()
System.Threading.WaitHandle.WaitAny(new WaitHandle[] { this._channelEof });
this is because the while loop above can exit gracefully if _socket.CanRead() returns false. In this scenario no CloseMessage was ever sent so it will sit waiting forever. I suggest adding:
this.SendMessage( new ChannelCloseMessage( this.RemoteChannelNumber ) );
after the while loop or moving the WaitAny line into Close();
Comments: ** Comment from web user: nlewis **
I also found a case where the local port is not closed.
If the session has an error, but the local fowarded port is waiting to read from the local socket. When the session is broken, the local listener is stopped but existing sockets can get stuck in CanRead().
When the session is broken, all of the existing local socket connections should be explicitly closed.