Skip to content

Commit b632231

Browse files
puzpuzpuztargos
authored andcommitted
async_hooks: move to lazy destroy hook registration in AsyncResource
PR-URL: #32429 Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Vladimir de Turckheim <[email protected]>
1 parent 135f4b9 commit b632231

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

benchmark/async_hooks/gc-tracking.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
22
const common = require('../common.js');
3-
const { AsyncResource } = require('async_hooks');
3+
const { createHook, AsyncResource } = require('async_hooks');
44

55
const bench = common.createBenchmark(main, {
66
n: [1e6],
77
method: [
88
'trackingEnabled',
9+
'trackingEnabledWithDestroyHook',
910
'trackingDisabled',
1011
]
1112
}, {
@@ -30,6 +31,14 @@ function main({ n, method }) {
3031
}
3132
endAfterGC(n);
3233
break;
34+
case 'trackingEnabledWithDestroyHook':
35+
createHook({ destroy: () => {} }).enable();
36+
bench.start();
37+
for (let i = 0; i < n; i++) {
38+
new AsyncResource('foobar');
39+
}
40+
endAfterGC(n);
41+
break;
3342
case 'trackingDisabled':
3443
bench.start();
3544
for (let i = 0; i < n; i++) {

doc/api/async_hooks.md

+2
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,8 @@ asyncResource.triggerAsyncId();
685685
when the object is garbage collected. This usually does not need to be set
686686
(even if `emitDestroy` is called manually), unless the resource's `asyncId`
687687
is retrieved and the sensitive API's `emitDestroy` is called with it.
688+
When set to `false`, the `emitDestroy` call on garbage collection
689+
will only take place if there is at least one active `destroy` hook.
688690
**Default:** `false`.
689691

690692
Example usage:

lib/async_hooks.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const {
3636
emitDestroy,
3737
enabledHooksExist,
3838
initHooksExist,
39+
destroyHooksExist,
3940
} = internal_async_hooks;
4041

4142
// Get symbols
@@ -168,7 +169,7 @@ class AsyncResource {
168169
emitInit(asyncId, type, triggerAsyncId, this);
169170
}
170171

171-
if (!requireManualDestroy) {
172+
if (!requireManualDestroy && destroyHooksExist()) {
172173
// This prop name (destroyed) has to be synchronized with C++
173174
const destroyed = { destroyed: false };
174175
this[destroyedSymbol] = destroyed;

0 commit comments

Comments
 (0)