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

Fix Client memory leak caused by not detaching UnhandledException event handler #391

Merged
merged 1 commit into from
Aug 28, 2024

Conversation

Jordan-Osborn
Copy link
Contributor

The event handler registered in the client's constructor closes over the client, and is never detached. The AppDomain.CurrentDomain UnhandledException event invocation list lives for the length of the program and acts as a GC root for any created clients (due to the addition of this handler), this means clients will get leaked even if they are no longer reachable from elsewhere in the program.

In this PR I have just removed the registration of the handler, I think it's fairly unusual for a client library to need to register a handler to receive notification for all unhandled exceptions in the current app domain. But please let me know if you disagree.

(On a separate but related note, I have also noticed that the underlying socket in Connection is not deterministically cleaned up, for example if Client.Create throws, connection is not disposed of and we rely on the finalizer of Socket to clean up underlying resources (at the moment the finalizer never runs because of the reference chain Client -> Connection -> Socket and the event handler never being detached)

@Gsantomaggio
Copy link
Member

Thank you @Jordan-Osborn. Yes, you are right, the AppDomain.CurrentDomain.UnhandledException should be removed from there.

cc @lukebakken

@Gsantomaggio Gsantomaggio merged commit dc5c88d into rabbitmq:main Aug 28, 2024
2 checks passed
@Gsantomaggio
Copy link
Member

Thank you @Jordan-Osborn

Can I ask to open another issue with:

(On a separate but related note, I have also noticed that the underlying socket in Connection is not deterministically cleaned up, for example if Client.Create throws, connection is not disposed of and we rely on the finalizer of Socket to clean up underlying resources (at the moment the finalizer never runs because of the reference chain Client -> Connection -> Socket and the event handler never being detached)

If you can add some details, that would be useful.

@Gsantomaggio Gsantomaggio added this to the 1.8.8 milestone Aug 28, 2024
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

Successfully merging this pull request may close these issues.

2 participants