In the situation where a port forward has been established and actively used, therefore a listener thread has been created, if the network is disconnected (by pulling out the cable) and reconnected sometime later then the internal thread will remain there even after everything else has been disposed properly. This causes problems, especially when trying to exit the application since it will hang.
With a fair amount of Console.WriteLineing it was established that the internal listener thread, consisting mostly of the Bind function in ChannelDirectTcpip, eventually exits its main loop correctly but then stalls at this point forever (well at least until a kill -9 is issued):
```
System.Threading.WaitHandle.WaitAny(new WaitHandle[] { this._channelEof });
```
I do not know much about the inner workings of SSH.NET and therefore do not know the best solution to the problem, but a workaround that seems to do the job for us is to simply allow that wait to timeout:
```
System.Threading.WaitHandle.WaitAny(new WaitHandle[] { this._channelEof }, 5000);
```
After that, the inner thread exits properly and no more hangs on exit.
Comments: ** Comment from web user: olegkap **
Hmm,
Well, 23468 doesnt handle disconnect differently, it only insures that IsConnected is not called during disposing.
So may be a problem somewhere else :(:(.
Is it possible for you to create a test case that I can run locally to recreate the problem, or may be if possible to connect to test server that you have where I could recreate this problem. You can send me this info privately if such scenario is possible,
I guess one more idea, to prevent isConnected from blocking is to replace this:
```
isConnected = (!this._isDisconnecting && this._socket != null && this._socket.Connected && this._isAuthenticated && this._messageListenerCompleted != null)
&& this._socket.Poll(-1, SelectMode.SelectWrite);
```
with this:
```
isConnected = (!this._isDisconnecting && this._socket != null && this._socket.Connected && this._isAuthenticated && this._messageListenerCompleted != null);
```
And see if it still hangs on IsConnected
Thanks,
Oleg