AccessViolationException when issuing BeginConnect

I've created some code which runs a Socket asynchronously. This code has been fine until recently, where now, any time the client socket is either ungracefully exit, or if the connection attempt fails, the code fails with an unhandled AccessViolationException producing the following:

System.AccessViolationException was unhandled
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Source="System"
StackTrace:
at System.Net.UnsafeNclNativeMethods.OSSOCK.WSAGetOverlappedResult(SafeCloseSocket socketHandle, IntPtr overlapped, UInt32& bytesTransferred, Boolean wait, IntPtr ignored)
at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

Now, I've seen this before on the forums dealing with Remoting and various other networking technologies. So I checked the event log looking for the culprit DLL,
and it seems to be failing in kaseyasp.dll.

I may not be able to remove this, I will double check with IT.

The question I have, is there a hotfix for this problem I don't know what has changed on the machine, as I may have gotten a push from IT for something relating to this.



Answer this question

AccessViolationException when issuing BeginConnect

  • Tryst

    The error is such that it cannot be trapped. The BeginConnect call isn't raising it, otherwise I could trap it in the existing try...catch...finally block. Once the call is marshalled to the unmanaged code underlying the sockets implementation is when it gets raised. Like I said, the code I posted completes without raising an error, it is something going on with the thread it's spinning to execute the connect asynchronously, so I get an exception with no breakpoint.
  • Cammyr

    Hello, I had the same issue. The issue is cause by a bug in .NET Framework when there is an antivirus or driver in the system hooking the sockets and a socket connection is not closed properly.

    Microsoft released a hotfix for this, but you must call Microsoft to request it, so until Microsoft releases the hotfix in Windows Update there is no way to make a reliable sockets application in .NET 2.0.... unless nobody use antivirus :p

    The KB article is here http://support.microsoft.com/default.aspx scid=kb;EN-US;923028


  • Alexander Petukhov

    I'd assume that this hotfix would be in the next (first) service pack for .NET 2.0.  Anyone have a clue when we might be treated to this  
  • soanfu

    The following is the call. If there is an available target for the connection, the connect call responds correctly. However, if there is no target, an unmanaged thread (not trappable) is throwing the exception, as the exception is thrown after this call executes. If a connection can be established, the exception occurs upon severing the connection.

    Please note that this code worked until very recently. No code was changed from when it worked to when it did not.

    Like I said, this mirrors a similar issue found with Remoting applications, and the issue appears to be in the LSP layer as DLLs implementing an LSP are typically the faulting modules.

    protected override void Reconnect()

    {

    bool error = false;

    try

    {

    base.Reconnect();

    InitializeSocket();

    m_clientClosed = false;

    this.State = PeerState.Opening;

    m_remoteEndPoint = ResolveRemoteEndpoint();

    m_socket.BeginConnect(m_remoteHost, m_remotePort, this.CompleteConnect, m_socket);

    }

    catch (SocketException e)

    {

    this.SignalError(Resources.ConnectionError, e.ErrorCode.ToString(System.Globalization.CultureInfo.CurrentCulture) + " - " + e.Message);

    error = true;

    }

    catch (ObjectDisposedException)

    {

    error = true;

    }

    catch (ArgumentException e)

    {

    this.SignalError(Resources.ConnectionError, e.Message);

    error = true;

    }

    catch (FormatException e)

    {

    this.SignalError(Resources.ConnectionError, e.Message);

    error = true;

    }

    finally

    {

    if (error)

    {

    SetClosedState();

    }

    }

    }


  • Docpro777

    Sorry but you code doesnot explain the hole story. I was getting this error once due to Thread safety and collection and I got rid of it using Locking.

    May be you can debug the code to see what part of it is raising exception.

    Best regards,

    Rizwan aka RizwanSharp



  • talha zia

    Can you please post your code. So we can have a check what's going wrong, IS there any shared resource at server end which is getting grabled when a client is disconnected so you are getting this error.

    So its better to post the code.

    Best Regards,

    Rizwan aka RizwanSharp



  • AccessViolationException when issuing BeginConnect