-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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 promise not destroyed #14446
Comments
Promises are only destroyed by the V8 garbage collector. If you give the process some time it should eventually destroy them. |
Yes, exactly what @AndreasMadsen said. (Generally avoid holding onto any resource of handle for an indeterminate amount of time.) |
const async_hooks = require('async_hooks');
let map = new Map();
function init(asyncId, type, triggerId, resource) {
let emptyObj = {};
Error.captureStackTrace(emptyObj, init);
map.set(asyncId,emptyObj.stack)
}
function before(asyncId) {
global.longStack = map.get(asyncId);
}
function after(asyncId) {
}
function destroy(asyncId) {
map.delete(asyncId);
}
async_hooks.createHook({init, before, after, destroy}).enable();
debugger;
const parent_promise = new Promise((resolve, reject) => {resolve(5);});
const promise = parent_promise.then((val) => {return val;}); i want to implement a 'long stack trace' using async hooks like this:
how can i delete the stack in the map? (not using weakmap,because
in nodejs api doc) |
In terms of PS: You should be aware that before and after can be called multiple times in the same tick. (e.g. You can look at my long stack trace (https://github.com/AndreasMadsen/trace/blob/master/trace.js) for more inspiration. |
@AndreasMadsen Thanks for your info:
i've inspected your implementation and gained much.
i got the same result: |
my worries in this issue do not exist!!! thanks @AndreasMadsen The reason is:
|
While it doesn't make any difference now. In the future PromiseHooks could be refactored to provide an asyncId instead of the promise object. That would make escape analysis on promises possible. Escape analysis on promises could lead to a more efficient destroy hook, if provide by PromiseHooks as well. But at the very least would allow the destroy hook to be emitted earlier. The destroy hook not being emitted on promises frequent enough is a known and reported issue. See nodejs#14446 and Jeff-Lewis/cls-hooked#11. While all this is speculation for now, it all depends on the promise object not being a part of the PromiseWrap resource object. Ref: nodejs#14446 Ref: nodejs/diagnostics#188
While it doesn't make any difference now. In the future PromiseHooks could be refactored to provide an asyncId instead of the promise object. That would make escape analysis on promises possible. Escape analysis on promises could lead to a more efficient destroy hook, if provide by PromiseHooks as well. But at the very least would allow the destroy hook to be emitted earlier. The destroy hook not being emitted on promises frequent enough is a known and reported issue. See #14446 and Jeff-Lewis/cls-hooked#11. While all this is speculation for now, it all depends on the promise object not being a part of the PromiseWrap resource object. Ref: #14446 Ref: nodejs/diagnostics#188 PR-URL: #23443 Refs: #14446 Refs: nodejs/diagnostics#188 Reviewed-By: Benedikt Meurer <benedikt.meurer@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: George Adams <george.adams@uk.ibm.com>
my code is like follows:
the output is:
why the Promise async resources' destroy callback not triggered?
The text was updated successfully, but these errors were encountered: