-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
async_hooks: no way to EmitDestroy on garbage collection with the high-level Embedder API #16153
Comments
+1 – the only thing we need to be careful about is that the API we expose here makes garbage collection of arbitrary objects visible. I guess there’s no way around that, though? Also, should /cc @nodejs/async_hooks |
As a non-expert on the innards of this, it would make sense if AsyncResource did it by default. That way GC remains hidden. Not sure if there should be an opt out method or not. (just check if it's already been emitted in the default implementation?) |
I don't see why we would make it optional. Either the users hold a reference because they want to call |
@AndreasMadsen The thing is, if |
We also need to be aware of the
Right. In the |
I’m labeling this |
I've got an initial draft of this, will try to open a PR either tomorrow or Monday at the latest. |
Another unintended consequence I was thinking of: if function parent() {
const resource = new AsyncResource('parent')
// setup resource
return resource.asyncId;
}
function child(triggerAsyncId) {
const resource = new AsyncResource('child', triggerAsyncId)
// setup resource
}
const triggerAsyncId = parent()
// parent::emitDestroy could be called
//
// ... some time later
child(triggerAsyncId) This is only an issue if the user holds only on the |
@AndreasMadsen Very good point, but since asyncIds are all There're two ways I see here:
Any other ways to solve that / opinions on these two ^? |
In cases where libraries create AsyncResources which may be emitting more events depending on usage, the only way to ensure that destroy is called properly is by calling it when the resource gets garbage collected. Fixes: #16153 PR-URL: #16998 Fixes: #16153 Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
In cases where libraries create AsyncResources which may be emitting more events depending on usage, the only way to ensure that destroy is called properly is by calling it when the resource gets garbage collected. Fixes: nodejs#16153 PR-URL: nodejs#16998 Backport-PR-URL: nodejs#18179 Fixes: nodejs#16153 Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
In cases where libraries create AsyncResources which may be emitting more events depending on usage, the only way to ensure that destroy is called properly is by calling it when the resource gets garbage collected. Backport-PR-URL: #18179 Fixes: #16153 PR-URL: #16998 Fixes: #16153 Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
Let's say I make a super simple
AsyncResource
, that lets me bind a function to always run in a particular executionId.I can use it with the following wrapper:
However, it seems I have no way to schedule cleanup. Let's say I do
I want
BoundFunction#emitDestroy()
to run once the GC grabs boundF. The docs seem to say that this is a normal approach to async resources, byHowever, as far as I can see the JS Embedder API gives me no way to achieve this behaviour.
The text was updated successfully, but these errors were encountered: