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

async_hooks: move to lazy destroy hook registration in AsyncResource #32429

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion benchmark/async_hooks/gc-tracking.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';
const common = require('../common.js');
const { AsyncResource } = require('async_hooks');
const { createHook, AsyncResource } = require('async_hooks');

const bench = common.createBenchmark(main, {
n: [1e6],
method: [
'trackingEnabled',
'trackingEnabledWithDestroyHook',
'trackingDisabled',
]
}, {
Expand All @@ -30,6 +31,14 @@ function main({ n, method }) {
}
endAfterGC(n);
break;
case 'trackingEnabledWithDestroyHook':
createHook({ destroy: () => {} }).enable();
bench.start();
for (let i = 0; i < n; i++) {
new AsyncResource('foobar');
}
endAfterGC(n);
break;
case 'trackingDisabled':
bench.start();
for (let i = 0; i < n; i++) {
Expand Down
2 changes: 2 additions & 0 deletions doc/api/async_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,8 @@ asyncResource.triggerAsyncId();
when the object is garbage collected. This usually does not need to be set
(even if `emitDestroy` is called manually), unless the resource's `asyncId`
is retrieved and the sensitive API's `emitDestroy` is called with it.
When set to `false`, the `emitDestroy` call on garbage collection
will only take place if there is at least one active `destroy` hook.
**Default:** `false`.

Example usage:
Expand Down
3 changes: 2 additions & 1 deletion lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const {
emitDestroy,
enabledHooksExist,
initHooksExist,
destroyHooksExist,
} = internal_async_hooks;

// Get symbols
Expand Down Expand Up @@ -168,7 +169,7 @@ class AsyncResource {
emitInit(asyncId, type, triggerAsyncId, this);
}

if (!requireManualDestroy) {
if (!requireManualDestroy && destroyHooksExist()) {
// This prop name (destroyed) has to be synchronized with C++
const destroyed = { destroyed: false };
this[destroyedSymbol] = destroyed;
Expand Down