Skip to content

Commit c22b433

Browse files
mcollinajoyeecheung
andcommitted
async_hooks: runtime deprecate createHook()
Emit DEP0215 when async_hooks.createHook() is called, as the first step before putting it behind a flag. The inspector and the node.async_hooks trace event category use an internal createHook() and do not emit the warning. Refs: #61030 Signed-off-by: Matteo Collina <hello@matteocollina.com> Co-authored-by: Joyee Cheung <joyeec9h3@gmail.com> Assisted-by: OpenAI
1 parent ccf9d2d commit c22b433

79 files changed

Lines changed: 210 additions & 20 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Ždoc/api/async_hooks.mdβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,16 @@ function promiseResolve(asyncId) { }
148148

149149
<!-- YAML
150150
added: v8.1.0
151+
deprecated: REPLACEME
152+
changes:
153+
- version: REPLACEME
154+
pr-url: https://github.com/nodejs/node/pull/67271
155+
description: Runtime deprecation (DEP0215).
151156
-->
152157

158+
> Stability: 0 - Deprecated: Use [`AsyncLocalStorage`][] or
159+
> [Diagnostics Channel][] instead. See [DEP0215][].
160+
153161
* `options` {Object} The [Hook Callbacks][] to register
154162
* `init` {Function} The [`init` callback][].
155163
* `before` {Function} The [`before` callback][].
@@ -960,6 +968,7 @@ The documentation for this class has moved [`AsyncResource`][].
960968
The documentation for this class has moved [`AsyncLocalStorage`][].
961969
962970
[DEP0111]: deprecations.md#dep0111-processbinding
971+
[DEP0215]: deprecations.md#dep0215-async_hookscreatehook
963972
[Diagnostics Channel]: diagnostics_channel.md
964973
[Hook Callbacks]: #hook-callbacks
965974
[PromiseHooks]: https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk/edit

