Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC Server - delegate gets garbage collected #56

Open
tom-j-irvine opened this issue Jan 19, 2022 · 3 comments
Open

RFC Server - delegate gets garbage collected #56

tom-j-irvine opened this issue Jan 19, 2022 · 3 comments

Comments

@tom-j-irvine
Copy link

I'm having an issue trying to use the RFC Server functionality. I'm able to launch the server and listen for calls. However, after some time, the world comes crashing down with the following fatal error:

Process terminated. A callback was made on a garbage collected delegate of type 'SapNwRfcDotNet!SapNwRfc.Internal.Interop.RfcInterop+RfcServerFunction::Invoke'.

Initially, I would run into this when receiving some larger function calls (after about 4 or 5 calls), but then I realized I could reproduce it with a higher number of smaller calls. So, I believe the larger calls simply trigger the GC sooner.

This link seems to describe something similar where the managed code isn't holding onto the delegate so it gets collected: https://stackoverflow.com/questions/6193711/call-has-been-made-on-garbage-collected-delegate-in-c

I may try to look closer at it myself (though, I don't know a whole lot about this interop stuff), but wanted to open this issue in case it makes more sense to one of you. Thanks.

@campersau
Copy link
Contributor

campersau commented Jan 20, 2022

Thanks for the report! I can reproduce the issue with this testcase:

var connectionPool = new SapConnectionPool(clientParameters);
var pooledConnection = new SapPooledConnection(connectionPool);

SapServer.InstallGenericServerFunctionHandler(
    (string functionName, SapAttributes attributes) => pooledConnection.GetFunctionMetadata(functionName),
    (ISapServerConnection connection, ISapServerFunction function) => { }
);

using (var server = SapServer.Create(serverParameters))
{
    server.Launch();

    while (true)
    {
        // force GC to collect unreferenced code like the generic function delegates
        GC.Collect();
        GC.WaitForPendingFinalizers();

        await Task.Delay(500);
    }
}

I have added the fix to my existing PR #53 in commit 65dfaa8

@tom-j-irvine
Copy link
Author

Thank you very much for the quick turnaround. I have confirmed that this resolves the issue I was having.

@huysentruitw
Copy link
Owner

@campersau thanks for this and sorry for the huge delay at my end. Can you create a fix for this in a separate PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants