-
-
Notifications
You must be signed in to change notification settings - Fork 837
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
OnRelease not triggered if Provided Instance service has never been resolved #1102
Comments
Hmmm; looking at the
Because the service is never activated, it is never released. It feels that perhaps we should imply AutoActivate for provided instances (or something like that). |
Ok, so it looks like if we add a new AutoActivate service in OnRelease if any Activating or Activated handlers have been attached, then the OnRelease will fire correctly when the container is disposed. I'll look at putting a PR together. |
Oh, and thanks @arialdomartini for the detailed issue report. |
There's a bit of history here. #383 has a similar discussion and there's been discussion (though I can't find it now... maybe the Google Group?) where there's a sort of question around how Autofac should deal with provided instances - Autofac should clearly dispose of things it creates; but if you create the thing then you own it. Should Autofac dispose out from under you? From the docs it appears we settled on requiring I'm not sure I'd |
I see what you mean, thanks for the reference; I suppose in a way, having an OnRelease for a provided instance is just a replacement for releasing manually when you dispose of the container... But if you don't have access to the code at the point the container is released (e.g. in ASP.NET core) or the type does not implement dispose, and you cannot change it, what is the suggested behaviour? Register a decorator for the service that provides a Dispose method and put OnRelease behaviour in there? |
If the type doesn't implement The only way the container gets disposed is by having access to that point in the code. Even in ASP.NET Core you can subscribe to a shutdown event to handle disposing things. So your options would be...
That said, it's somewhat moot. It seems like the decision made way back when, for consistency, was to treat provided instances as something we need to dispose as part of the container so it's consistent with the rest of things in the container. Apparently that's not happening, though I'm not sure why it changed; possibly something to do with the immutable container updates? |
The crux of this specific issue is that:
This last point definitely feels like it introduces a contradiction, where one form of clean-up works in both cases (Dispose), and another form of clean-up (OnRelease), which we document on the same page as Dispose as being a equatable alternative, only works in one of them.
This is not a new problem, it appears to have been around for some time based on the code history. Just a side effect of using the Activating handlers. |
Yeah, sounds like we need a fix. |
Fixes #1102 - AutoActivate Instance Registrations if they have Activation/ed handlers
@ale7canna and I noticed a weird behavior regarding the disposal of components registered with
RegisterInstance()
in combination with the use ofOnRelease()
.We know from the documentation about Provided Instances that
This is confirmed by the next test, which shows how Autofac automatically disposes of the
MyClass
instance during the container disposal:This works even if
MyClass
is actually never resolved, that is if the lineis commented out.
So far so good.
We know from OnRelease that:
So, if we register
MyClass
instance providing the registration with anOnRelease
callback, Autofac will stop invokingDispose
on the Container disposal.In the next test, we use
OnRelease
to manually invokeDispose
whenOnRelease
is triggered.Strangely, and differently from the previous case, if the service is never resolved,
Dispose
is in fact not called. The following test fails:This means that the clean up of
MyClass
instance is never performed:Dispose
is not called, because the automatic disposal of the provided instance has been disabled by the use ofOnRelease
and replaced by the provided callback;OnRelease
is never triggered, because no instance ofMyClass
service has never been requested.Consequently, the instance is never cleaned up, potentially leading to a resource leak.
We are not sure this is a bug, but it sounds a bit confusing. Both v4 and v5 have the same behavior.
The text was updated successfully, but these errors were encountered: