typedi icon indicating copy to clipboard operation
typedi copied to clipboard

question: How do you destroy a scoped container?

Open mzyrc opened this issue 2 years ago • 8 comments

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?

mzyrc avatar May 09 '22 13:05 mzyrc

Hi @mzyrc, which version are you using?

attilaorosz avatar May 09 '22 13:05 attilaorosz

Hey @attilaorosz

I'm using 0.10.0 with node 16.13.2

mzyrc avatar May 09 '22 13:05 mzyrc

Hi @attilaorosz

Any update on this?

mzyrc avatar May 16 '22 07:05 mzyrc

@mzyrc sorry for the late reply. Could you please provide a repro repo to see how you create the scoped container?

attilaorosz avatar May 17 '22 04:05 attilaorosz

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 avatar May 17 '22 17:05 mzyrc

@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?

attilaorosz avatar May 17 '22 18:05 attilaorosz

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

mzyrc avatar May 17 '22 19:05 mzyrc

@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.

97amarnathk avatar Jul 26 '23 08:07 97amarnathk