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

question: How do you destroy a scoped container? #739

Open
mzyrc opened this issue May 9, 2022 · 8 comments
Open

question: How do you destroy a scoped container? #739

mzyrc opened this issue May 9, 2022 · 8 comments
Labels
type: question Questions about the usage of the library.

Comments

@mzyrc
Copy link

mzyrc commented May 9, 2022

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 mzyrc added the type: question Questions about the usage of the library. label May 9, 2022
@attilaorosz
Copy link
Member

Hi @mzyrc, which version are you using?

@mzyrc
Copy link
Author

mzyrc commented May 9, 2022

Hey @attilaorosz

I'm using 0.10.0 with node 16.13.2

@mzyrc
Copy link
Author

mzyrc commented May 16, 2022

Hi @attilaorosz

Any update on this?

@attilaorosz
Copy link
Member

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

@mzyrc
Copy link
Author

mzyrc commented May 17, 2022

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

@attilaorosz
Copy link
Member

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

@mzyrc
Copy link
Author

mzyrc commented May 17, 2022

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

@97amarnathk
Copy link

97amarnathk commented Jul 26, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Questions about the usage of the library.
Development

No branches or pull requests

3 participants