GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — async_hooks: move to lazy destroy hook registration in AsyncResource · nodejs/node@b632231

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • benchmark/async_hooks
    • gc-tracking.js
  • doc/api
    • async_hooks.md
  • lib
    • async_hooks.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎benchmark/async_hooks/gc-tracking.js‎

Copy file name to clipboardExpand all lines: benchmark/async_hooks/gc-tracking.js
+10-1Lines changed: 10 additions & 1 deletion
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++) {
Collapse file

‎doc/api/async_hooks.md‎

Copy file name to clipboardExpand all lines: doc/api/async_hooks.md
+2Lines changed: 2 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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:
Collapse file

‎lib/async_hooks.js‎

Copy file name to clipboardExpand all lines: lib/async_hooks.js
+2-1Lines changed: 2 additions & 1 deletion
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 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.