Search code examples
c#.netaccess-violationsap-dotnet-connector

SAP NCO 3.0 Library throwing "AccessViolationException" when calling RFCServer.Shutdown


I'm having an issue with the SAP NCO 3.0 library, but can't even log in to their website to post on their forums because of a server error of 500.

The issue is that I have an RFCServer set up to receive iDocs from a SAP system (<i>IDOC_INBOUND_ASYNCHRONOUS</i> for the function table) which receives an iDoc and does some processing.

When the server is in the process of receiving a large amount of files and the user shuts down the RFCServer, the SAP library throws an <i>AccessViolationException</i> causing the entire application to crash.

I've tried dressing up the encapsulated Shutdown method with the [HandleProcessCorruptedStateExceptions] Attribute, but the exception still isn't caught.

The code is :

    public void Shutdown()
    {
        try
        {
            if (_SapServer != null)
            {
                _SapServer.Shutdown(false);
            }
        }
        catch (Exception e)
        {
            throw new Exception("Error stopping server", e);
        }

        if (_SapServer != null)
        {
            _SapServer.RfcServerError -= OnRfcServerError;
            _SapServer.RfcServerApplicationError -= OnRfcServerApplicationError;
            _SapServer.RfcServerStateChanged -= OnRfcServerStateChanged;
            _SapServer.TransactionIDHandler = null;
        }
    }

I've swapped out Shutdown(true) for Shutdown(false), which tells it to abort running calls.

The stack trace is :

"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

at STRCV(Byte* , Char* , Int32* , Int32* , Int32* , Int32* , Int32* , Int32* )
at SAP.Middleware.Connector.CpicConnection.CpicReceive(Int32 timeout)
at SAP.Middleware.Connector.RfcConnection.Accept(Int32 timeout)
at SAP.Middleware.Connector.RfcConnection.RfcServerWorkerThreadProc(Object rfcConnObj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()

at System.Threading.ThreadPoolWorkQueue.Dispatch()

in sapnco_utils.dll

If anyone could point me in the right direction, as I've basically come to a dead end with this.


Solution

  • Bit late, but for anyone who has the same issue, the fix is the make sure the general configuration parameter for keeping CPIC responses alive is set to false.

    GeneralConfiguration.CPICKeepAliveResponse = false;
    

    Seems that if you ever end up in the scenario where you're stopping the SAP Server while receiving a connection it will sometimes throw this exception (probably something to do with the underlying connection being persisted). Setting this to false fixed the issue.