jchristn / SuperSimpleTcp

Simple wrapper for TCP client and server in C# with SSL support
MIT License
459 stars 95 forks source link

Added DisconnectAsync, use this when becoming disconnected from the s… #145

Closed redrabbit007 closed 2 years ago

redrabbit007 commented 2 years ago

I noticed that when my client program was disconnected from the server. (This was caused by the program running on the server closing) My program would usually hang. When I did a Break and looked at the Tasks it showed that a cycle was detected.

Looking into the code I believe I've found the issue. Inside the DataReceiver Task it calls DataReadAsync. Inside DataReadAsync if we read zero bytes from the stream, it checks the tcpConnections. If the connection is not ok it calls this.Disconnect. Inside the Disconnect function it does _dataReceiver.Wait(). The Disconnect function is waiting for the DataReceiver Task to finish, but the DataReceiver Task is blocked waiting for control to come back from Disconnect, hence the deadlock.

Changing these to be awaits instead of waits solves the issue.

jchristn commented 2 years ago

Thank you so much @redrabbit007 - merged, published to NuGet! Thank you so much for your contribution! Also sorry I tagged you as @redrabbit25 instead of 007, I fixed it in the README (but the commit has the wrong username, sorry about that)

NuGet: https://www.nuget.org/packages/SuperSimpleTcp/2.6.1.2 Commit: https://github.com/jchristn/SuperSimpleTcp/commit/fbf97072f96537205fb3d160490c1fc2cabc79ff