tylerjensen/ServiceWire

TcpHost accepting new client synchronously when AcceptAsync returns false

aprofessionalpirate opened this issue · 2 comments

Hi there,

I was taking a look at the code for TcpHost.cs and noticed that inside of the loop in Listen() if _listener.AcceptAsync(_acceptEventArg) returns false it will call AcceptNewClient(_acceptEventArg) synchronously which means that until this client disconnects the listener will not be connecting to any new clients.

On https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.acceptasync it says this about AcceptAsync(SocketAsyncEventArgs) returning false:

false if the I/O operation completed synchronously. The Completed event on the e parameter will not be raised and the e object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.

I'm not exactly sure how to trigger this condition but it seems like it would be easy to fix if AcceptNewClient(_acceptEventArg) was run on a separate thread if this condition were met, allowing other clients through once this client is accepted.

I've tried making AcceptNewClient run on a separate thread but cascading problems occur. I will continue to investigate.

Closing it as I've not been able to create a scenario in which this occurs. I'd be happy to entertain a PR if anyone has a solution.