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

Harden AsyncHooksScopeManager regarding faulty use of AsyncResources #591

Closed
Flarna opened this issue Dec 5, 2019 · 16 comments
Closed

Harden AsyncHooksScopeManager regarding faulty use of AsyncResources #591

Flarna opened this issue Dec 5, 2019 · 16 comments
Labels
feature-request up-for-grabs Good for taking. Extra help will be provided by maintainers

Comments

@Flarna
Copy link
Member

Flarna commented Dec 5, 2019

Is your feature request related to a problem? Please describe.
Some libraries may implement misbehaving AsyncResources for which the destroy callback won't be ever called. This would result in a memory leak in AsyncHooksScopeManager as the corresponding entry is never deleted from _scopes.
Even the problem/bug is actually in the faulty use of AsyncResources the leak may only appear if OTel is used in such an application.

inspired by nodejs/node#26540 (comment)

Describe the solution you'd like
Add some counter measures to detect such leaks and do a cleanup of old/outdated entries.

Describe alternatives you've considered
Only document that memory leaks like this could happen but root cause is somewhere else and needs to be handled somewhere else.

Additional context
I think this is more a discussion then a feature request but there exists on discussion type for issues and my feeling is that feature request fits better then bug.

@mayurkale22
Copy link
Member

/cc @vmarchaud

@mayurkale22 mayurkale22 added the up-for-grabs Good for taking. Extra help will be provided by maintainers label Dec 5, 2019
@vmarchaud
Copy link
Member

Indeed thats problematic, i believe we could use weakmaps to never old reference but i'm not sure. I don't know if it's that much important for alpha/beta but we need to have something in place for GA for sure

@michaelgoin
Copy link
Contributor

Assuming this is still wanted/looking to be handled, I'm willing to take a stab at it likely along with @lykkin.

@dyladan
Copy link
Member

dyladan commented Jun 11, 2020

You're welcome to take a shot :)

@michaelgoin
Copy link
Contributor

After getting reacquainted with the state of things... In Node 14, and back ported to v12.17, executionAsyncResource was introduced to async-hooks. This allows storing of context data on the resource representing the current execution. A big benefit being it enables storing of context data without needing to maintain a map locally that relies on seeing destroy to cleanup. By avoiding this external collection, a class of memory leak is no-longer of concern. This is leveraged by the new async-storage API that landed in these versions. It also has the potential to remove the need for additional hooks, providing a potential perf boost.

It appears, looking at our AsyncHooksContextManager, that executionAsyncResource should work. There might be some awkwardness with the current stack design, but will have to see. Before digging too deep, I wanted to get thoughts on this potential approach and Node version support. It seems, for a community project like this, sticking to current or upcoming paved paths may be more long-term beneficial than trying to force in a solution. Particularly for something like this where something has already gone bad with end-user code.

Would we want to look into having a version of the context manager with this solved just for Node ^12.17 and >= 14? Would we prefer to defer this work until Node 10 is dropped and it is more likely to benefit more users? If defer, do we still want to do some prototyping or should I move into other work that may be more high priority for GA?

@michaelgoin
Copy link
Contributor

@dyladan just pinging you on the above in case you didn't get a notification (not pressuring, know you are probably busy - just for visibility).

@vmarchaud
Copy link
Member

For me, we should let the current AsyncHooksScopeManager as-is and focus on making a new one based on AsyncLocalStorage since its way more performant than the implementation i have wrote. See #1210

@michaelgoin
Copy link
Contributor

I think that makes sense. Some of the benefit comes from what I was mentioning above but beyond that... using what is hoped as the future of such context tracking for Node would potentially have mutual benefit for this project and Node.

@dyladan
Copy link
Member

dyladan commented Jun 23, 2020

I agree with @vmarchaud that the much safer way to handle this is to introduce it as a new context manager. After we have some use and have worked out the bugs, we can see about making it the default later.

@michaelgoin
Copy link
Contributor

OK, sounds good. I'm guessing that's where y'all will steer #1210? If so, I'm happy to concede this issue if that makes sense and grab another item (feel free to suggest).

@vmarchaud
Copy link
Member

/cc @johanneswuerbach since you opened the PR, do you plan into working on a AsyncLocalStorage-based ContextManager ?

@johanneswuerbach
Copy link
Contributor

Yes, I actually wanted to work on that today 😂

@vmarchaud
Copy link
Member

I think we can close this issue, maybe re-open a new one for the AsyncLocalStorageContextManager, WDYT ?

@michaelgoin I would suggest to try to tackle #1040

@dyladan
Copy link
Member

dyladan commented Jun 23, 2020

I really want #1040 to be done. I started work on it once before but got busy doing other things. If you want it @michaelgoin comment on the issue and i'll assign you.

@michaelgoin michaelgoin removed their assignment Jun 25, 2020
@johanneswuerbach
Copy link
Contributor

As #1344 and #1040 are merged now I guess this can be closed?

@vmarchaud
Copy link
Member

Well i guess. One question remains open i believe: do we want to enable the AsyncStorageContextManager by default with newer version of node 14 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request up-for-grabs Good for taking. Extra help will be provided by maintainers
Projects
None yet
Development

No branches or pull requests

6 participants