JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
563 stars 118 forks source link

Singleton registered service that is wrapped in a Decorator is being disposed #357

Closed RickBlouch closed 1 year ago

RickBlouch commented 1 year ago

I believe I may have found a bug where a Singleton registered service that is wrapped in a Decorator is being disposed when I don't think it should be.

I forked the Lamar repo and made an effort on a fix, but I do not know this codebase very well and if this is the right spot for a change (assuming this is a bug and not intended) or what other impacts this change could have. That being said, I ran all tests after the change and all are green.

You can see a commit here on my fork with the reproduction code / unit test and proposed change: https://github.com/RickBlouch/lamar/commit/8df2c30c9a43547354b6063e5ad33a094aedc0c0. I can clean up the test code and comments if this ends up being an accepted fix.

When looking at the reproduction code, you might be wondering why we are using a nested container within a nested container. In my real world app, the scenario is in an Azure WebJob SDK project where the WebJob SDK is creating the first nested container prior to a timer / bus trigger, then our logic is creating a nested container per tenant while doing some job processing.

I do not like to use third party tools in a way not intended by the author, so I am wondering if this is an intended usage or if this is outside of how it's meant to work? I want to work towards correcting our usage if so.

Lastly, can you think of any issues we might run into by using the workaround of resolving the ISingleton instance very early on in the app so it is cached by Lamar and not disposed when used in a nested container?

jeremydmiller commented 1 year ago

"Lastly, can you think of any issues we might run into by using the workaround of resolving the ISingleton instance very early on in the app so it is cached by Lamar and not disposed when used in a nested container?"

Just the normal issues. Is it thread safe? Is it stateless, or not? Nothing specific to being used in the container per se.

RickBlouch commented 1 year ago

Thanks for the update, and that makes sense. I can make a PR with the proposed change or if you don't think that is the correct change I can dig deeper (any pointers in the right direction would be helpful). If you aren't interested in supporting this nested container within a nested container scenario I understand as well. Thanks again!

RickBlouch commented 1 year ago

Just noting that this commit resolves this issue. I have a related but different scenario that I'll open a separate ticket on for tracking.

https://github.com/JasperFx/lamar/commit/d64890c704a5810603d46a2800b6e6697c27a1df#diff-c4c85257269f04f4c381366cf58c6abaeda1b65326fee609ae101149e0cf9040