Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does System.Net.Sockets.Socket.AcceptAsync complete with ConnectionReset after a long time of inactivity?

It appears that when you call Socket.AcceptAsync with a valid SocketAsyncEventArgs and have the SocketAsyncEventArgs.Completed event rigged properly and no connection is accepted for a very long period of time that it just resets the connection. Though, my Socket.ReceiveTimeout and Socket.SendTimeout are both zero.

I'm unsure of how to set the timeout for accepting connections and even if it's a good idea at all. Does anyone have a workaround and perhaps some information as to why this is the default behavior?

I filed a bug report on Microsoft Connect to see if they have any reasoning behind why the timeout period is unstable. Sometimes it times out in five minutes and other times more than two hours.

like image 361
Michael J. Gray Avatar asked Nov 24 '11 23:11

Michael J. Gray


1 Answers

After playing around with this for a bit I have found that a single SYN -> SYN-ACK -> RST sequence will raise the SocketAsyncEventArgs.Completed event and cause the SocketAsyncEventArgs.SocketError property to become SocketError.ConnectionReset. It appears this is expected behavior but it's definitely a gotcha and should be documented better.

Anyone port scanning your server and doing a half-open SYN type scan will generate similar traffic and cause the same problem. To prevent denial of service vulnerabilities in the software, one should be handling this special condition.

like image 123
Michael J. Gray Avatar answered Oct 27 '22 01:10

Michael J. Gray