typedi
typedi copied to clipboard
question: How do you destroy a scoped container?
I'm currently using typedi in an HTTP application and I'm creating a scoped container per request to ensure there's no bleed from other requests that are in flight.
At the end of the request I call .reset() on the scoped container and I also set the scoped container to null. However, when profiling the application with Chrome Dev Tools I can see that the container instances are still in memory and even after calling .reset() I'm still able to retrieve different container instances back with .get().
I'm just wondering if I'm missing something or perhaps my usage of typedi is incorrect?
Hi @mzyrc, which version are you using?
Hey @attilaorosz
I'm using 0.10.0 with node 16.13.2
Hi @attilaorosz
Any update on this?
@mzyrc sorry for the late reply. Could you please provide a repro repo to see how you create the scoped container?
No problem at all!
Here's a link to a spike I've put together that simulates the issue - https://github.com/mzyrc/typedi-memory-leak
Let me know if I can provide anymore information or if anything is unclear
@mzyrc Thanks for the detailed report! So it seems like the container registry is not released yet so unfortunately there is no official way to dispose of a container (that i know of). There is a workaround tho:
const index = Container['instances'].findIndex((instance: any) => instance.id === containerId);
if (index > -1) {
Container['instances'].splice(index, 1);
}
This is obviously very hacky, but if you have a critical project needing this right now, there you go.
@NoNameProvided Do you have a schedule for the next release?
Hi @attilaorosz
Thanks for such a quick turn around, I can confirm the workaround worked great 👍
If I can help at all with the upcoming release I'd be happy to lend a hand
@attilaorosz @NoNameProvided is this fixed in the latest version ? Facing a similar issue.
Will doing Container.reset(<myScopedContainerId>) help ?
I see that it has a similar code.