In my own web server software, I get entries in the event viewer on the server that contain the following stack trace:
Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.ArgumentNullException Stack: at System.Net.FixedSizeReader.ReadCallback(System.IAsyncResult) at System.Net.LazyAsyncResult.Complete(IntPtr) at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) at System.Net.ContextAwareResult.Complete(IntPtr) at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32, UInt32, System.Threading.NativeOverlapped*) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
Needless to say, this leads to a process crash, which means that the server is down.
Since none of the stacktrace mentions my own code, I am very puzzled. Is this a bug in .NET? If so, are there any known workarounds? Or is there a known reason for this particular exception?
The name CompletionPortCallback given in stacktrace makes me think that this happens when the server tries to accept an incoming TCP connection, so I'm going to include the appropriate code for this below. Of course, I'm glad to include another code if you think the problem lies elsewhere.
The BeginAccept call is as follows:
_listeningSocket.BeginAccept(acceptSocket, null);
Here _listeningSocket is of type System.Net.Sockets.Socket .
Below is the acceptSocket method. I'm going to suggest that the comments explain the code well enough; if not, I am happy to clarify in the comment. Since this code works in RELEASE mode on a real server, #if DEBUG will of course be false.
private void acceptSocket(IAsyncResult result) { #if DEBUG // Workaround for bug in .NET 4.0 and 4.5: // https://connect.microsoft.com/VisualStudio/feedback/details/535917 new Thread(() => #endif { // Ensure that this callback is really due to a new connection (might be due to listening socket closure) if (!IsListening) return; // Get the socket Socket socket = null; try { socket = _listeningSocket.EndAccept(result); } catch (SocketException) { } // can happen if the remote party has closed the socket while it was waiting for us to accept catch (ObjectDisposedException) { } catch (NullReferenceException) { if (_listeningSocket != null) throw; } // can happen if StopListening is called at precisely the "wrong" time // Schedule the next socket accept if (_listeningSocket != null) try { _listeningSocket.BeginAccept(acceptSocket, null); } catch (NullReferenceException) { if (_listeningSocket != null) throw; } // can happen if StopListening is called at precisely the "wrong" time // Handle this connection if (socket != null) HandleConnection(socket); } #if DEBUG ).Start(); #endif }