Hi,
I've been having this issue for a while now and was never really able to resolve it. This issue only appears when I'm using the SendAsync/ReceiveAsync methods rather than the Begin/EndSend socket methods for asynchronous operations.
If have a fairly sophisticated tcp socket library and been meaning to replace the BeginSend methods with SendAsync but because of the issue I'm experiencing I always had to put it off. My socket server is handling heavy stress scenarios with > 1000 clients connected pushing constantly over 100mbit/sec and I'd like to utilize SendAsync method so I don't have the IAsyncResult allocation overhead.
Anyways, what happens is, everything works fine as long as I'm just sending/receiving data, however under high stress scenarios when the server is trying to disconnect/shutdown a client I'm occasionally getting the following Exception:
System.InvalidOperationException was unhandled
Message=Cannot apply a context that has been marshaled across AppDomains, that was not acquired through a Capture operation or that has already been the argument to a Set call.
Source=mscorlib
StackTrace:
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSuccess(SocketError socketError, Int32 bytesTransferred, SocketFlags flags)
at System.Net.Sockets.SocketAsyncEventArgs.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
InnerException:
I'm unable to catch this exception anywhere as it seems to happen within the .NET framework and I can't do anything about it crashing my server.
As this only happens when I call my shutdown procedure, I'm guessing it has something to do with calling Shutdown on the socket while it still has a Read/Write procedure pending. However, I also tried to delay calling shutdown until all read/write Send/ReceiveAsync calls have returned and call shutdown after that but that didn't help either.
Here's how I try to shutdown sockets:
private void InternalDisconnect(SocketError socketError)
{
lock (shutdownLock)
{
if (isShutdown)
return;
isShutdown = true;
}
allowSend = false;
SocketError = socketError;
ThreadPool.QueueUserWorkItem(delegate
{
lock (padLock)
{
try
{
if (TcpSocketStatus == TcpSocketStatus.Disconnected)
return;
TcpSocketStatus = TcpSocketStatus.Disconnecting;
if (asyncSendArgs != null)
{
asyncSendArgs.Completed -= SendCompleted;
asyncSendArgs.SetBuffer(null, 0, 0);
asyncSendArgs.Dispose();
}
if (asyncReceiveArgs != null)
{
asyncReceiveArgs.Completed -= ReceiveCompleted;
asyncReceiveArgs.SetBuffer(null, 0, 0);
asyncReceiveArgs.Dispose();
}
try
{
bufferedSender.Clear();
Socket.Shutdown(SocketShutdown.Both);
if (Socket.Connected)
{
Socket.Disconnect(true);
}
}
catch
{
}
try
{
Socket.Close();
}
catch
{
}
TcpSocketStatus = TcpSocketStatus.Disconnected;
if (socketError != SocketError.Success)
{
if (log.IsDebugEnabled)
log.Debug("SocketDisconnected\tSocketError:{0}", socketError);
}
else
{
if (log.IsDebugEnabled)
log.Debug("SocketDisconnected");
}
DisconnectTime = DateTime.UtcNow;
if (TcpSocketDisconnected != null)
TcpSocketDisconnected(this);
}
catch (Exception ex)
{
log.ErrorException("InternalDisconnect", ex);
}
}
});
}