β€Ždoc/api/deprecations.mdβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4963,6 +4963,30 @@ certificates added with [`--use-system-ca`][] or [`NODE_EXTRA_CA_CERTS`][].
49634963
Use [`tls.getCACertificates('default')`][] to get the default CA certificates,
49644964
or `tls.getCACertificates('bundled')` to get the bundled Mozilla CA store.
49654965
4966+
### DEP0215: `async_hooks.createHook()`
4967+
4968+
<!-- YAML
4969+
changes:
4970+
- version: REPLACEME
4971+
pr-url: https://github.com/nodejs/node/pull/67271
4972+
description: Runtime deprecation.
4973+
-->
4974+
4975+
Type: Runtime
4976+
4977+
[`async_hooks.createHook()`][] is deprecated. The callbacks it registers run for
4978+
every asynchronous resource created in the process, which has a significant
4979+
performance cost, and they expose internal implementation details that change
4980+
between Node.js versions.
4981+
4982+
Use [`AsyncLocalStorage`][] to propagate state across asynchronous operations,
4983+
and [`diagnostics_channel`][] (in particular [`TracingChannel`][]) to observe
4984+
operations for tracing and monitoring. [`process.getActiveResourcesInfo()`][]
4985+
returns the types of the resources that keep the event loop alive.
4986+
4987+
`async_hooks.createHook()` may be put behind a command-line flag or removed in
4988+
a future version of Node.js.
4989+
49664990
[DEP0142]: #dep0142-repl_builtinlibs
49674991
[DEP0156]: #dep0156-aborted-property-and-abort-aborted-event-in-http
49684992
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
@@ -4977,6 +5001,7 @@ or `tls.getCACertificates('bundled')` to get the bundled Mozilla CA store.
49775001
[`--throw-deprecation`]: cli.md#--throw-deprecation
49785002
[`--unhandled-rejections`]: cli.md#--unhandled-rejectionsmode
49795003
[`--use-system-ca`]: cli.md#--use-system-ca
5004+
[`AsyncLocalStorage`]: async_context.md#class-asynclocalstorage
49805005
[`Buffer.allocUnsafeSlow(size)`]: buffer.md#static-method-bufferallocunsafeslowsize-alignment
49815006
[`Buffer.from(array)`]: buffer.md#static-method-bufferfromarray
49825007
[`Buffer.from(buffer)`]: buffer.md#static-method-bufferfrombuffer
@@ -4995,9 +5020,11 @@ or `tls.getCACertificates('bundled')` to get the bundled Mozilla CA store.
49955020
[`Server.getConnections()`]: net.md#servergetconnectionscallback
49965021
[`Server.listen({fd: <number>})`]: net.md#serverlistenhandle-backlog-callback
49975022
[`String.prototype.toWellFormed`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toWellFormed
5023+
[`TracingChannel`]: diagnostics_channel.md#class-tracingchannel
49985024
[`WriteStream.open()`]: fs.md#class-fswritestream
49995025
[`assert`]: assert.md
50005026
[`asyncResource.runInAsyncScope()`]: async_context.md#asyncresourceruninasyncscopefn-thisarg-args
5027+
[`async_hooks.createHook()`]: async_hooks.md#async_hookscreatehookoptions
50015028
[`buffer.subarray`]: buffer.md#bufsubarraystart-end
50025029
[`child_process.execFile`]: child_process.md#child_processexecfilefile-args-options-callback
50035030
[`child_process.exec`]: child_process.md#child_processexeccommand-options-callback
@@ -5017,6 +5044,7 @@ or `tls.getCACertificates('bundled')` to get the bundled Mozilla CA store.
50175044
[`crypto.scrypt()`]: crypto.md#cryptoscryptpassword-salt-keylen-options-callback
50185045
[`decipher.final()`]: crypto.md#decipherfinaloutputencoding
50195046
[`decipher.setAuthTag()`]: crypto.md#deciphersetauthtagbuffer-encoding
5047+
[`diagnostics_channel`]: diagnostics_channel.md
50205048
[`dirent.parentPath`]: fs.md#direntparentpath
50215049
[`dns.lookup()`]: dns.md#dnslookuphostname-options-callback
50225050
[`dnsPromises.lookup()`]: dns.md#dnspromiseslookuphostname-options

β€Žlib/async_hooks.jsβ€Ž

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const {
2222
ERR_INVALID_ARG_VALUE,
2323
} = require('internal/errors').codes;
2424
const {
25+
deprecate,
2526
kEmptyObject,
2627
} = require('internal/util');
2728
const {
@@ -167,9 +168,14 @@ class AsyncHook {
167168
}
168169

169170

170-
function createHook(fns) {
171-
return new AsyncHook(fns);
172-
}
171+
const createHook = deprecate(
172+
function createHook(fns) {
173+
return new AsyncHook(fns);
174+
},
175+
'async_hooks.createHook() is deprecated. Use AsyncLocalStorage for async ' +
176+
'context tracking and diagnostics_channel for tracing instead.',
177+
'DEP0215',
178+
);
173179

174180

175181
// Embedder API //

β€Žlib/internal/async_hooks.jsβ€Ž

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
'use strict';
22

33
const {
4+
ArrayPrototypeIncludes,
5+
ArrayPrototypeIndexOf,
6+
ArrayPrototypePush,
47
ArrayPrototypeSlice,
8+
ArrayPrototypeSplice,
59
ErrorCaptureStackTrace,
610
ObjectDefineProperty,
711
ObjectPrototypeHasOwnProperty,
@@ -592,9 +596,63 @@ function triggerAsyncId() {
592596

593597
const kNoPromiseHook = Symbol('kNoPromiseHook');
594598

599+
// A minimal AsyncHook for the inspector and the node.async_hooks trace event
600+
// category, so that they do not go through the deprecated public
601+
// async_hooks.createHook(). It does not validate its arguments, does not
602+
// support promiseResolve, and expects init, before, after and destroy to all
603+
// be functions.
604+
class InternalAsyncHook {
605+
constructor({ init, before, after, destroy, trackPromises = true }) {
606+
this[init_symbol] = init;
607+
this[before_symbol] = before;
608+
this[after_symbol] = after;
609+
this[destroy_symbol] = destroy;
610+
this[kNoPromiseHook] = !trackPromises;
611+
}
612+
613+
enable() {
614+
const { 0: hooks_array, 1: hook_fields } = getHookArrays();
615+
if (ArrayPrototypeIncludes(hooks_array, this))
616+
return this;
617+
618+
const prev_kTotals = hook_fields[kTotals];
619+
hook_fields[kTotals] = hook_fields[kInit] += 1;
620+
hook_fields[kTotals] += hook_fields[kBefore] += 1;
621+
hook_fields[kTotals] += hook_fields[kAfter] += 1;
622+
hook_fields[kTotals] += hook_fields[kDestroy] += 1;
623+
hook_fields[kTotals] += hook_fields[kPromiseResolve];
624+
ArrayPrototypePush(hooks_array, this);
625+
626+
if (prev_kTotals === 0)
627+
enableHooks();
628+
if (!this[kNoPromiseHook])
629+
updatePromiseHookMode();
630+
return this;
631+
}
632+
633+
disable() {
634+
const { 0: hooks_array, 1: hook_fields } = getHookArrays();
635+
const index = ArrayPrototypeIndexOf(hooks_array, this);
636+
if (index === -1)
637+
return this;
638+
639+
hook_fields[kTotals] = hook_fields[kInit] -= 1;
640+
hook_fields[kTotals] += hook_fields[kBefore] -= 1;
641+
hook_fields[kTotals] += hook_fields[kAfter] -= 1;
642+
hook_fields[kTotals] += hook_fields[kDestroy] -= 1;
643+
hook_fields[kTotals] += hook_fields[kPromiseResolve];
644+
ArrayPrototypeSplice(hooks_array, index, 1);
645+
646+
if (hook_fields[kTotals] === 0)
647+
disableHooks();
648+
return this;
649+
}
650+
}
651+
595652
module.exports = {
596653
executionAsyncId,
597654
triggerAsyncId,
655+
InternalAsyncHook,
598656
// Private API
599657
getHookArrays,
600658
symbols: {

β€Žlib/internal/inspector_async_hook.jsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ let config;
55

66
function lazyHookCreation() {
77
const inspector = internalBinding('inspector');
8-
const { createHook } = require('async_hooks');
8+
const { InternalAsyncHook } = require('internal/async_hooks');
99
config = internalBinding('config');
1010

11-
hook = createHook({
11+
hook = new InternalAsyncHook({
1212
init(asyncId, type, triggerAsyncId, resource) {
1313
// It's difficult to tell which tasks will be recurring and which won't,
1414
// therefore we mark all tasks as recurring. Based on the discussion

β€Žlib/internal/trace_events_async_hooks.jsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const {
1616
kSyncEnd,
1717
} = require('internal/trace_events');
1818
const async_wrap = internalBinding('async_wrap');
19-
const async_hooks = require('async_hooks');
19+
const async_hooks = require('internal/async_hooks');
2020

2121
const kTraceEventCategory = nodeTraceEventCategory('node.async_hooks');
2222

@@ -38,7 +38,7 @@ function createHook() {
3838
// repeated. Since async_hooks doesn't expose the provider type in the
3939
// non-init events, use a map to manually map the asyncId to the type name.
4040

41-
const hook = async_hooks.createHook({
41+
const hook = new async_hooks.InternalAsyncHook({
4242
init(asyncId, type, triggerAsyncId, resource) {
4343
if (nativeProviders.has(type)) return;
4444

β€Žtest/addons/async-resource/test.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Flags: --no-deprecation
12
'use strict';
23

34
const common = require('../../common');

β€Žtest/addons/callback-scope/test-async-hooks.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Flags: --no-deprecation
12
'use strict';
23

34
const common = require('../../common');

β€Žtest/async-hooks/test-async-await.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Flags: --no-deprecation
12
'use strict';
23
const common = require('../common');
34

β€Žtest/async-hooks/test-async-local-storage-gcable.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
// Flags: --expose_gc
2+
// Flags: --expose_gc --no-deprecation
33

44
// This test ensures that AsyncLocalStorage gets gced once it was disabled
55
// and no strong references remain in userland.

0 commit comments

Comments
Β (0)