是什么导致.NET中的NullReferenceException线程/接受TCP连接?

Tim*_*mwi 6 .net c# sockets tcp

在我自己的网络服务器软件中,我在服务器上的事件查看器中获取包含以下堆栈跟踪的条目:

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*)
Run Code Online (Sandbox Code Playgroud)

不用说,这会导致进程崩溃,这意味着服务器出现故障.

由于堆栈跟踪都没有提到我自己的代码,所以我很困惑.这是.NET中的错误吗?如果是这样,有没有任何已知的解决方法?或者这个特殊例外有一个已知的原因吗?

CompletionPortCallbackstacktrace中提到的名称让我相信它是在服务器尝试接受传入的TCP连接时发生的,因此我将在下面包含相关的代码.当然,如果您认为问题出在其他地方,我很乐意加入其他代码.

BeginAccept看起来像这样的调用:

_listeningSocket.BeginAccept(acceptSocket, null);
Run Code Online (Sandbox Code Playgroud)

这里_listeningSocket是类型System.Net.Sockets.Socket.

该acceptSocket方法如下所示.我将假设这些评论很好地解释了代码; 如果没有,我很乐意在评论中澄清.由于此代码在实时服务器上以RELEASE模式运行,因此#if DEBUG当然是错误的.

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
}
Run Code Online (Sandbox Code Playgroud)

Rol*_*Bär -1

我认为抛出此异常是因为您传递了一个空引用作为第二个参数。

来自 MSDN Socket.BeginAccept 文档(http://msdn.microsoft.com/de-de/library/5bb431f9.aspx):

您必须创建一个实现 AsyncCallback 委托的回调方法,并将其名称传递给 BeginAccept 方法。为此,至少必须通过 state 参数将侦听 Socket 对象传递给 BeginAccept。如果您的回调需要更多信息,您可以创建一个小类来保存 Socket 和其他所需信息。通过 state 参数将此类的实例传递给 BeginAccept 方法。