Socket not detecting dead sockets
SUMMARY
If an client is disconnected by unknown reasons (taskkill -f, shutdown, etc), the server won't disconnect it properly.
EXPECTED BEHAVIOR
Socket.Receive would return a 0 length message telling the server that a client has died, but Socket.ReceiveAsync doesn't return anything and the program doesn't complete it's path.
ACTUAL BEHAVIOR
Socket.ReceiveAsync doesn't reach the Disconnection Breakpoint.
REPEAT STEPS
- Connect an client to the server
- Force it to close (taskkill -f)
SOLUTION
Socket.ReceiveAsync won't return a 0 length message anymore, instead it will throw an Exception. Catch the Exception in
examined = await actor.Socket.ReceiveAsync( actor.Buffer.Slice(remaining), SocketFlags.None, this.ShutdownToken.Token); and break the Receive loop.
try { examined = await actor.Socket.ReceiveAsync(actor.Buffer.Slice(remaining), SocketFlags.None, this.ShutdownToken.Token); } catch { break; }
Source: MSDN