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

AspNetCore.HealthChecks.Rabbitmq leaks connections when using the factory #2364

Open
JanEggers opened this issue Jan 10, 2025 · 5 comments
Open
Labels

Comments

@JanEggers
Copy link

What happened:
when using

 services.AddHealthChecks()
.AddRabbitMQ(factory: c => /* my factory does not matter*/)

the healthcheck does not close the connection causing a massive ammount of connections to be opened

What you expected to happen:
either one of these:

  • the healthcheck should cache the connection after it was initially created and use that one connection for subsequent health checks
  • the healthcheck should close the connection after healthcheck is completed if the connection was created with the factory
  • drop the factory support and require the connection to be provided by DI (I would discourage this approach as the connection requires async code to be created which does not play nicely with di as far as im aware and the creation may fail if there is no broker running)

Version 8.0.2 was working fine Version 9.0.0 contains the issue.

propably this change: #2343

@JanEggers
Copy link
Author

I actually read the readme and now its clear. but I was just updating from one version to the next and was not expecting that a parameter called factory was supposed to provide a cached singleton.

@JanEggers
Copy link
Author

FYI my solution for the time beeing:

services.AddSingleton<Lazy<Task>>(c =>
new Lazy<Task>(() => /* my factory does not matter*/);

services.AddHealthChecks()
.AddRabbitMQ(factory: c => c.GetRequiredService<Lazy<Task>>().Value.Result);

that way I still have access to the service provider while creating the connection (I use it to resolve configuration)

@adamsitnik
Copy link
Collaborator

Why do you register a Lazy factory? The DI container won't create it until it's needed for the first time.

cc @eerhardt

@JanEggers
Copy link
Author

I guess its not needed then.

@kooshan
Copy link

kooshan commented Jan 19, 2025

This is a really fucked up way to setup a so called FACTORY!!! for health check. Wasted my whole day on finding the source of resource leakage. It looks more like an unintentional bug now being disguised as an intentional clever design. Please do fix this atrocity.

That setup guide in the readme offers no help either.

  1. What if I have another service which will use IConnection and I don't want to use singleton registration for that?
  2. How can I access my IConfiguration for rabbit user/pass when I use the static Lazy to initialize a factory? Should I always hardcode my user/pass in my code?

Wouldn't it be easier to add a using at the place where you invoke the factory instead of this nonsense?

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

No branches or pull requests

3 participants