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

[Bug] Dispose() not called for singleton services #281

Open
vvdb-architecture opened this issue Jan 27, 2024 · 0 comments
Open

[Bug] Dispose() not called for singleton services #281

vvdb-architecture opened this issue Jan 27, 2024 · 0 comments
Labels
bug Something isn't working triage

Comments

@vvdb-architecture
Copy link

Context / Scenario

For some reason I have not been able to determine, the KernelMemory builder creates its own service provider in various places, i.e. here and here.

However, singleton services that implement IDisposable should be disposed when their service provider is disposed. Since the service providers created by the KernelMemory builder are never disposed, the singleton's Dispose() is never called.

The problem is mitigated somewhat by the fact that many services are added to the service collection as direct instances: in that case, it's up to the creator of those instances to dispose them. This is documented (here)[https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines#disposal-of-services].

But having a mechanism that does this at the right time (i.e. when using var host = hostBuilder.Build(); leaves the scope) is needlessly complicated and fragile.

What happened?

I've implemented an IMemoryDB that is attached to a Lucene.NET search engine. It implements IDisposable because the index writer needs to be disposed cleanly when the web service or console app is terminated. The Dispose() method is never called in the current implementation.

Since the IKernelMemory service is injected, I expected that at least its implementation would implement IDisposable and that it would dispose the necessary providers. That doesn't happen. The fix would be simple but there are two (!) instances of ServiceProvider created and I do not understand why.

Importance

a fix would make my life easier

Platform, Language, Versions

C#, Any platform/version

Relevant log output

N/A
@vvdb-architecture vvdb-architecture added bug Something isn't working triage labels Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage
Projects
None yet
Development

No branches or pull requests

1 participant