From 411591301c7ca61acc354fdce4976faf781c3192 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 12 Dec 2025 13:02:25 +0100 Subject: [PATCH 1/8] async_hooks: add --enable-async-hooks flag Add a new `--enable-async-hooks` flag that must be passed to enable `async_hooks.createHook()`. This is a breaking change - createHook is now disabled by default. When createHook() is called without the flag, it throws ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED with a message indicating that the flag is required. This change affects all code using async_hooks.createHook(), including APM tools and debugging utilities. Users must now explicitly opt-in to this functionality. BREAKING CHANGE: async_hooks.createHook() now requires the `--enable-async-hooks` flag. Signed-off-by: Matteo Collina --- doc/api/cli.md | 41 ++++++++++ doc/api/errors.md | 13 +++ doc/node.1 | 32 ++++++++ lib/async_hooks.js | 5 ++ lib/internal/async_hooks.js | 79 +++++++++++++++++++ lib/internal/errors.js | 2 + lib/internal/inspector_async_hook.js | 2 +- src/node_options.cc | 4 + src/node_options.h | 1 + test/addons/async-resource/test.js | 1 + .../addons/callback-scope/test-async-hooks.js | 1 + test/async-hooks/init-hooks.js | 2 +- test/async-hooks/test-async-await.js | 1 + .../test-async-exec-resource-http-32060.js | 1 + .../test-async-exec-resource-http-agent.js | 1 + .../test-async-exec-resource-http.js | 1 + .../test-async-exec-resource-match.js | 1 + .../test-async-local-storage-gcable.js | 2 +- test/async-hooks/test-callback-error.js | 9 ++- test/async-hooks/test-crypto-pbkdf2.js | 1 + test/async-hooks/test-crypto-randomBytes.js | 1 + test/async-hooks/test-destroy-not-blocked.js | 2 +- test/async-hooks/test-disable-in-init.js | 1 + ...est-embedder.api.async-resource-no-type.js | 3 +- .../test-embedder.api.async-resource.js | 1 + .../test-emit-after-on-destroyed.js | 4 +- test/async-hooks/test-emit-before-after.js | 2 +- .../test-emit-before-on-destroyed.js | 4 +- test/async-hooks/test-emit-init.js | 2 +- test/async-hooks/test-enable-disable.js | 1 + test/async-hooks/test-enable-in-init.js | 1 + test/async-hooks/test-filehandle-no-reuse.js | 1 + test/async-hooks/test-fseventwrap.js | 1 + test/async-hooks/test-fsreqcallback-access.js | 1 + .../test-fsreqcallback-readFile.js | 1 + test/async-hooks/test-getaddrinforeqwrap.js | 1 + test/async-hooks/test-getnameinforeqwrap.js | 1 + test/async-hooks/test-graph.fsreq-readFile.js | 1 + test/async-hooks/test-graph.http.js | 1 + test/async-hooks/test-graph.intervals.js | 1 + test/async-hooks/test-graph.pipe.js | 1 + test/async-hooks/test-graph.pipeconnect.js | 1 + test/async-hooks/test-graph.shutdown.js | 1 + test/async-hooks/test-graph.signal.js | 1 + test/async-hooks/test-graph.statwatcher.js | 1 + test/async-hooks/test-graph.tcp.js | 1 + test/async-hooks/test-graph.timeouts.js | 1 + test/async-hooks/test-graph.tls-write-12.js | 1 + test/async-hooks/test-graph.tls-write.js | 1 + .../test-http-agent-handle-reuse-parallel.js | 2 +- .../test-http-agent-handle-reuse-serial.js | 2 +- test/async-hooks/test-httpparser-reuse.js | 1 + test/async-hooks/test-httpparser.request.js | 2 +- test/async-hooks/test-httpparser.response.js | 2 +- test/async-hooks/test-immediate.js | 1 + test/async-hooks/test-improper-order.js | 4 +- test/async-hooks/test-improper-unwind.js | 4 +- test/async-hooks/test-late-hook-enable.js | 1 + .../test-nexttick-default-trigger.js | 1 + test/async-hooks/test-pipeconnectwrap.js | 1 + test/async-hooks/test-pipewrap.js | 2 +- ...promise.chain-promise-before-init-hooks.js | 1 + test/async-hooks/test-promise.js | 1 + .../test-promise.promise-before-init-hooks.js | 1 + test/async-hooks/test-querywrap.js | 2 +- test/async-hooks/test-queue-microtask.js | 1 + test/async-hooks/test-shutdownwrap.js | 1 + test/async-hooks/test-signalwrap.js | 1 + test/async-hooks/test-statwatcher.js | 1 + test/async-hooks/test-tcpwrap.js | 1 + test/async-hooks/test-timers.setInterval.js | 1 + test/async-hooks/test-timers.setTimeout.js | 1 + test/async-hooks/test-tlswrap.js | 1 + .../test-track-promises-default.js | 1 + .../test-track-promises-false-check.js | 2 +- test/async-hooks/test-track-promises-false.js | 1 + test/async-hooks/test-track-promises-true.js | 1 + .../test-track-promises-validation.js | 1 + test/async-hooks/test-ttywrap.readstream.js | 1 + test/async-hooks/test-udpsendwrap.js | 2 +- test/async-hooks/test-udpwrap.js | 1 + .../test-unhandled-exception-valid-ids.js | 1 + .../test-unhandled-rejection-context.js | 1 + test/async-hooks/test-writewrap.js | 1 + .../test-zlib.zlib-binding.deflate.js | 2 +- .../test-vm-compile-function-leak.js | 2 +- .../test-vm-contextified-script-leak.js | 2 +- .../test-vm-source-text-module-leak.js | 2 +- .../test-vm-synthetic-module-leak.js | 2 +- test/node-api/test_async/test-async-hooks.js | 1 + .../test-gcable-callback.js | 2 +- test/node-api/test_async_context/test.js | 2 +- .../test_callback_scope/test-async-hooks.js | 1 + .../test_make_callback/test-async-hooks.js | 1 + test/parallel/test-async-hooks-async-await.js | 1 + ...t-async-hooks-asyncresource-constructor.js | 1 + .../test-async-hooks-close-during-destroy.js | 1 + test/parallel/test-async-hooks-constructor.js | 1 + ...ync-hooks-correctly-switch-promise-hook.js | 1 + ...nc-hooks-create-hook-disabled-internals.js | 38 +++++++++ .../test-async-hooks-create-hook-disabled.js | 15 ++++ .../test-async-hooks-create-hook-enabled.js | 18 +++++ .../test-async-hooks-destroy-on-gc.js | 2 +- ...test-async-hooks-disable-during-promise.js | 1 + .../test-async-hooks-disable-gc-tracking.js | 2 +- ...ync-hooks-enable-before-promise-resolve.js | 1 + .../test-async-hooks-enable-disable-enable.js | 1 + .../test-async-hooks-enable-disable.js | 1 + .../test-async-hooks-enable-during-promise.js | 1 + .../test-async-hooks-enable-recursive.js | 1 + .../test-async-hooks-enabledhooksexits.js | 2 +- ...nc-hooks-execution-async-resource-await.js | 1 + ...st-async-hooks-execution-async-resource.js | 1 + test/parallel/test-async-hooks-fatal-error.js | 3 +- .../test-async-hooks-http-agent-destroy.js | 2 +- test/parallel/test-async-hooks-http-agent.js | 2 +- .../test-async-hooks-http-parser-destroy.js | 1 + ...test-async-hooks-prevent-double-destroy.js | 2 +- ...test-async-hooks-promise-enable-disable.js | 1 + .../test-async-hooks-promise-triggerid.js | 1 + test/parallel/test-async-hooks-promise.js | 1 + ...c-hooks-recursive-stack-runInAsyncScope.js | 1 + ...oks-run-in-async-scope-caught-exception.js | 1 + ...async-hooks-run-in-async-scope-this-arg.js | 1 + ...async-hooks-stack-overflow-nested-async.js | 2 +- ...st-async-hooks-stack-overflow-try-catch.js | 2 +- .../test-async-hooks-stack-overflow.js | 2 +- ...st-async-hooks-top-level-clearimmediate.js | 1 + test/parallel/test-async-hooks-vm-gc.js | 2 +- ...-async-hooks-worker-asyncfn-terminate-1.js | 1 + ...-async-hooks-worker-asyncfn-terminate-2.js | 1 + ...-async-hooks-worker-asyncfn-terminate-3.js | 1 + ...-async-hooks-worker-asyncfn-terminate-4.js | 1 + ...st-async-local-storage-http-parser-leak.js | 2 +- test/parallel/test-async-wrap-constructor.js | 1 + test/parallel/test-async-wrap-destroyid.js | 2 +- .../test-async-wrap-pop-id-during-load.js | 1 + .../test-async-wrap-promise-after-enabled.js | 1 + .../test-async-wrap-tlssocket-asyncreset.js | 1 + test/parallel/test-async-wrap-trigger-id.js | 1 + .../test-async-wrap-uncaughtexception.js | 1 + test/parallel/test-cli-node-options.js | 4 +- test/parallel/test-common-gc.js | 2 +- .../test-diagnostics-channel-memory-leak.js | 2 +- .../test-emit-after-uncaught-exception.js | 1 + .../test-eventemitter-asyncresource.js | 1 + ...t-fs-cp-async-destination-appears-late.mjs | 1 + .../test-fs-existssync-memleak-longpath.js | 2 +- .../test-fs-readfile-one-roundtrip.js | 1 + .../test-gc-http-client-connaborted.js | 2 +- test/parallel/test-gc-net-timeout.js | 2 +- test/parallel/test-gc-tls-external-memory.js | 2 +- .../test-heapdump-async-hooks-init-promise.js | 2 +- .../test-http-agent-domain-reused-gc.js | 2 +- ...-http-client-leaky-with-double-response.js | 2 +- ...t-http-server-connections-checking-leak.js | 2 +- .../test-http-server-keepalive-req-gc.js | 2 +- ...est-http-uncaught-from-request-callback.js | 1 + test/parallel/test-http2-debug.js | 1 + test/parallel/test-http2-ping.js | 1 + ...-https-server-connections-checking-leak.js | 2 +- test/parallel/test-inspector-contexts.js | 2 +- .../test-inspector-debug-async-hook.js | 1 + .../test-internal-util-weakreference.js | 2 +- test/parallel/test-messageport-hasref.js | 1 + test/parallel/test-net-connect-memleak.js | 2 +- test/parallel/test-primitive-timer-leak.js | 2 +- .../parallel/test-promise-hook-create-hook.js | 1 + ...est-queue-microtask-uncaught-asynchooks.js | 1 + test/parallel/test-shadow-realm-gc-module.js | 2 +- test/parallel/test-shadow-realm-gc.js | 2 +- .../test-source-map-cjs-require-cache.js | 2 +- test/parallel/test-sqlite-session.js | 2 +- ...-stream-finished-bindAsyncResource-path.js | 2 +- .../test-stream-writable-samecb-singletick.js | 1 + test/parallel/test-tls-connect-memleak.js | 2 +- .../test-trace-events-async-hooks-dynamic.js | 2 +- .../test-trace-events-perfetto-pftrace.js | 2 +- test/parallel/test-util-debounce.js | 2 +- test/parallel/test-util-throttle.js | 2 +- test/parallel/test-v8-coverage.js | 1 + test/parallel/test-v8-serialize-leak.js | 2 +- test/parallel/test-worker-hasref.js | 1 + ...r-message-port-inspect-during-init-hook.js | 1 + .../test-worker-messageport-hasref.js | 1 + .../test-zlib-invalid-input-memory.js | 2 +- .../test-async-wrap-getasyncid-tty.js | 2 +- test/sequential/test-async-wrap-getasyncid.js | 2 +- .../sequential/test-gc-http-client-onerror.js | 2 +- .../sequential/test-gc-http-client-timeout.js | 2 +- test/sequential/test-gc-http-client.js | 2 +- test/trace_events/test-trace-events-all.js | 2 +- .../test-trace-events-async-hooks-worker.js | 2 +- .../test-trace-events-async-hooks.js | 2 +- .../test-trace-events-file-pattern.js | 1 + .../test-trace-events-process-exit.js | 2 +- 196 files changed, 444 insertions(+), 86 deletions(-) create mode 100644 test/parallel/test-async-hooks-create-hook-disabled-internals.js create mode 100644 test/parallel/test-async-hooks-create-hook-disabled.js create mode 100644 test/parallel/test-async-hooks-create-hook-enabled.js diff --git a/doc/api/cli.md b/doc/api/cli.md index bddd4a5c0a5..b6e05e57316 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -139,6 +139,46 @@ Error: Cannot load native addon because loading addons is disabled. } ``` +### `--enable-async-hooks` + + + +Enable use of `async_hooks.createHook()`. This flag is required to enable +the `createHook` API, which is disabled by default. + +Example: + +```mjs +import { createHook } from 'node:async_hooks'; + +createHook({ init() {} }).enable(); +``` + +```cjs +const { createHook } = require('node:async_hooks'); + +createHook({ init() {} }).enable(); +``` + +```console +$ node index.js +node:async_hooks:160 + throw new ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED(); + ^ + +Error [ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED]: async_hooks.createHook() is disabled. Use --enable-async-hooks to enable it. + at createHook (node:async_hooks:160:11) + at Object. (/home/user/index.js:3:1) + ... +``` + +```console +$ node --enable-async-hooks index.js +# Works without error +``` + ### `--allow-child-process` * `--allow-addons` +* `--enable-async-hooks` * `--allow-child-process` * `--allow-env` * `--allow-ffi` diff --git a/doc/api/errors.md b/doc/api/errors.md index 2a1e423d10d..c25838ca7b6 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -733,6 +733,17 @@ by the `node:assert` module. An attempt was made to register something that is not a function as an `AsyncHooks` callback. + + +### `ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED` + + + +An attempt was made to call [`async_hooks.createHook()`][] without the +[`--enable-async-hooks`][] flag. + ### `ERR_ASYNC_LOADER_REQUEST_NEVER_SETTLED` @@ -4773,6 +4784,7 @@ An error occurred trying to allocate memory. This should never happen. [`"exports"`]: packages.md#exports [`"imports"`]: packages.md#imports [`'uncaughtException'`]: process.md#event-uncaughtexception +[`--enable-async-hooks`]: cli.md#--enable-async-hooks [`--disable-proto=throw`]: cli.md#--disable-protomode [`--force-fips`]: cli.md#--force-fips [`--no-addons`]: cli.md#--no-addons @@ -4798,6 +4810,7 @@ An error occurred trying to allocate memory. This should never happen. [`Writable`]: stream.md#class-streamwritable [`ZipBuffer`]: zlib.md#class-zlibzipbuffer [`ZipFile`]: zlib.md#class-zlibzipfile +[`async_hooks.createHook()`]: async_hooks.md#async_hookscreatehookoptions [`child_process`]: child_process.md [`cipher.getAuthTag()`]: crypto.md#ciphergetauthtag [`crypto.getDiffieHellman()`]: crypto.md#cryptogetdiffiehellmangroupname diff --git a/doc/node.1 b/doc/node.1 index 7a32ece46d2..2d5a3b98efc 100644 --- a/doc/node.1 +++ b/doc/node.1 @@ -86,6 +86,36 @@ Error: Cannot load native addon because loading addons is disabled. } .Ed . +.It Fl -enable-async-hooks +Enable use of \fBasync_hooks.createHook()\fR. This flag is required to enable +the \fBcreateHook\fR API, which is disabled by default. +Example: +.Bd -literal +import { createHook } from 'node:async_hooks'; + +createHook({ init() {} }).enable(); +.Ed +.Bd -literal +const { createHook } = require('node:async_hooks'); + +createHook({ init() {} }).enable(); +.Ed +.Bd -literal +$ node index.js +node:async_hooks:160 + throw new ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED(); + ^ + +Error [ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED]: async_hooks.createHook() is disabled. Use --enable-async-hooks to enable it. + at createHook (node:async_hooks:160:11) + at Object. (/home/user/index.js:3:1) + ... +.Ed +.Bd -literal +$ node --enable-async-hooks index.js +# Works without error +.Ed +. .It Fl -allow-child-process When using the Permission Model, the process will not be able to spawn any child process by default. @@ -2186,6 +2216,8 @@ one is included in the list below. .It \fB--allow-addons\fR .It +\fB--enable-async-hooks\fR +.It \fB--allow-child-process\fR .It \fB--allow-env\fR diff --git a/lib/async_hooks.js b/lib/async_hooks.js index b0106020d64..16899b1c1c5 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -16,11 +16,13 @@ const { const { ERR_ASYNC_CALLBACK, + ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED, ERR_ASYNC_RESOURCE_DOMAIN_REMOVED, ERR_ASYNC_TYPE, ERR_INVALID_ASYNC_ID, ERR_INVALID_ARG_VALUE, } = require('internal/errors').codes; +const { getOptionValue } = require('internal/options'); const { kEmptyObject, } = require('internal/util'); @@ -168,6 +170,9 @@ class AsyncHook { function createHook(fns) { + if (!getOptionValue('--enable-async-hooks')) { + throw new ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED(); + } return new AsyncHook(fns); } diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index 8a60b9a6ce5..01b232ac2a8 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -1,7 +1,11 @@ 'use strict'; const { + ArrayPrototypeIncludes, + ArrayPrototypeIndexOf, + ArrayPrototypePush, ArrayPrototypeSlice, + ArrayPrototypeSplice, ErrorCaptureStackTrace, ObjectDefineProperty, ObjectPrototypeHasOwnProperty, @@ -10,6 +14,7 @@ const { } = primordials; const { exitCodes: { kGenericUserError } } = internalBinding('errors'); +const { ERR_ASYNC_CALLBACK } = require('internal/errors').codes; const async_wrap = internalBinding('async_wrap'); const { setCallbackTrampoline } = async_wrap; @@ -576,9 +581,83 @@ function triggerAsyncId() { const kNoPromiseHook = Symbol('kNoPromiseHook'); +// Minimal internal async hook used by Node.js internals (e.g. the inspector) +// that must create hooks without --enable-async-hooks. Unlike the public +// async_hooks.createHook() this does not support promise callbacks. +class AsyncHook { + constructor({ init, before, after, destroy }) { + if (init !== undefined && typeof init !== 'function') + throw new ERR_ASYNC_CALLBACK('hook.init'); + if (before !== undefined && typeof before !== 'function') + throw new ERR_ASYNC_CALLBACK('hook.before'); + if (after !== undefined && typeof after !== 'function') + throw new ERR_ASYNC_CALLBACK('hook.after'); + if (destroy !== undefined && typeof destroy !== 'function') + throw new ERR_ASYNC_CALLBACK('hook.destroy'); + + // Internal hooks never track promises. + this[kNoPromiseHook] = true; + this[init_symbol] = init; + this[before_symbol] = before; + this[after_symbol] = after; + this[destroy_symbol] = destroy; + } + + enable() { + const { 0: hooks_array, 1: hook_fields } = getHookArrays(); + + if (ArrayPrototypeIncludes(hooks_array, this)) + return this; + + const prev_kTotals = hook_fields[kTotals]; + + hook_fields[kTotals] = hook_fields[kInit] += +!!this[init_symbol]; + hook_fields[kTotals] += hook_fields[kBefore] += +!!this[before_symbol]; + hook_fields[kTotals] += hook_fields[kAfter] += +!!this[after_symbol]; + hook_fields[kTotals] += hook_fields[kDestroy] += +!!this[destroy_symbol]; + ArrayPrototypePush(hooks_array, this); + + if (prev_kTotals === 0 && hook_fields[kTotals] > 0) { + enableHooks(); + } + + return this; + } + + disable() { + const { 0: hooks_array, 1: hook_fields } = getHookArrays(); + + const index = ArrayPrototypeIndexOf(hooks_array, this); + if (index === -1) + return this; + + const prev_kTotals = hook_fields[kTotals]; + + hook_fields[kTotals] = hook_fields[kInit] -= +!!this[init_symbol]; + hook_fields[kTotals] += hook_fields[kBefore] -= +!!this[before_symbol]; + hook_fields[kTotals] += hook_fields[kAfter] -= +!!this[after_symbol]; + hook_fields[kTotals] += hook_fields[kDestroy] -= +!!this[destroy_symbol]; + ArrayPrototypeSplice(hooks_array, index, 1); + + if (prev_kTotals > 0 && hook_fields[kTotals] === 0) { + disableHooks(); + } + + return this; + } +} + +// Creates an internal AsyncHook without honoring --enable-async-hooks. Only for +// use by Node.js internals; userland goes through async_hooks.createHook(). +function createHook(fns) { + return new AsyncHook(fns); +} + module.exports = { executionAsyncId, triggerAsyncId, + // Create an async hook without honoring --enable-async-hooks (internals only). + createHook, // Private API getHookArrays, symbols: { diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 88c4102eee4..b08b99c0227 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1143,6 +1143,8 @@ E('ERR_AMBIGUOUS_ARGUMENT', 'The "%s" argument is ambiguous. %s', TypeError); E('ERR_ARG_NOT_ITERABLE', '%s must be iterable', TypeError); E('ERR_ASSERTION', '%s', Error); E('ERR_ASYNC_CALLBACK', '%s must be a function', TypeError); +E('ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED', + 'async_hooks.createHook() is disabled. Use --enable-async-hooks to enable it.', Error); E('ERR_ASYNC_LOADER_REQUEST_NEVER_SETTLED', 'Async loader request never settled', Error); E('ERR_ASYNC_RESOURCE_DOMAIN_REMOVED', diff --git a/lib/internal/inspector_async_hook.js b/lib/internal/inspector_async_hook.js index 8ab833ea8ef..4bafef870b7 100644 --- a/lib/internal/inspector_async_hook.js +++ b/lib/internal/inspector_async_hook.js @@ -5,7 +5,7 @@ let config; function lazyHookCreation() { const inspector = internalBinding('inspector'); - const { createHook } = require('async_hooks'); + const { createHook } = require('internal/async_hooks'); config = internalBinding('config'); hook = createHook({ diff --git a/src/node_options.cc b/src/node_options.cc index 4ba4801ad1d..17a1acbdc9f 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -947,6 +947,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { kAllowedInEnvvar, false, OptionNamespaces::kPermissionNamespace); + AddOption("--enable-async-hooks", + "enable use of async_hooks.createHook", + BOOL_FIELD(enable_async_hooks), + kAllowedInEnvvar); AddOption("--allow-openssl-store", "allow use of OpenSSL STORE loaders when any permissions are set", BOOL_FIELD(allow_openssl_store), diff --git a/src/node_options.h b/src/node_options.h index f8aa5825386..3021dd230e0 100644 --- a/src/node_options.h +++ b/src/node_options.h @@ -238,6 +238,7 @@ class EnvironmentOptions : public Options { DEFINE_BOOL_FIELD(allow_ffi) = false; DEFINE_BOOL_FIELD(allow_openssl_store) = false; DEFINE_BOOL_FIELD(allow_worker_threads) = false; + DEFINE_BOOL_FIELD(enable_async_hooks) = false; DEFINE_BOOL_FIELD(experimental_vm_modules) = EXPERIMENTALS_DEFAULT_VALUE; DEFINE_BOOL_FIELD(expose_internals) = false; DEFINE_BOOL_FIELD(force_node_api_uncaught_exceptions_policy) = false; diff --git a/test/addons/async-resource/test.js b/test/addons/async-resource/test.js index d7bc44a92be..5a9b9e6f425 100644 --- a/test/addons/async-resource/test.js +++ b/test/addons/async-resource/test.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../../common'); const assert = require('assert'); diff --git a/test/addons/callback-scope/test-async-hooks.js b/test/addons/callback-scope/test-async-hooks.js index 2d1c8719b34..5801fcd18b4 100644 --- a/test/addons/callback-scope/test-async-hooks.js +++ b/test/addons/callback-scope/test-async-hooks.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../../common'); const assert = require('assert'); diff --git a/test/async-hooks/init-hooks.js b/test/async-hooks/init-hooks.js index 8fc44994fbc..24b353fc625 100644 --- a/test/async-hooks/init-hooks.js +++ b/test/async-hooks/init-hooks.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-async-await.js b/test/async-hooks/test-async-await.js index 4fd698c8e98..d05075373cb 100644 --- a/test/async-hooks/test-async-await.js +++ b/test/async-hooks/test-async-await.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); // This test ensures async hooks are being properly called diff --git a/test/async-hooks/test-async-exec-resource-http-32060.js b/test/async-hooks/test-async-exec-resource-http-32060.js index 35869708dfc..c5ae3bb7a97 100644 --- a/test/async-hooks/test-async-exec-resource-http-32060.js +++ b/test/async-hooks/test-async-exec-resource-http-32060.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); const { diff --git a/test/async-hooks/test-async-exec-resource-http-agent.js b/test/async-hooks/test-async-exec-resource-http-agent.js index abc2df3a193..fc919af4eb5 100644 --- a/test/async-hooks/test-async-exec-resource-http-agent.js +++ b/test/async-hooks/test-async-exec-resource-http-agent.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); diff --git a/test/async-hooks/test-async-exec-resource-http.js b/test/async-hooks/test-async-exec-resource-http.js index 0615a404045..05bdc014dba 100644 --- a/test/async-hooks/test-async-exec-resource-http.js +++ b/test/async-hooks/test-async-exec-resource-http.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-async-exec-resource-match.js b/test/async-hooks/test-async-exec-resource-match.js index 0b68b30ffa9..a94ecfa2e41 100644 --- a/test/async-hooks/test-async-exec-resource-match.js +++ b/test/async-hooks/test-async-exec-resource-match.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-async-local-storage-gcable.js b/test/async-hooks/test-async-local-storage-gcable.js index 303442e60c8..ee81250856d 100644 --- a/test/async-hooks/test-async-local-storage-gcable.js +++ b/test/async-hooks/test-async-local-storage-gcable.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose_gc +// Flags: --enable-async-hooks --expose_gc // This test ensures that AsyncLocalStorage gets gced once it was disabled // and no strong references remain in userland. diff --git a/test/async-hooks/test-callback-error.js b/test/async-hooks/test-callback-error.js index ad70feb70c9..5e42ba5b3f0 100644 --- a/test/async-hooks/test-callback-error.js +++ b/test/async-hooks/test-callback-error.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); const { spawnSync } = require('child_process'); @@ -37,7 +38,7 @@ assert.ok(!arg); // console.log should stay until this test's flakiness is solved console.log('start case 1'); console.time('end case 1'); - const child = spawnSync(process.execPath, [__filename, 'test_init_callback']); + const child = spawnSync(process.execPath, ['--enable-async-hooks', __filename, 'test_init_callback']); assert.ifError(child.error); const test_init_first_line = child.stderr.toString().split(/[\r\n]+/g)[0]; assert.strictEqual(test_init_first_line, 'Error: test_init_callback'); @@ -48,7 +49,7 @@ assert.ok(!arg); { console.log('start case 2'); console.time('end case 2'); - const child = spawnSync(process.execPath, [__filename, 'test_callback']); + const child = spawnSync(process.execPath, ['--enable-async-hooks', __filename, 'test_callback']); assert.ifError(child.error); const test_callback_first_line = child.stderr.toString().split(/[\r\n]+/g)[0]; assert.strictEqual(test_callback_first_line, 'Error: test_callback'); @@ -61,10 +62,10 @@ assert.ok(!arg); console.time('end case 3'); let program = process.execPath; let args = [ - '--abort-on-uncaught-exception', __filename, 'test_callback_abort' ]; + '--enable-async-hooks', '--abort-on-uncaught-exception', __filename, 'test_callback_abort' ]; let options = {}; if (!common.isWindows) { - [program, options] = common.escapePOSIXShell`ulimit -c 0 && exec "${program}" ${args[0]} "${args[1]}" ${args[2]}`; + [program, options] = common.escapePOSIXShell`ulimit -c 0 && exec "${program}" ${args[0]} ${args[1]} "${args[2]}" ${args[3]}`; args = []; options.shell = true; } diff --git a/test/async-hooks/test-crypto-pbkdf2.js b/test/async-hooks/test-crypto-pbkdf2.js index c607adf7258..e6f9c7cc245 100644 --- a/test/async-hooks/test-crypto-pbkdf2.js +++ b/test/async-hooks/test-crypto-pbkdf2.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); if (!common.hasCrypto) { diff --git a/test/async-hooks/test-crypto-randomBytes.js b/test/async-hooks/test-crypto-randomBytes.js index 8ecc1c45a9a..2213d08f3dc 100644 --- a/test/async-hooks/test-crypto-randomBytes.js +++ b/test/async-hooks/test-crypto-randomBytes.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); if (!common.hasCrypto) { diff --git a/test/async-hooks/test-destroy-not-blocked.js b/test/async-hooks/test-destroy-not-blocked.js index 2bbddf3047b..38541eb9f52 100644 --- a/test/async-hooks/test-destroy-not-blocked.js +++ b/test/async-hooks/test-destroy-not-blocked.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose_gc +// Flags: --enable-async-hooks --expose_gc const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-disable-in-init.js b/test/async-hooks/test-disable-in-init.js index f810dfca3b9..32cc2622fa4 100644 --- a/test/async-hooks/test-disable-in-init.js +++ b/test/async-hooks/test-disable-in-init.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const async_hooks = require('async_hooks'); diff --git a/test/async-hooks/test-embedder.api.async-resource-no-type.js b/test/async-hooks/test-embedder.api.async-resource-no-type.js index e21c5458df8..60050c33950 100644 --- a/test/async-hooks/test-embedder.api.async-resource-no-type.js +++ b/test/async-hooks/test-embedder.api.async-resource-no-type.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); @@ -25,7 +26,7 @@ if (process.argv[2] === 'child') { }); } else { - const args = process.argv.slice(1).concat('child'); + const args = ['--enable-async-hooks', ...process.argv.slice(1), 'child']; spawn(process.execPath, args) .on('close', common.mustCall((code) => { // No error because the type was defaulted diff --git a/test/async-hooks/test-embedder.api.async-resource.js b/test/async-hooks/test-embedder.api.async-resource.js index d60c03fb87b..1b10c05470d 100644 --- a/test/async-hooks/test-embedder.api.async-resource.js +++ b/test/async-hooks/test-embedder.api.async-resource.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-emit-after-on-destroyed.js b/test/async-hooks/test-emit-after-on-destroyed.js index 719d6ab91a5..8595caaff08 100644 --- a/test/async-hooks/test-emit-after-on-destroyed.js +++ b/test/async-hooks/test-emit-after-on-destroyed.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals 'use strict'; const common = require('../common'); @@ -42,7 +42,7 @@ if (process.argv[2] === 'child') { emitAfter(asyncId); } } else { - const args = ['--expose-internals'] + const args = ['--enable-async-hooks', '--expose-internals'] .concat(process.argv.slice(1)) .concat('child'); let errData = Buffer.from(''); diff --git a/test/async-hooks/test-emit-before-after.js b/test/async-hooks/test-emit-before-after.js index 561004cac2f..75c7e226783 100644 --- a/test/async-hooks/test-emit-before-after.js +++ b/test/async-hooks/test-emit-before-after.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-emit-before-on-destroyed.js b/test/async-hooks/test-emit-before-on-destroyed.js index ca0553e5334..7b367d7f8e9 100644 --- a/test/async-hooks/test-emit-before-on-destroyed.js +++ b/test/async-hooks/test-emit-before-on-destroyed.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals 'use strict'; const common = require('../common'); @@ -42,7 +42,7 @@ if (process.argv[2] === 'child') { emitBefore(asyncId, triggerId); } } else { - const args = ['--expose-internals'] + const args = ['--enable-async-hooks', '--expose-internals'] .concat(process.argv.slice(1)) .concat('child'); let errData = Buffer.from(''); diff --git a/test/async-hooks/test-emit-init.js b/test/async-hooks/test-emit-init.js index 8994943b472..7e6f2e2dedd 100644 --- a/test/async-hooks/test-emit-init.js +++ b/test/async-hooks/test-emit-init.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-enable-disable.js b/test/async-hooks/test-enable-disable.js index d408338e892..257752ac9be 100644 --- a/test/async-hooks/test-enable-disable.js +++ b/test/async-hooks/test-enable-disable.js @@ -81,6 +81,7 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-enable-in-init.js b/test/async-hooks/test-enable-in-init.js index e710657a340..0fee0d1bd19 100644 --- a/test/async-hooks/test-enable-in-init.js +++ b/test/async-hooks/test-enable-in-init.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const async_hooks = require('async_hooks'); diff --git a/test/async-hooks/test-filehandle-no-reuse.js b/test/async-hooks/test-filehandle-no-reuse.js index f607a7a22e0..7486405e8fc 100644 --- a/test/async-hooks/test-filehandle-no-reuse.js +++ b/test/async-hooks/test-filehandle-no-reuse.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const initHooks = require('./init-hooks'); diff --git a/test/async-hooks/test-fseventwrap.js b/test/async-hooks/test-fseventwrap.js index a5e1a3b9d2f..5c705564bc4 100644 --- a/test/async-hooks/test-fseventwrap.js +++ b/test/async-hooks/test-fseventwrap.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-fsreqcallback-access.js b/test/async-hooks/test-fsreqcallback-access.js index b1b75f56930..5bdc605101a 100644 --- a/test/async-hooks/test-fsreqcallback-access.js +++ b/test/async-hooks/test-fsreqcallback-access.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-fsreqcallback-readFile.js b/test/async-hooks/test-fsreqcallback-readFile.js index fb84306cfa8..ce42224bbb5 100644 --- a/test/async-hooks/test-fsreqcallback-readFile.js +++ b/test/async-hooks/test-fsreqcallback-readFile.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-getaddrinforeqwrap.js b/test/async-hooks/test-getaddrinforeqwrap.js index a21557bcd56..9505ae9d396 100644 --- a/test/async-hooks/test-getaddrinforeqwrap.js +++ b/test/async-hooks/test-getaddrinforeqwrap.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-getnameinforeqwrap.js b/test/async-hooks/test-getnameinforeqwrap.js index b00fa0d4d9d..71bb868304a 100644 --- a/test/async-hooks/test-getnameinforeqwrap.js +++ b/test/async-hooks/test-getnameinforeqwrap.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-graph.fsreq-readFile.js b/test/async-hooks/test-graph.fsreq-readFile.js index 39435cdc489..f1722d0e0c4 100644 --- a/test/async-hooks/test-graph.fsreq-readFile.js +++ b/test/async-hooks/test-graph.fsreq-readFile.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const initHooks = require('./init-hooks'); diff --git a/test/async-hooks/test-graph.http.js b/test/async-hooks/test-graph.http.js index 859000b14aa..03fdfc04c37 100644 --- a/test/async-hooks/test-graph.http.js +++ b/test/async-hooks/test-graph.http.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); if (!common.hasIPv6) diff --git a/test/async-hooks/test-graph.intervals.js b/test/async-hooks/test-graph.intervals.js index 9e626951a14..f63e186bbc1 100644 --- a/test/async-hooks/test-graph.intervals.js +++ b/test/async-hooks/test-graph.intervals.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const initHooks = require('./init-hooks'); diff --git a/test/async-hooks/test-graph.pipe.js b/test/async-hooks/test-graph.pipe.js index abf8a98ff00..36200af616f 100644 --- a/test/async-hooks/test-graph.pipe.js +++ b/test/async-hooks/test-graph.pipe.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const initHooks = require('./init-hooks'); diff --git a/test/async-hooks/test-graph.pipeconnect.js b/test/async-hooks/test-graph.pipeconnect.js index db237ff6b2d..1dca97abbf2 100644 --- a/test/async-hooks/test-graph.pipeconnect.js +++ b/test/async-hooks/test-graph.pipeconnect.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const initHooks = require('./init-hooks'); diff --git a/test/async-hooks/test-graph.shutdown.js b/test/async-hooks/test-graph.shutdown.js index 0f633787e5e..e165a0405c3 100644 --- a/test/async-hooks/test-graph.shutdown.js +++ b/test/async-hooks/test-graph.shutdown.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); if (!common.hasIPv6) diff --git a/test/async-hooks/test-graph.signal.js b/test/async-hooks/test-graph.signal.js index 351fb7550af..589a0032201 100644 --- a/test/async-hooks/test-graph.signal.js +++ b/test/async-hooks/test-graph.signal.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); if (common.isWindows) { diff --git a/test/async-hooks/test-graph.statwatcher.js b/test/async-hooks/test-graph.statwatcher.js index f960ffebd3e..aac1fe43186 100644 --- a/test/async-hooks/test-graph.statwatcher.js +++ b/test/async-hooks/test-graph.statwatcher.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks require('../common'); const commonPath = require.resolve('../common'); diff --git a/test/async-hooks/test-graph.tcp.js b/test/async-hooks/test-graph.tcp.js index 6f8db20add6..43bb18414be 100644 --- a/test/async-hooks/test-graph.tcp.js +++ b/test/async-hooks/test-graph.tcp.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); if (!common.hasIPv6) diff --git a/test/async-hooks/test-graph.timeouts.js b/test/async-hooks/test-graph.timeouts.js index 88350db2bae..031020d6f46 100644 --- a/test/async-hooks/test-graph.timeouts.js +++ b/test/async-hooks/test-graph.timeouts.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const initHooks = require('./init-hooks'); diff --git a/test/async-hooks/test-graph.tls-write-12.js b/test/async-hooks/test-graph.tls-write-12.js index 748234402c0..621c085faab 100644 --- a/test/async-hooks/test-graph.tls-write-12.js +++ b/test/async-hooks/test-graph.tls-write-12.js @@ -1,3 +1,4 @@ +// Flags: --enable-async-hooks 'use strict'; const common = require('../common'); diff --git a/test/async-hooks/test-graph.tls-write.js b/test/async-hooks/test-graph.tls-write.js index 243039e01d1..bb71c01ebac 100644 --- a/test/async-hooks/test-graph.tls-write.js +++ b/test/async-hooks/test-graph.tls-write.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); if (!common.hasCrypto) diff --git a/test/async-hooks/test-http-agent-handle-reuse-parallel.js b/test/async-hooks/test-http-agent-handle-reuse-parallel.js index 3827691cc8b..cdabb7b4b42 100644 --- a/test/async-hooks/test-http-agent-handle-reuse-parallel.js +++ b/test/async-hooks/test-http-agent-handle-reuse-parallel.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals const common = require('../common'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); diff --git a/test/async-hooks/test-http-agent-handle-reuse-serial.js b/test/async-hooks/test-http-agent-handle-reuse-serial.js index bae0698a0a6..50974fc254f 100644 --- a/test/async-hooks/test-http-agent-handle-reuse-serial.js +++ b/test/async-hooks/test-http-agent-handle-reuse-serial.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals const common = require('../common'); const initHooks = require('./init-hooks'); const { checkInvocations } = require('./hook-checks'); diff --git a/test/async-hooks/test-httpparser-reuse.js b/test/async-hooks/test-httpparser-reuse.js index f7ce2cd8762..29b6a94e92b 100644 --- a/test/async-hooks/test-httpparser-reuse.js +++ b/test/async-hooks/test-httpparser-reuse.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-httpparser.request.js b/test/async-hooks/test-httpparser.request.js index 6379bf330d2..c7b00130e16 100644 --- a/test/async-hooks/test-httpparser.request.js +++ b/test/async-hooks/test-httpparser.request.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals --no-warnings +// Flags: --enable-async-hooks --expose-internals --no-warnings 'use strict'; diff --git a/test/async-hooks/test-httpparser.response.js b/test/async-hooks/test-httpparser.response.js index 2e600b12747..1493fbe4d8b 100644 --- a/test/async-hooks/test-httpparser.response.js +++ b/test/async-hooks/test-httpparser.response.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals --no-warnings +// Flags: --enable-async-hooks --expose-internals --no-warnings 'use strict'; diff --git a/test/async-hooks/test-immediate.js b/test/async-hooks/test-immediate.js index fa9475abaed..657b0d028d8 100644 --- a/test/async-hooks/test-immediate.js +++ b/test/async-hooks/test-immediate.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-improper-order.js b/test/async-hooks/test-improper-order.js index 47678fb1977..13245e8a24d 100644 --- a/test/async-hooks/test-improper-order.js +++ b/test/async-hooks/test-improper-order.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals 'use strict'; const common = require('../common'); @@ -41,7 +41,7 @@ if (process.argv[2] === 'child') { emitAfter(asyncId); } } else { - const args = ['--expose-internals'] + const args = ['--enable-async-hooks', '--expose-internals'] .concat(process.argv.slice(1)) .concat('child'); let errData = Buffer.from(''); diff --git a/test/async-hooks/test-improper-unwind.js b/test/async-hooks/test-improper-unwind.js index 9e651b3bc50..3de183852a4 100644 --- a/test/async-hooks/test-improper-unwind.js +++ b/test/async-hooks/test-improper-unwind.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals 'use strict'; const common = require('../common'); @@ -45,7 +45,7 @@ if (process.argv[2] === 'child') { console.log('heartbeat: still alive'); emitAfter(eventOneId); } else { - const args = ['--expose-internals'] + const args = ['--enable-async-hooks', '--expose-internals'] .concat(process.argv.slice(1)) .concat('child'); let errData = Buffer.from(''); diff --git a/test/async-hooks/test-late-hook-enable.js b/test/async-hooks/test-late-hook-enable.js index 3305a05065b..a84eb625b2b 100644 --- a/test/async-hooks/test-late-hook-enable.js +++ b/test/async-hooks/test-late-hook-enable.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); const async_hooks = require('async_hooks'); diff --git a/test/async-hooks/test-nexttick-default-trigger.js b/test/async-hooks/test-nexttick-default-trigger.js index fa1ad2c561c..ebdf996b58e 100644 --- a/test/async-hooks/test-nexttick-default-trigger.js +++ b/test/async-hooks/test-nexttick-default-trigger.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); // This tests ensures that the triggerId of the nextTick function sets the diff --git a/test/async-hooks/test-pipeconnectwrap.js b/test/async-hooks/test-pipeconnectwrap.js index 36980f01859..d23ef20b507 100644 --- a/test/async-hooks/test-pipeconnectwrap.js +++ b/test/async-hooks/test-pipeconnectwrap.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-pipewrap.js b/test/async-hooks/test-pipewrap.js index 7ea5f38adc8..45eae3fd899 100644 --- a/test/async-hooks/test-pipewrap.js +++ b/test/async-hooks/test-pipewrap.js @@ -1,7 +1,7 @@ // NOTE: this also covers process wrap as one is created along with the pipes // when we launch the sleep process 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-promise.chain-promise-before-init-hooks.js b/test/async-hooks/test-promise.chain-promise-before-init-hooks.js index f292c828657..ee4a7b85a55 100644 --- a/test/async-hooks/test-promise.chain-promise-before-init-hooks.js +++ b/test/async-hooks/test-promise.chain-promise-before-init-hooks.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-promise.js b/test/async-hooks/test-promise.js index 96be5e5efcb..9ff89658315 100644 --- a/test/async-hooks/test-promise.js +++ b/test/async-hooks/test-promise.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); diff --git a/test/async-hooks/test-promise.promise-before-init-hooks.js b/test/async-hooks/test-promise.promise-before-init-hooks.js index b61a214f428..1a5e5927361 100644 --- a/test/async-hooks/test-promise.promise-before-init-hooks.js +++ b/test/async-hooks/test-promise.promise-before-init-hooks.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-querywrap.js b/test/async-hooks/test-querywrap.js index 5ddb2fe700e..9cdda6eaa3a 100644 --- a/test/async-hooks/test-querywrap.js +++ b/test/async-hooks/test-querywrap.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-queue-microtask.js b/test/async-hooks/test-queue-microtask.js index 01200d45423..c20cc72eb8d 100644 --- a/test/async-hooks/test-queue-microtask.js +++ b/test/async-hooks/test-queue-microtask.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-shutdownwrap.js b/test/async-hooks/test-shutdownwrap.js index fea4a3a166a..0d1c44924d3 100644 --- a/test/async-hooks/test-shutdownwrap.js +++ b/test/async-hooks/test-shutdownwrap.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-signalwrap.js b/test/async-hooks/test-signalwrap.js index 60adaedd476..ec140eefe0d 100644 --- a/test/async-hooks/test-signalwrap.js +++ b/test/async-hooks/test-signalwrap.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); if (common.isWindows) { diff --git a/test/async-hooks/test-statwatcher.js b/test/async-hooks/test-statwatcher.js index d24aab58402..8e223311353 100644 --- a/test/async-hooks/test-statwatcher.js +++ b/test/async-hooks/test-statwatcher.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const tmpdir = require('../common/tmpdir'); diff --git a/test/async-hooks/test-tcpwrap.js b/test/async-hooks/test-tcpwrap.js index 3f0987b9882..177e27e1b91 100644 --- a/test/async-hooks/test-tcpwrap.js +++ b/test/async-hooks/test-tcpwrap.js @@ -1,5 +1,6 @@ // Covers TCPWRAP and related TCPCONNECTWRAP 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); if (!common.hasIPv6) diff --git a/test/async-hooks/test-timers.setInterval.js b/test/async-hooks/test-timers.setInterval.js index 10b2bafb2a2..a46cc46fa62 100644 --- a/test/async-hooks/test-timers.setInterval.js +++ b/test/async-hooks/test-timers.setInterval.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-timers.setTimeout.js b/test/async-hooks/test-timers.setTimeout.js index 540c88abade..288c0c69121 100644 --- a/test/async-hooks/test-timers.setTimeout.js +++ b/test/async-hooks/test-timers.setTimeout.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-tlswrap.js b/test/async-hooks/test-tlswrap.js index 6a8996e00f2..c9ac56522b6 100644 --- a/test/async-hooks/test-tlswrap.js +++ b/test/async-hooks/test-tlswrap.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); if (!common.hasCrypto) diff --git a/test/async-hooks/test-track-promises-default.js b/test/async-hooks/test-track-promises-default.js index 071f8faaecc..7440d5a1a69 100644 --- a/test/async-hooks/test-track-promises-default.js +++ b/test/async-hooks/test-track-promises-default.js @@ -1,3 +1,4 @@ +// Flags: --enable-async-hooks 'use strict'; // Test that trackPromises default to true. const common = require('../common'); diff --git a/test/async-hooks/test-track-promises-false-check.js b/test/async-hooks/test-track-promises-false-check.js index 7ff4142d0d1..7fb059af1e1 100644 --- a/test/async-hooks/test-track-promises-false-check.js +++ b/test/async-hooks/test-track-promises-false-check.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals 'use strict'; // Test that trackPromises: false prevents promise hooks from being installed. diff --git a/test/async-hooks/test-track-promises-false.js b/test/async-hooks/test-track-promises-false.js index 1a3b5eda2e2..2a753ede035 100644 --- a/test/async-hooks/test-track-promises-false.js +++ b/test/async-hooks/test-track-promises-false.js @@ -1,3 +1,4 @@ +// Flags: --enable-async-hooks 'use strict'; // Test that trackPromises: false works. const common = require('../common'); diff --git a/test/async-hooks/test-track-promises-true.js b/test/async-hooks/test-track-promises-true.js index cc53f3a9c92..8f11eab4a27 100644 --- a/test/async-hooks/test-track-promises-true.js +++ b/test/async-hooks/test-track-promises-true.js @@ -1,3 +1,4 @@ +// Flags: --enable-async-hooks 'use strict'; // Test that trackPromises: true works. const common = require('../common'); diff --git a/test/async-hooks/test-track-promises-validation.js b/test/async-hooks/test-track-promises-validation.js index 108f82ff256..c7ed7465b33 100644 --- a/test/async-hooks/test-track-promises-validation.js +++ b/test/async-hooks/test-track-promises-validation.js @@ -1,3 +1,4 @@ +// Flags: --enable-async-hooks 'use strict'; // Test validation of trackPromises option. diff --git a/test/async-hooks/test-ttywrap.readstream.js b/test/async-hooks/test-ttywrap.readstream.js index 7e27ffc5bfb..1cb02109218 100644 --- a/test/async-hooks/test-ttywrap.readstream.js +++ b/test/async-hooks/test-ttywrap.readstream.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-udpsendwrap.js b/test/async-hooks/test-udpsendwrap.js index f1403e3226a..c1772e4b10a 100644 --- a/test/async-hooks/test-udpsendwrap.js +++ b/test/async-hooks/test-udpsendwrap.js @@ -1,4 +1,4 @@ -// Flags: --test-udp-no-try-send +// Flags: --enable-async-hooks --test-udp-no-try-send 'use strict'; const common = require('../common'); diff --git a/test/async-hooks/test-udpwrap.js b/test/async-hooks/test-udpwrap.js index 0a0e03c5d51..b8bb97a5bce 100644 --- a/test/async-hooks/test-udpwrap.js +++ b/test/async-hooks/test-udpwrap.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-unhandled-exception-valid-ids.js b/test/async-hooks/test-unhandled-exception-valid-ids.js index 09e5823452f..367ba4e6577 100644 --- a/test/async-hooks/test-unhandled-exception-valid-ids.js +++ b/test/async-hooks/test-unhandled-exception-valid-ids.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const initHooks = require('./init-hooks'); diff --git a/test/async-hooks/test-unhandled-rejection-context.js b/test/async-hooks/test-unhandled-rejection-context.js index 168b51a3331..c2e8b5fb52b 100644 --- a/test/async-hooks/test-unhandled-rejection-context.js +++ b/test/async-hooks/test-unhandled-rejection-context.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); diff --git a/test/async-hooks/test-writewrap.js b/test/async-hooks/test-writewrap.js index 4f3455480ba..62b6254b5a8 100644 --- a/test/async-hooks/test-writewrap.js +++ b/test/async-hooks/test-writewrap.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/async-hooks/test-zlib.zlib-binding.deflate.js b/test/async-hooks/test-zlib.zlib-binding.deflate.js index 802f09eb306..24bc53dccb9 100644 --- a/test/async-hooks/test-zlib.zlib-binding.deflate.js +++ b/test/async-hooks/test-zlib.zlib-binding.deflate.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals 'use strict'; const common = require('../common'); diff --git a/test/es-module/test-vm-compile-function-leak.js b/test/es-module/test-vm-compile-function-leak.js index f9f04588fdc..a544586a65e 100644 --- a/test/es-module/test-vm-compile-function-leak.js +++ b/test/es-module/test-vm-compile-function-leak.js @@ -1,4 +1,4 @@ -// Flags: --max-old-space-size=16 --trace-gc +// Flags: --enable-async-hooks --max-old-space-size=16 --trace-gc 'use strict'; // This tests that vm.compileFunction with dynamic import callback does not leak. diff --git a/test/es-module/test-vm-contextified-script-leak.js b/test/es-module/test-vm-contextified-script-leak.js index a8625fc94d2..f44e4331ffc 100644 --- a/test/es-module/test-vm-contextified-script-leak.js +++ b/test/es-module/test-vm-contextified-script-leak.js @@ -1,4 +1,4 @@ -// Flags: --max-old-space-size=16 --trace-gc +// Flags: --enable-async-hooks --max-old-space-size=16 --trace-gc 'use strict'; // This tests that vm.Script with dynamic import callback does not leak. diff --git a/test/es-module/test-vm-source-text-module-leak.js b/test/es-module/test-vm-source-text-module-leak.js index ad85c08e393..e73abca7ff6 100644 --- a/test/es-module/test-vm-source-text-module-leak.js +++ b/test/es-module/test-vm-source-text-module-leak.js @@ -1,4 +1,4 @@ -// Flags: --experimental-vm-modules --max-old-space-size=16 --trace-gc +// Flags: --enable-async-hooks --experimental-vm-modules --max-old-space-size=16 --trace-gc 'use strict'; // This tests that vm.SourceTextModule() does not leak. diff --git a/test/es-module/test-vm-synthetic-module-leak.js b/test/es-module/test-vm-synthetic-module-leak.js index bc0e4689535..dd93898a6b2 100644 --- a/test/es-module/test-vm-synthetic-module-leak.js +++ b/test/es-module/test-vm-synthetic-module-leak.js @@ -1,4 +1,4 @@ -// Flags: --experimental-vm-modules --max-old-space-size=16 +// Flags: --enable-async-hooks --experimental-vm-modules --max-old-space-size=16 'use strict'; // This tests that vm.SyntheticModule does not leak. diff --git a/test/node-api/test_async/test-async-hooks.js b/test/node-api/test_async/test-async-hooks.js index 956d210f3f0..775f5566183 100644 --- a/test/node-api/test_async/test-async-hooks.js +++ b/test/node-api/test_async/test-async-hooks.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../../common'); const assert = require('assert'); const async_hooks = require('async_hooks'); diff --git a/test/node-api/test_async_context/test-gcable-callback.js b/test/node-api/test_async_context/test-gcable-callback.js index b1e4d29c5f4..6b3baccd119 100644 --- a/test/node-api/test_async_context/test-gcable-callback.js +++ b/test/node-api/test_async_context/test-gcable-callback.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --gc-interval=100 --gc-global +// Flags: --enable-async-hooks --gc-interval=100 --gc-global const common = require('../../common'); const assert = require('assert'); diff --git a/test/node-api/test_async_context/test.js b/test/node-api/test_async_context/test.js index e4e46afbf2e..2cb9fa246d6 100644 --- a/test/node-api/test_async_context/test.js +++ b/test/node-api/test_async_context/test.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --gc-interval=100 --gc-global +// Flags: --enable-async-hooks --gc-interval=100 --gc-global const common = require('../../common'); const assert = require('assert'); diff --git a/test/node-api/test_callback_scope/test-async-hooks.js b/test/node-api/test_callback_scope/test-async-hooks.js index 7c12e25848d..4a87660749c 100644 --- a/test/node-api/test_callback_scope/test-async-hooks.js +++ b/test/node-api/test_callback_scope/test-async-hooks.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../../common'); const assert = require('assert'); diff --git a/test/node-api/test_make_callback/test-async-hooks.js b/test/node-api/test_make_callback/test-async-hooks.js index d6053c5058a..610946d93b6 100644 --- a/test/node-api/test_make_callback/test-async-hooks.js +++ b/test/node-api/test_make_callback/test-async-hooks.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../../common'); const assert = require('assert'); diff --git a/test/parallel/test-async-hooks-async-await.js b/test/parallel/test-async-hooks-async-await.js index 97b50f3f268..c8b8fa7ed9f 100644 --- a/test/parallel/test-async-hooks-async-await.js +++ b/test/parallel/test-async-hooks-async-await.js @@ -1,6 +1,7 @@ // Test async-hooks fired on right // asyncIds & triggerAsyncId for async-await 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const async_hooks = require('async_hooks'); diff --git a/test/parallel/test-async-hooks-asyncresource-constructor.js b/test/parallel/test-async-hooks-asyncresource-constructor.js index 8b504aa7a79..39fb8920519 100644 --- a/test/parallel/test-async-hooks-asyncresource-constructor.js +++ b/test/parallel/test-async-hooks-asyncresource-constructor.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks // This tests that AsyncResource throws an error if bad parameters are passed diff --git a/test/parallel/test-async-hooks-close-during-destroy.js b/test/parallel/test-async-hooks-close-during-destroy.js index 6e485d2d111..8cfa096ea4f 100644 --- a/test/parallel/test-async-hooks-close-during-destroy.js +++ b/test/parallel/test-async-hooks-close-during-destroy.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks // Test that async ids that are added to the destroy queue while running a // `destroy` callback are handled correctly. diff --git a/test/parallel/test-async-hooks-constructor.js b/test/parallel/test-async-hooks-constructor.js index 62ec854108e..9559c86c46a 100644 --- a/test/parallel/test-async-hooks-constructor.js +++ b/test/parallel/test-async-hooks-constructor.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks // This tests that AsyncHooks throws an error if bad parameters are passed. diff --git a/test/parallel/test-async-hooks-correctly-switch-promise-hook.js b/test/parallel/test-async-hooks-correctly-switch-promise-hook.js index 73127f1ebaf..1de9ed824fa 100644 --- a/test/parallel/test-async-hooks-correctly-switch-promise-hook.js +++ b/test/parallel/test-async-hooks-correctly-switch-promise-hook.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks require('../common'); const assert = require('assert'); const async_hooks = require('async_hooks'); diff --git a/test/parallel/test-async-hooks-create-hook-disabled-internals.js b/test/parallel/test-async-hooks-create-hook-disabled-internals.js new file mode 100644 index 00000000000..70c4ff1c7e9 --- /dev/null +++ b/test/parallel/test-async-hooks-create-hook-disabled-internals.js @@ -0,0 +1,38 @@ +'use strict'; + +// Internals that use async_hooks for a purpose other than observing +// async_hooks themselves (e.g. node:test via AsyncLocalStorage) must keep +// working without --enable-async-hooks. Consumers that are themselves +// async_hooks observers (e.g. the `node.async_hooks` trace event category) +// require the flag. + +const common = require('../common'); +const assert = require('assert'); +const { spawnSync } = require('child_process'); +const test = require('node:test'); + +test('node:test runs without --enable-async-hooks', common.mustCall()); + +{ + const tmpdir = require('../common/tmpdir'); + tmpdir.refresh(); + + // The `node.async_hooks` trace event category is itself an async_hooks + // consumer, so it requires --enable-async-hooks. + const child = spawnSync(process.execPath, [ + '--enable-async-hooks', + '--trace-event-categories', 'node.async_hooks', + '-e', 'setTimeout(() => {}, 1)', + ], { cwd: tmpdir.path, encoding: 'utf8' }); + assert.strictEqual(child.stderr, ''); + assert.strictEqual(child.status, 0); + + // Without --enable-async-hooks the same category must fail. + const childWithoutFlag = spawnSync(process.execPath, [ + '--trace-event-categories', 'node.async_hooks', + '-e', 'setTimeout(() => {}, 1)', + ], { cwd: tmpdir.path, encoding: 'utf8' }); + assert.notStrictEqual(childWithoutFlag.status, 0); + assert.match(childWithoutFlag.stderr, + /ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED/); +} diff --git a/test/parallel/test-async-hooks-create-hook-disabled.js b/test/parallel/test-async-hooks-create-hook-disabled.js new file mode 100644 index 00000000000..ad102c718f9 --- /dev/null +++ b/test/parallel/test-async-hooks-create-hook-disabled.js @@ -0,0 +1,15 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const { createHook } = require('async_hooks'); + +// async_hooks.createHook is disabled by default (without --enable-async-hooks) +{ + assert.throws(() => { + createHook({ init() {} }); + }, common.expectsError({ + code: 'ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED', + message: 'async_hooks.createHook() is disabled. Use --enable-async-hooks to enable it.', + })); +} diff --git a/test/parallel/test-async-hooks-create-hook-enabled.js b/test/parallel/test-async-hooks-create-hook-enabled.js new file mode 100644 index 00000000000..4b6db49ac67 --- /dev/null +++ b/test/parallel/test-async-hooks-create-hook-enabled.js @@ -0,0 +1,18 @@ +// Flags: --enable-async-hooks +'use strict'; + +require('../common'); +const { createHook } = require('async_hooks'); + +// When --enable-async-hooks is passed, createHook should work +{ + // doesNotThrow + const hook = createHook({ + init() {}, + before() {}, + after() {}, + destroy() {}, + }); + hook.enable(); + hook.disable(); +} diff --git a/test/parallel/test-async-hooks-destroy-on-gc.js b/test/parallel/test-async-hooks-destroy-on-gc.js index a401f6651cc..39558640f78 100644 --- a/test/parallel/test-async-hooks-destroy-on-gc.js +++ b/test/parallel/test-async-hooks-destroy-on-gc.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose_gc +// Flags: --enable-async-hooks --expose_gc // This test ensures that userland-only AsyncResources cause a destroy event to // be emitted when they get gced. diff --git a/test/parallel/test-async-hooks-disable-during-promise.js b/test/parallel/test-async-hooks-disable-during-promise.js index a25dae51e1f..b63e735bec9 100644 --- a/test/parallel/test-async-hooks-disable-during-promise.js +++ b/test/parallel/test-async-hooks-disable-during-promise.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const async_hooks = require('async_hooks'); const { isMainThread } = require('worker_threads'); diff --git a/test/parallel/test-async-hooks-disable-gc-tracking.js b/test/parallel/test-async-hooks-disable-gc-tracking.js index 87b096c2581..d227853a437 100644 --- a/test/parallel/test-async-hooks-disable-gc-tracking.js +++ b/test/parallel/test-async-hooks-disable-gc-tracking.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose_gc +// Flags: --enable-async-hooks --expose_gc // This test ensures that userland-only AsyncResources cause a destroy event to // be emitted when they get gced. diff --git a/test/parallel/test-async-hooks-enable-before-promise-resolve.js b/test/parallel/test-async-hooks-enable-before-promise-resolve.js index e6facfb6392..e9e8947dec0 100644 --- a/test/parallel/test-async-hooks-enable-before-promise-resolve.js +++ b/test/parallel/test-async-hooks-enable-before-promise-resolve.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); const async_hooks = require('async_hooks'); diff --git a/test/parallel/test-async-hooks-enable-disable-enable.js b/test/parallel/test-async-hooks-enable-disable-enable.js index 640815e14c8..cc9a29f1206 100644 --- a/test/parallel/test-async-hooks-enable-disable-enable.js +++ b/test/parallel/test-async-hooks-enable-disable-enable.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); const async_hooks = require('async_hooks'); diff --git a/test/parallel/test-async-hooks-enable-disable.js b/test/parallel/test-async-hooks-enable-disable.js index 71d35c91371..4e6a1488bb1 100644 --- a/test/parallel/test-async-hooks-enable-disable.js +++ b/test/parallel/test-async-hooks-enable-disable.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); const async_hooks = require('async_hooks'); diff --git a/test/parallel/test-async-hooks-enable-during-promise.js b/test/parallel/test-async-hooks-enable-during-promise.js index ce3253b01c1..d861d6041bc 100644 --- a/test/parallel/test-async-hooks-enable-during-promise.js +++ b/test/parallel/test-async-hooks-enable-during-promise.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const async_hooks = require('async_hooks'); diff --git a/test/parallel/test-async-hooks-enable-recursive.js b/test/parallel/test-async-hooks-enable-recursive.js index 74caebbad22..e04eb538511 100644 --- a/test/parallel/test-async-hooks-enable-recursive.js +++ b/test/parallel/test-async-hooks-enable-recursive.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const async_hooks = require('async_hooks'); diff --git a/test/parallel/test-async-hooks-enabledhooksexits.js b/test/parallel/test-async-hooks-enabledhooksexits.js index 11737d65485..2aae942ab5a 100644 --- a/test/parallel/test-async-hooks-enabledhooksexits.js +++ b/test/parallel/test-async-hooks-enabledhooksexits.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals 'use strict'; require('../common'); diff --git a/test/parallel/test-async-hooks-execution-async-resource-await.js b/test/parallel/test-async-hooks-execution-async-resource-await.js index 8dc04c83d73..4a2a942f163 100644 --- a/test/parallel/test-async-hooks-execution-async-resource-await.js +++ b/test/parallel/test-async-hooks-execution-async-resource-await.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const sleep = require('util').promisify(setTimeout); diff --git a/test/parallel/test-async-hooks-execution-async-resource.js b/test/parallel/test-async-hooks-execution-async-resource.js index b3a9f76ef4e..0374548b7c8 100644 --- a/test/parallel/test-async-hooks-execution-async-resource.js +++ b/test/parallel/test-async-hooks-execution-async-resource.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/parallel/test-async-hooks-fatal-error.js b/test/parallel/test-async-hooks-fatal-error.js index 30ead86132f..9447fc75f3b 100644 --- a/test/parallel/test-async-hooks-fatal-error.js +++ b/test/parallel/test-async-hooks-fatal-error.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); const childProcess = require('child_process'); @@ -41,7 +42,7 @@ function main() { for (const [valueType, expect] of valueTypes) { const cp = childProcess.spawnSync( process.execPath, - [ __filename, 'child', type, valueType ], + [ '--enable-async-hooks', __filename, 'child', type, valueType ], { encoding: 'utf8', }); diff --git a/test/parallel/test-async-hooks-http-agent-destroy.js b/test/parallel/test-async-hooks-http-agent-destroy.js index 4d90cb7d350..12435fa3a1f 100644 --- a/test/parallel/test-async-hooks-http-agent-destroy.js +++ b/test/parallel/test-async-hooks-http-agent-destroy.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals const common = require('../common'); const assert = require('assert'); const { async_id_symbol } = require('internal/async_hooks').symbols; diff --git a/test/parallel/test-async-hooks-http-agent.js b/test/parallel/test-async-hooks-http-agent.js index 337bab65d9d..9ff12a20a6e 100644 --- a/test/parallel/test-async-hooks-http-agent.js +++ b/test/parallel/test-async-hooks-http-agent.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals const common = require('../common'); const assert = require('assert'); const { async_id_symbol } = require('internal/async_hooks').symbols; diff --git a/test/parallel/test-async-hooks-http-parser-destroy.js b/test/parallel/test-async-hooks-http-parser-destroy.js index b2d1fd9a94f..d773ea1159f 100644 --- a/test/parallel/test-async-hooks-http-parser-destroy.js +++ b/test/parallel/test-async-hooks-http-parser-destroy.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); const async_hooks = require('async_hooks'); diff --git a/test/parallel/test-async-hooks-prevent-double-destroy.js b/test/parallel/test-async-hooks-prevent-double-destroy.js index 4aa55a5a6c8..9b136cfcc2a 100644 --- a/test/parallel/test-async-hooks-prevent-double-destroy.js +++ b/test/parallel/test-async-hooks-prevent-double-destroy.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose_gc +// Flags: --enable-async-hooks --expose_gc // This test ensures that userland-only AsyncResources cause a destroy event to // be emitted when they get gced. diff --git a/test/parallel/test-async-hooks-promise-enable-disable.js b/test/parallel/test-async-hooks-promise-enable-disable.js index 92504ac121e..6e1e9135555 100644 --- a/test/parallel/test-async-hooks-promise-enable-disable.js +++ b/test/parallel/test-async-hooks-promise-enable-disable.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/parallel/test-async-hooks-promise-triggerid.js b/test/parallel/test-async-hooks-promise-triggerid.js index 89e5bc1464f..7193223b475 100644 --- a/test/parallel/test-async-hooks-promise-triggerid.js +++ b/test/parallel/test-async-hooks-promise-triggerid.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); const async_hooks = require('async_hooks'); diff --git a/test/parallel/test-async-hooks-promise.js b/test/parallel/test-async-hooks-promise.js index 74f72a18824..d05bef69e9e 100644 --- a/test/parallel/test-async-hooks-promise.js +++ b/test/parallel/test-async-hooks-promise.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); const async_hooks = require('async_hooks'); diff --git a/test/parallel/test-async-hooks-recursive-stack-runInAsyncScope.js b/test/parallel/test-async-hooks-recursive-stack-runInAsyncScope.js index d0d5ee76e9d..b71927724c9 100644 --- a/test/parallel/test-async-hooks-recursive-stack-runInAsyncScope.js +++ b/test/parallel/test-async-hooks-recursive-stack-runInAsyncScope.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); const async_hooks = require('async_hooks'); diff --git a/test/parallel/test-async-hooks-run-in-async-scope-caught-exception.js b/test/parallel/test-async-hooks-run-in-async-scope-caught-exception.js index e38cefd20e4..28108543b56 100644 --- a/test/parallel/test-async-hooks-run-in-async-scope-caught-exception.js +++ b/test/parallel/test-async-hooks-run-in-async-scope-caught-exception.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks require('../common'); const { AsyncResource } = require('async_hooks'); diff --git a/test/parallel/test-async-hooks-run-in-async-scope-this-arg.js b/test/parallel/test-async-hooks-run-in-async-scope-this-arg.js index a5016da9d5f..a59b4595f42 100644 --- a/test/parallel/test-async-hooks-run-in-async-scope-this-arg.js +++ b/test/parallel/test-async-hooks-run-in-async-scope-this-arg.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks // Test that passing thisArg to runInAsyncScope() works. diff --git a/test/parallel/test-async-hooks-stack-overflow-nested-async.js b/test/parallel/test-async-hooks-stack-overflow-nested-async.js index faa457e211f..87b30711f5f 100644 --- a/test/parallel/test-async-hooks-stack-overflow-nested-async.js +++ b/test/parallel/test-async-hooks-stack-overflow-nested-async.js @@ -66,7 +66,7 @@ if (process.argv[2] === 'child') { // Parent process - spawn the child and check exit code spawnSyncAndAssert( process.execPath, - [__filename, 'child'], + ['--enable-async-hooks', __filename, 'child'], { encoding: 'utf8', timeout: 30000 }, { stdout: /SUCCESS: try-catch caught the stack overflow/, diff --git a/test/parallel/test-async-hooks-stack-overflow-try-catch.js b/test/parallel/test-async-hooks-stack-overflow-try-catch.js index 43338905e78..f25dcbb9fd1 100644 --- a/test/parallel/test-async-hooks-stack-overflow-try-catch.js +++ b/test/parallel/test-async-hooks-stack-overflow-try-catch.js @@ -35,7 +35,7 @@ if (process.argv[2] === 'child') { // Parent process - spawn the child and check exit code const result = spawnSync( process.execPath, - [__filename, 'child'], + ['--enable-async-hooks', __filename, 'child'], { encoding: 'utf8', timeout: 30000 } ); diff --git a/test/parallel/test-async-hooks-stack-overflow.js b/test/parallel/test-async-hooks-stack-overflow.js index aff41969dbd..ea00b07c241 100644 --- a/test/parallel/test-async-hooks-stack-overflow.js +++ b/test/parallel/test-async-hooks-stack-overflow.js @@ -34,7 +34,7 @@ if (process.argv[2] === 'child') { // Parent process - spawn the child and check exit code const result = spawnSync( process.execPath, - [__filename, 'child'], + ['--enable-async-hooks', __filename, 'child'], { encoding: 'utf8', timeout: 30000 } ); diff --git a/test/parallel/test-async-hooks-top-level-clearimmediate.js b/test/parallel/test-async-hooks-top-level-clearimmediate.js index fd91fefa9c4..b966560ba6f 100644 --- a/test/parallel/test-async-hooks-top-level-clearimmediate.js +++ b/test/parallel/test-async-hooks-top-level-clearimmediate.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks // Regression test for https://github.com/nodejs/node/issues/13262 diff --git a/test/parallel/test-async-hooks-vm-gc.js b/test/parallel/test-async-hooks-vm-gc.js index da95e3579dc..cef7b4ed269 100644 --- a/test/parallel/test-async-hooks-vm-gc.js +++ b/test/parallel/test-async-hooks-vm-gc.js @@ -1,4 +1,4 @@ -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc 'use strict'; require('../common'); diff --git a/test/parallel/test-async-hooks-worker-asyncfn-terminate-1.js b/test/parallel/test-async-hooks-worker-asyncfn-terminate-1.js index eb166459130..fcb1dad5262 100644 --- a/test/parallel/test-async-hooks-worker-asyncfn-terminate-1.js +++ b/test/parallel/test-async-hooks-worker-asyncfn-terminate-1.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const { Worker } = require('worker_threads'); diff --git a/test/parallel/test-async-hooks-worker-asyncfn-terminate-2.js b/test/parallel/test-async-hooks-worker-asyncfn-terminate-2.js index 049264d3e8a..75f00029caf 100644 --- a/test/parallel/test-async-hooks-worker-asyncfn-terminate-2.js +++ b/test/parallel/test-async-hooks-worker-asyncfn-terminate-2.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const { Worker } = require('worker_threads'); diff --git a/test/parallel/test-async-hooks-worker-asyncfn-terminate-3.js b/test/parallel/test-async-hooks-worker-asyncfn-terminate-3.js index 40c7d85835a..1dc2cc5ba08 100644 --- a/test/parallel/test-async-hooks-worker-asyncfn-terminate-3.js +++ b/test/parallel/test-async-hooks-worker-asyncfn-terminate-3.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const { Worker } = require('worker_threads'); diff --git a/test/parallel/test-async-hooks-worker-asyncfn-terminate-4.js b/test/parallel/test-async-hooks-worker-asyncfn-terminate-4.js index c522091006c..b6136ac9cc7 100644 --- a/test/parallel/test-async-hooks-worker-asyncfn-terminate-4.js +++ b/test/parallel/test-async-hooks-worker-asyncfn-terminate-4.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); const { Worker } = require('worker_threads'); diff --git a/test/parallel/test-async-local-storage-http-parser-leak.js b/test/parallel/test-async-local-storage-http-parser-leak.js index b55aed8717a..2d8f3e5a9d6 100644 --- a/test/parallel/test-async-local-storage-http-parser-leak.js +++ b/test/parallel/test-async-local-storage-http-parser-leak.js @@ -1,4 +1,4 @@ -// Flags: --expose-gc --expose-internals +// Flags: --enable-async-hooks --expose-gc --expose-internals 'use strict'; diff --git a/test/parallel/test-async-wrap-constructor.js b/test/parallel/test-async-wrap-constructor.js index 853898aa0ad..8ef02e93699 100644 --- a/test/parallel/test-async-wrap-constructor.js +++ b/test/parallel/test-async-wrap-constructor.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks // This tests that using falsy values in createHook throws an error. diff --git a/test/parallel/test-async-wrap-destroyid.js b/test/parallel/test-async-wrap-destroyid.js index 25a33246e77..6a1f2e26227 100644 --- a/test/parallel/test-async-wrap-destroyid.js +++ b/test/parallel/test-async-wrap-destroyid.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals 'use strict'; const common = require('../common'); diff --git a/test/parallel/test-async-wrap-pop-id-during-load.js b/test/parallel/test-async-wrap-pop-id-during-load.js index bd53c4d7893..c2c27c2cc54 100644 --- a/test/parallel/test-async-wrap-pop-id-during-load.js +++ b/test/parallel/test-async-wrap-pop-id-during-load.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks require('../common'); diff --git a/test/parallel/test-async-wrap-promise-after-enabled.js b/test/parallel/test-async-wrap-promise-after-enabled.js index fafb732c289..bfe2b0ca858 100644 --- a/test/parallel/test-async-wrap-promise-after-enabled.js +++ b/test/parallel/test-async-wrap-promise-after-enabled.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks // Regression test for https://github.com/nodejs/node/issues/13237 diff --git a/test/parallel/test-async-wrap-tlssocket-asyncreset.js b/test/parallel/test-async-wrap-tlssocket-asyncreset.js index 513911978b1..b6b300453bb 100644 --- a/test/parallel/test-async-wrap-tlssocket-asyncreset.js +++ b/test/parallel/test-async-wrap-tlssocket-asyncreset.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); if (!common.hasCrypto) common.skip('missing crypto'); const fixtures = require('../common/fixtures'); diff --git a/test/parallel/test-async-wrap-trigger-id.js b/test/parallel/test-async-wrap-trigger-id.js index e8b4682b716..2b6455be98a 100644 --- a/test/parallel/test-async-wrap-trigger-id.js +++ b/test/parallel/test-async-wrap-trigger-id.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks require('../common'); const assert = require('assert'); diff --git a/test/parallel/test-async-wrap-uncaughtexception.js b/test/parallel/test-async-wrap-uncaughtexception.js index 37557b4aacc..6afa94bff80 100644 --- a/test/parallel/test-async-wrap-uncaughtexception.js +++ b/test/parallel/test-async-wrap-uncaughtexception.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); if (!common.hasCrypto) diff --git a/test/parallel/test-cli-node-options.js b/test/parallel/test-cli-node-options.js index fa290d23c96..ec7680e4eff 100644 --- a/test/parallel/test-cli-node-options.js +++ b/test/parallel/test-cli-node-options.js @@ -33,7 +33,7 @@ expect('--no-extra-info-on-fatal-exception', 'B\n'); expect('--redirect-warnings=_', 'B\n'); expect('--trace-deprecation', 'B\n'); expect('--trace-sync-io', 'B\n'); -expectNoWorker('--trace-events-enabled', 'B\n'); +expectNoWorker('--trace-events-enabled --enable-async-hooks', 'B\n'); expect('--track-heap-objects', 'B\n'); expect('--throw-deprecation', /.*DeprecationWarning: Buffer\(\) is deprecated due to security and usability issues.*/, @@ -49,7 +49,7 @@ expectNoWorker( ); // eslint-disable-next-line no-template-curly-in-string expectNoWorker('--trace-event-file-pattern {pid}-${rotation}.trace_events ' + - '--trace-event-categories node.async_hooks', 'B\n'); + '--trace-event-categories node.async_hooks --enable-async-hooks', 'B\n'); expect('--unhandled-rejections=none', 'B\n'); if (common.isLinux) { diff --git a/test/parallel/test-common-gc.js b/test/parallel/test-common-gc.js index 54abe3695cc..c205ee89f9c 100644 --- a/test/parallel/test-common-gc.js +++ b/test/parallel/test-common-gc.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc const common = require('../common'); const { onGC } = require('../common/gc'); diff --git a/test/parallel/test-diagnostics-channel-memory-leak.js b/test/parallel/test-diagnostics-channel-memory-leak.js index 06301847ed6..c0f6f8d812c 100644 --- a/test/parallel/test-diagnostics-channel-memory-leak.js +++ b/test/parallel/test-diagnostics-channel-memory-leak.js @@ -1,4 +1,4 @@ -// Flags: --max-old-space-size=16 +// Flags: --enable-async-hooks --max-old-space-size=16 'use strict'; // This test ensures that diagnostic channel references aren't leaked. diff --git a/test/parallel/test-emit-after-uncaught-exception.js b/test/parallel/test-emit-after-uncaught-exception.js index 5003972e998..6eb47fcd87e 100644 --- a/test/parallel/test-emit-after-uncaught-exception.js +++ b/test/parallel/test-emit-after-uncaught-exception.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); diff --git a/test/parallel/test-eventemitter-asyncresource.js b/test/parallel/test-eventemitter-asyncresource.js index fe7839a1bb2..4de57d09fc3 100644 --- a/test/parallel/test-eventemitter-asyncresource.js +++ b/test/parallel/test-eventemitter-asyncresource.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const { EventEmitterAsyncResource } = require('events'); diff --git a/test/parallel/test-fs-cp-async-destination-appears-late.mjs b/test/parallel/test-fs-cp-async-destination-appears-late.mjs index 40ec15e1ef3..dd777678bdb 100644 --- a/test/parallel/test-fs-cp-async-destination-appears-late.mjs +++ b/test/parallel/test-fs-cp-async-destination-appears-late.mjs @@ -1,3 +1,4 @@ +// Flags: --enable-async-hooks // This tests that cp() into a destination that did not exist when it was // checked, but does by the time the copy starts, fails with EEXIST instead // of copying through whatever appeared there. diff --git a/test/parallel/test-fs-existssync-memleak-longpath.js b/test/parallel/test-fs-existssync-memleak-longpath.js index 90617badc96..ebfe395c4f1 100644 --- a/test/parallel/test-fs-existssync-memleak-longpath.js +++ b/test/parallel/test-fs-existssync-memleak-longpath.js @@ -1,4 +1,4 @@ -// Flags: --expose-gc --expose-internals +// Flags: --enable-async-hooks --expose-gc --expose-internals 'use strict'; const common = require('../common'); diff --git a/test/parallel/test-fs-readfile-one-roundtrip.js b/test/parallel/test-fs-readfile-one-roundtrip.js index 87df04795d8..a0d423426a8 100644 --- a/test/parallel/test-fs-readfile-one-roundtrip.js +++ b/test/parallel/test-fs-readfile-one-roundtrip.js @@ -1,3 +1,4 @@ +// Flags: --enable-async-hooks 'use strict'; // fs.readFile()/fs.promises.readFile() read files of up to one chunk // (512 KiB) with a single thread pool round trip and hand larger files over diff --git a/test/parallel/test-gc-http-client-connaborted.js b/test/parallel/test-gc-http-client-connaborted.js index e52a555d788..3ce541f2684 100644 --- a/test/parallel/test-gc-http-client-connaborted.js +++ b/test/parallel/test-gc-http-client-connaborted.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc // just like test-gc-http-client.js, // but aborting every connection that comes in. diff --git a/test/parallel/test-gc-net-timeout.js b/test/parallel/test-gc-net-timeout.js index 0fc93bfc62c..d621b39ce36 100644 --- a/test/parallel/test-gc-net-timeout.js +++ b/test/parallel/test-gc-net-timeout.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc // just like test-gc-http-client-timeout.js, // but using a net server/client instead diff --git a/test/parallel/test-gc-tls-external-memory.js b/test/parallel/test-gc-tls-external-memory.js index 480b1086b53..d4bb4c0c7a6 100644 --- a/test/parallel/test-gc-tls-external-memory.js +++ b/test/parallel/test-gc-tls-external-memory.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc // Tests that memoryUsage().external doesn't go negative // when a lot tls connections are opened and closed diff --git a/test/parallel/test-heapdump-async-hooks-init-promise.js b/test/parallel/test-heapdump-async-hooks-init-promise.js index 80d6309deab..086d5bc1a78 100644 --- a/test/parallel/test-heapdump-async-hooks-init-promise.js +++ b/test/parallel/test-heapdump-async-hooks-init-promise.js @@ -1,4 +1,4 @@ -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc 'use strict'; const common = require('../common'); const assert = require('assert'); diff --git a/test/parallel/test-http-agent-domain-reused-gc.js b/test/parallel/test-http-agent-domain-reused-gc.js index 4f12c2ede83..abad3d8f16d 100644 --- a/test/parallel/test-http-agent-domain-reused-gc.js +++ b/test/parallel/test-http-agent-domain-reused-gc.js @@ -1,4 +1,4 @@ -// Flags: --expose-gc --expose-internals +// Flags: --enable-async-hooks --expose-gc --expose-internals 'use strict'; const common = require('../common'); const http = require('http'); diff --git a/test/parallel/test-http-client-leaky-with-double-response.js b/test/parallel/test-http-client-leaky-with-double-response.js index a569b3bb31b..03ba6abe8f7 100644 --- a/test/parallel/test-http-client-leaky-with-double-response.js +++ b/test/parallel/test-http-client-leaky-with-double-response.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc const common = require('../common'); const http = require('http'); const assert = require('assert'); diff --git a/test/parallel/test-http-server-connections-checking-leak.js b/test/parallel/test-http-server-connections-checking-leak.js index 38dca83102c..4070d1a17bc 100644 --- a/test/parallel/test-http-server-connections-checking-leak.js +++ b/test/parallel/test-http-server-connections-checking-leak.js @@ -1,6 +1,6 @@ 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc // Check that creating a server without listening does not leak resources. diff --git a/test/parallel/test-http-server-keepalive-req-gc.js b/test/parallel/test-http-server-keepalive-req-gc.js index c827cd19ad7..8850a800e87 100644 --- a/test/parallel/test-http-server-keepalive-req-gc.js +++ b/test/parallel/test-http-server-keepalive-req-gc.js @@ -1,4 +1,4 @@ -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc 'use strict'; const common = require('../common'); const { onGC } = require('../common/gc'); diff --git a/test/parallel/test-http-uncaught-from-request-callback.js b/test/parallel/test-http-uncaught-from-request-callback.js index 5c759586178..86f5b2a73ea 100644 --- a/test/parallel/test-http-uncaught-from-request-callback.js +++ b/test/parallel/test-http-uncaught-from-request-callback.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const asyncHooks = require('async_hooks'); const http = require('http'); diff --git a/test/parallel/test-http2-debug.js b/test/parallel/test-http2-debug.js index c7252317be3..49a7f1468b1 100644 --- a/test/parallel/test-http2-debug.js +++ b/test/parallel/test-http2-debug.js @@ -15,6 +15,7 @@ const stderrFile = tmpdir.resolve('stderr.log'); const stderrFd = fs.openSync(stderrFile, 'w'); try { spawnSyncAndAssert(process.execPath, [ + '--enable-async-hooks', path.resolve(__dirname, 'test-http2-ping.js'), ], { env: { diff --git a/test/parallel/test-http2-ping.js b/test/parallel/test-http2-ping.js index 90ef57e03cc..0d0958f88b7 100644 --- a/test/parallel/test-http2-ping.js +++ b/test/parallel/test-http2-ping.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); if (!common.hasCrypto) diff --git a/test/parallel/test-https-server-connections-checking-leak.js b/test/parallel/test-https-server-connections-checking-leak.js index f79149ef70a..b9d3dbc2e49 100644 --- a/test/parallel/test-https-server-connections-checking-leak.js +++ b/test/parallel/test-https-server-connections-checking-leak.js @@ -1,6 +1,6 @@ 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc // Check that creating a server without listening does not leak resources. diff --git a/test/parallel/test-inspector-contexts.js b/test/parallel/test-inspector-contexts.js index 9ab2c515b4a..39749dfcca3 100644 --- a/test/parallel/test-inspector-contexts.js +++ b/test/parallel/test-inspector-contexts.js @@ -1,6 +1,6 @@ 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc const common = require('../common'); common.skipIfInspectorDisabled(); diff --git a/test/parallel/test-inspector-debug-async-hook.js b/test/parallel/test-inspector-debug-async-hook.js index e8f13b40c7e..fa2bb5f7143 100644 --- a/test/parallel/test-inspector-debug-async-hook.js +++ b/test/parallel/test-inspector-debug-async-hook.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); common.skipIfInspectorDisabled(); const test = require('node:test'); diff --git a/test/parallel/test-internal-util-weakreference.js b/test/parallel/test-internal-util-weakreference.js index 0ecef956a44..d98839093c7 100644 --- a/test/parallel/test-internal-util-weakreference.js +++ b/test/parallel/test-internal-util-weakreference.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals --expose-gc +// Flags: --enable-async-hooks --expose-internals --expose-gc 'use strict'; require('../common'); const { gcUntil } = require('../common/gc'); diff --git a/test/parallel/test-messageport-hasref.js b/test/parallel/test-messageport-hasref.js index 563a598b365..b2a810acbec 100644 --- a/test/parallel/test-messageport-hasref.js +++ b/test/parallel/test-messageport-hasref.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const { MessageChannel } = require('worker_threads'); diff --git a/test/parallel/test-net-connect-memleak.js b/test/parallel/test-net-connect-memleak.js index de925f5d08c..e329722c2b7 100644 --- a/test/parallel/test-net-connect-memleak.js +++ b/test/parallel/test-net-connect-memleak.js @@ -20,7 +20,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc const common = require('../common'); const { onGC } = require('../common/gc'); diff --git a/test/parallel/test-primitive-timer-leak.js b/test/parallel/test-primitive-timer-leak.js index a0fe2765e12..a231012b328 100644 --- a/test/parallel/test-primitive-timer-leak.js +++ b/test/parallel/test-primitive-timer-leak.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc require('../common'); const { onGC } = require('../common/gc'); diff --git a/test/parallel/test-promise-hook-create-hook.js b/test/parallel/test-promise-hook-create-hook.js index 543f67da486..95820419ef4 100644 --- a/test/parallel/test-promise-hook-create-hook.js +++ b/test/parallel/test-promise-hook-create-hook.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); const { promiseHooks } = require('v8'); diff --git a/test/parallel/test-queue-microtask-uncaught-asynchooks.js b/test/parallel/test-queue-microtask-uncaught-asynchooks.js index 35b3d9fa309..f2f39707023 100644 --- a/test/parallel/test-queue-microtask-uncaught-asynchooks.js +++ b/test/parallel/test-queue-microtask-uncaught-asynchooks.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const assert = require('assert'); const async_hooks = require('async_hooks'); diff --git a/test/parallel/test-shadow-realm-gc-module.js b/test/parallel/test-shadow-realm-gc-module.js index 6077bf03146..c0109f495c1 100644 --- a/test/parallel/test-shadow-realm-gc-module.js +++ b/test/parallel/test-shadow-realm-gc-module.js @@ -1,4 +1,4 @@ -// Flags: --experimental-shadow-realm --max-old-space-size=20 +// Flags: --enable-async-hooks --experimental-shadow-realm --max-old-space-size=20 'use strict'; /** diff --git a/test/parallel/test-shadow-realm-gc.js b/test/parallel/test-shadow-realm-gc.js index c6de4a32a88..e1a0827256e 100644 --- a/test/parallel/test-shadow-realm-gc.js +++ b/test/parallel/test-shadow-realm-gc.js @@ -1,4 +1,4 @@ -// Flags: --experimental-shadow-realm --max-old-space-size=20 +// Flags: --enable-async-hooks --experimental-shadow-realm --max-old-space-size=20 'use strict'; /** diff --git a/test/parallel/test-source-map-cjs-require-cache.js b/test/parallel/test-source-map-cjs-require-cache.js index 155b7a66efa..3639f64fd34 100644 --- a/test/parallel/test-source-map-cjs-require-cache.js +++ b/test/parallel/test-source-map-cjs-require-cache.js @@ -1,4 +1,4 @@ -// Flags: --enable-source-maps --max-old-space-size=10 --expose-gc +// Flags: --enable-async-hooks --enable-source-maps --max-old-space-size=10 --expose-gc /** * This test verifies that the source map of a CJS module is cleared after the diff --git a/test/parallel/test-sqlite-session.js b/test/parallel/test-sqlite-session.js index e5a478e1706..7ef87e3597e 100644 --- a/test/parallel/test-sqlite-session.js +++ b/test/parallel/test-sqlite-session.js @@ -1,4 +1,4 @@ -// Flags: --expose-gc --experimental-sqlite +// Flags: --enable-async-hooks --expose-gc --experimental-sqlite 'use strict'; const { skipIfSQLiteMissing } = require('../common'); skipIfSQLiteMissing(); diff --git a/test/parallel/test-stream-finished-bindAsyncResource-path.js b/test/parallel/test-stream-finished-bindAsyncResource-path.js index 82de2947c29..941f88d4271 100644 --- a/test/parallel/test-stream-finished-bindAsyncResource-path.js +++ b/test/parallel/test-stream-finished-bindAsyncResource-path.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals 'use strict'; const common = require('../common'); diff --git a/test/parallel/test-stream-writable-samecb-singletick.js b/test/parallel/test-stream-writable-samecb-singletick.js index 9357a56535b..ca5553e0dc9 100644 --- a/test/parallel/test-stream-writable-samecb-singletick.js +++ b/test/parallel/test-stream-writable-samecb-singletick.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const { Console } = require('console'); const { Writable } = require('stream'); diff --git a/test/parallel/test-tls-connect-memleak.js b/test/parallel/test-tls-connect-memleak.js index 220ea4a9248..90f2d6512cc 100644 --- a/test/parallel/test-tls-connect-memleak.js +++ b/test/parallel/test-tls-connect-memleak.js @@ -20,7 +20,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc const common = require('../common'); if (!common.hasCrypto) diff --git a/test/parallel/test-trace-events-async-hooks-dynamic.js b/test/parallel/test-trace-events-async-hooks-dynamic.js index 8c6ef26e638..0d56e2d2aa9 100644 --- a/test/parallel/test-trace-events-async-hooks-dynamic.js +++ b/test/parallel/test-trace-events-async-hooks-dynamic.js @@ -29,7 +29,7 @@ const filename = tmpdir.resolve('node_trace.1.log'); tmpdir.refresh(); const proc = cp.spawnSync( process.execPath, - ['-e', enable + code ], + ['--enable-async-hooks', '-e', enable + code ], { cwd: tmpdir.path, env: { ...process.env, diff --git a/test/parallel/test-trace-events-perfetto-pftrace.js b/test/parallel/test-trace-events-perfetto-pftrace.js index 8cfcb52182d..63a3cf11263 100644 --- a/test/parallel/test-trace-events-perfetto-pftrace.js +++ b/test/parallel/test-trace-events-perfetto-pftrace.js @@ -17,7 +17,7 @@ tmpdir.refresh(); const FILE_NAME = tmpdir.resolve('node_trace.1.pftrace'); const proc = cp.spawn(process.execPath, - [ '--trace-events-enabled', '-e', CODE ], + [ '--enable-async-hooks', '--trace-events-enabled', '-e', CODE ], { cwd: tmpdir.path }); proc.once('exit', common.mustCall(() => { diff --git a/test/parallel/test-util-debounce.js b/test/parallel/test-util-debounce.js index 15d93e61041..04ff7ef6d58 100644 --- a/test/parallel/test-util-debounce.js +++ b/test/parallel/test-util-debounce.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals 'use strict'; const common = require('../common'); diff --git a/test/parallel/test-util-throttle.js b/test/parallel/test-util-throttle.js index b22fb4630e7..4d2fcaa901a 100644 --- a/test/parallel/test-util-throttle.js +++ b/test/parallel/test-util-throttle.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals +// Flags: --enable-async-hooks --expose-internals 'use strict'; const common = require('../common'); diff --git a/test/parallel/test-v8-coverage.js b/test/parallel/test-v8-coverage.js index 0306e07c6fd..470c3387823 100644 --- a/test/parallel/test-v8-coverage.js +++ b/test/parallel/test-v8-coverage.js @@ -153,6 +153,7 @@ function nextdir() { { const coverageDirectory = tmpdir.resolve(nextdir()); const output = spawnSync(process.execPath, [ + '--enable-async-hooks', require.resolve('../fixtures/v8-coverage/async-hooks'), ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } }); if (output.status !== 0) { diff --git a/test/parallel/test-v8-serialize-leak.js b/test/parallel/test-v8-serialize-leak.js index dd51a879eff..af79c6fcea8 100644 --- a/test/parallel/test-v8-serialize-leak.js +++ b/test/parallel/test-v8-serialize-leak.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc const common = require('../common'); const { gcUntil } = require('../common/gc'); diff --git a/test/parallel/test-worker-hasref.js b/test/parallel/test-worker-hasref.js index 936a144bebb..dbec73de150 100644 --- a/test/parallel/test-worker-hasref.js +++ b/test/parallel/test-worker-hasref.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const { Worker } = require('worker_threads'); diff --git a/test/parallel/test-worker-message-port-inspect-during-init-hook.js b/test/parallel/test-worker-message-port-inspect-during-init-hook.js index d5784dceb71..4bc29ac3e5c 100644 --- a/test/parallel/test-worker-message-port-inspect-during-init-hook.js +++ b/test/parallel/test-worker-message-port-inspect-during-init-hook.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const util = require('util'); const assert = require('assert'); diff --git a/test/parallel/test-worker-messageport-hasref.js b/test/parallel/test-worker-messageport-hasref.js index c46feb7c66e..7d301bbcf22 100644 --- a/test/parallel/test-worker-messageport-hasref.js +++ b/test/parallel/test-worker-messageport-hasref.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks const common = require('../common'); const { Worker } = require('worker_threads'); diff --git a/test/parallel/test-zlib-invalid-input-memory.js b/test/parallel/test-zlib-invalid-input-memory.js index ac718395dae..dd15be0d630 100644 --- a/test/parallel/test-zlib-invalid-input-memory.js +++ b/test/parallel/test-zlib-invalid-input-memory.js @@ -1,4 +1,4 @@ -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc 'use strict'; const common = require('../common'); const { onGC } = require('../common/gc'); diff --git a/test/pseudo-tty/test-async-wrap-getasyncid-tty.js b/test/pseudo-tty/test-async-wrap-getasyncid-tty.js index 5995a911811..c984cb247e8 100644 --- a/test/pseudo-tty/test-async-wrap-getasyncid-tty.js +++ b/test/pseudo-tty/test-async-wrap-getasyncid-tty.js @@ -1,4 +1,4 @@ -// Flags: --expose-internals --no-warnings +// Flags: --enable-async-hooks --expose-internals --no-warnings 'use strict'; // See also test/sequential/test-async-wrap-getasyncid.js diff --git a/test/sequential/test-async-wrap-getasyncid.js b/test/sequential/test-async-wrap-getasyncid.js index 2cb15e144ee..0e3ea9147c0 100644 --- a/test/sequential/test-async-wrap-getasyncid.js +++ b/test/sequential/test-async-wrap-getasyncid.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-gc --expose-internals --no-warnings --test-udp-no-try-send +// Flags: --enable-async-hooks --expose-gc --expose-internals --no-warnings --test-udp-no-try-send const common = require('../common'); const { internalBinding } = require('internal/test/binding'); diff --git a/test/sequential/test-gc-http-client-onerror.js b/test/sequential/test-gc-http-client-onerror.js index 1c311c6a9b6..1f5b8fc9b84 100644 --- a/test/sequential/test-gc-http-client-onerror.js +++ b/test/sequential/test-gc-http-client-onerror.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc // just like test-gc-http-client.js, // but with an on('error') handler that does nothing. diff --git a/test/sequential/test-gc-http-client-timeout.js b/test/sequential/test-gc-http-client-timeout.js index bd5589fca10..b5d94bf6141 100644 --- a/test/sequential/test-gc-http-client-timeout.js +++ b/test/sequential/test-gc-http-client-timeout.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc // Like test-gc-http-client.js, but with a timeout set. const common = require('../common'); diff --git a/test/sequential/test-gc-http-client.js b/test/sequential/test-gc-http-client.js index 6b2a5fc03af..cd0de81c13b 100644 --- a/test/sequential/test-gc-http-client.js +++ b/test/sequential/test-gc-http-client.js @@ -1,5 +1,5 @@ 'use strict'; -// Flags: --expose-gc +// Flags: --enable-async-hooks --expose-gc // just a simple http server and client. const common = require('../common'); diff --git a/test/trace_events/test-trace-events-all.js b/test/trace_events/test-trace-events-all.js index fe422d03a77..00bedb68d99 100644 --- a/test/trace_events/test-trace-events-all.js +++ b/test/trace_events/test-trace-events-all.js @@ -21,7 +21,7 @@ tmpdir.refresh(); const FILE_NAME = tmpdir.resolve(defaultTraceFileName); const proc = cp.spawn(process.execPath, - [ '--trace-events-enabled', '-e', CODE ], + [ '--enable-async-hooks', '--trace-events-enabled', '-e', CODE ], { cwd: tmpdir.path }); proc.once('exit', common.mustCall(() => { diff --git a/test/trace_events/test-trace-events-async-hooks-worker.js b/test/trace_events/test-trace-events-async-hooks-worker.js index 4fdeb2138eb..ddde1ea569f 100644 --- a/test/trace_events/test-trace-events-async-hooks-worker.js +++ b/test/trace_events/test-trace-events-async-hooks-worker.js @@ -40,7 +40,7 @@ const filename = tmpdir.resolve(defaultTraceFileName); tmpdir.refresh(); const proc = cp.spawnSync( process.execPath, - [ '--trace-event-categories', 'node.async_hooks', '-e', worker ], + [ '--enable-async-hooks', '--trace-event-categories', 'node.async_hooks', '-e', worker ], { cwd: tmpdir.path, env: { ...process.env, diff --git a/test/trace_events/test-trace-events-async-hooks.js b/test/trace_events/test-trace-events-async-hooks.js index c404402594a..b931bf39e2b 100644 --- a/test/trace_events/test-trace-events-async-hooks.js +++ b/test/trace_events/test-trace-events-async-hooks.js @@ -22,7 +22,7 @@ tmpdir.refresh(); const FILE_NAME = tmpdir.resolve(defaultTraceFileName); const proc = cp.spawn(process.execPath, - [ '--trace-event-categories', 'node.async_hooks', + [ '--enable-async-hooks', '--trace-event-categories', 'node.async_hooks', '-e', CODE ], { cwd: tmpdir.path }); diff --git a/test/trace_events/test-trace-events-file-pattern.js b/test/trace_events/test-trace-events-file-pattern.js index 4b053aeb60c..90bee8e2cc3 100644 --- a/test/trace_events/test-trace-events-file-pattern.js +++ b/test/trace_events/test-trace-events-file-pattern.js @@ -18,6 +18,7 @@ const CODE = 'setTimeout(() => { for (let i = 0; i < 100000; i++) { "test" + i } }, 1)'; const proc = cp.spawn(process.execPath, [ + '--enable-async-hooks', '--trace-events-enabled', '--trace-event-file-pattern', // eslint-disable-next-line no-template-curly-in-string diff --git a/test/trace_events/test-trace-events-process-exit.js b/test/trace_events/test-trace-events-process-exit.js index cb09c1e3cdc..e5946989eea 100644 --- a/test/trace_events/test-trace-events-process-exit.js +++ b/test/trace_events/test-trace-events-process-exit.js @@ -17,7 +17,7 @@ tmpdir.refresh(); const FILE_NAME = tmpdir.resolve(defaultTraceFileName); const proc = cp.spawn(process.execPath, - [ '--trace-events-enabled', + [ '--enable-async-hooks', '--trace-events-enabled', '-e', 'process.exit()' ], { cwd: tmpdir.path }); From 553778885ee1f76914d28f0a72d5fb78864a9573 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 30 Oct 2026 16:58:12 +0000 Subject: [PATCH 2/8] async_hooks: inline internal inspector async hook The internal AsyncHook class was added to internal/async_hooks solely so the inspector could create a hook without --enable-async-hooks, complete with ERR_ASYNC_CALLBACK validation that is not needed for an internal-only consumer. The inspector is the only internal consumer (the node.async_hooks trace event category intentionally still requires the flag), so register its hook directly from internal/inspector_async_hook instead. Since the inspector hook always registers init/before/after/destroy, the generic class and its validation add no value. Remove it and inline the minimal wiring. Refs: https://github.com/nodejs/node/pull/61030 Signed-off-by: Matteo Collina Assisted-by: pi --- lib/internal/async_hooks.js | 79 --------------------------- lib/internal/inspector_async_hook.js | 82 +++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 87 deletions(-) diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index 01b232ac2a8..8a60b9a6ce5 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -1,11 +1,7 @@ 'use strict'; const { - ArrayPrototypeIncludes, - ArrayPrototypeIndexOf, - ArrayPrototypePush, ArrayPrototypeSlice, - ArrayPrototypeSplice, ErrorCaptureStackTrace, ObjectDefineProperty, ObjectPrototypeHasOwnProperty, @@ -14,7 +10,6 @@ const { } = primordials; const { exitCodes: { kGenericUserError } } = internalBinding('errors'); -const { ERR_ASYNC_CALLBACK } = require('internal/errors').codes; const async_wrap = internalBinding('async_wrap'); const { setCallbackTrampoline } = async_wrap; @@ -581,83 +576,9 @@ function triggerAsyncId() { const kNoPromiseHook = Symbol('kNoPromiseHook'); -// Minimal internal async hook used by Node.js internals (e.g. the inspector) -// that must create hooks without --enable-async-hooks. Unlike the public -// async_hooks.createHook() this does not support promise callbacks. -class AsyncHook { - constructor({ init, before, after, destroy }) { - if (init !== undefined && typeof init !== 'function') - throw new ERR_ASYNC_CALLBACK('hook.init'); - if (before !== undefined && typeof before !== 'function') - throw new ERR_ASYNC_CALLBACK('hook.before'); - if (after !== undefined && typeof after !== 'function') - throw new ERR_ASYNC_CALLBACK('hook.after'); - if (destroy !== undefined && typeof destroy !== 'function') - throw new ERR_ASYNC_CALLBACK('hook.destroy'); - - // Internal hooks never track promises. - this[kNoPromiseHook] = true; - this[init_symbol] = init; - this[before_symbol] = before; - this[after_symbol] = after; - this[destroy_symbol] = destroy; - } - - enable() { - const { 0: hooks_array, 1: hook_fields } = getHookArrays(); - - if (ArrayPrototypeIncludes(hooks_array, this)) - return this; - - const prev_kTotals = hook_fields[kTotals]; - - hook_fields[kTotals] = hook_fields[kInit] += +!!this[init_symbol]; - hook_fields[kTotals] += hook_fields[kBefore] += +!!this[before_symbol]; - hook_fields[kTotals] += hook_fields[kAfter] += +!!this[after_symbol]; - hook_fields[kTotals] += hook_fields[kDestroy] += +!!this[destroy_symbol]; - ArrayPrototypePush(hooks_array, this); - - if (prev_kTotals === 0 && hook_fields[kTotals] > 0) { - enableHooks(); - } - - return this; - } - - disable() { - const { 0: hooks_array, 1: hook_fields } = getHookArrays(); - - const index = ArrayPrototypeIndexOf(hooks_array, this); - if (index === -1) - return this; - - const prev_kTotals = hook_fields[kTotals]; - - hook_fields[kTotals] = hook_fields[kInit] -= +!!this[init_symbol]; - hook_fields[kTotals] += hook_fields[kBefore] -= +!!this[before_symbol]; - hook_fields[kTotals] += hook_fields[kAfter] -= +!!this[after_symbol]; - hook_fields[kTotals] += hook_fields[kDestroy] -= +!!this[destroy_symbol]; - ArrayPrototypeSplice(hooks_array, index, 1); - - if (prev_kTotals > 0 && hook_fields[kTotals] === 0) { - disableHooks(); - } - - return this; - } -} - -// Creates an internal AsyncHook without honoring --enable-async-hooks. Only for -// use by Node.js internals; userland goes through async_hooks.createHook(). -function createHook(fns) { - return new AsyncHook(fns); -} - module.exports = { executionAsyncId, triggerAsyncId, - // Create an async hook without honoring --enable-async-hooks (internals only). - createHook, // Private API getHookArrays, symbols: { diff --git a/lib/internal/inspector_async_hook.js b/lib/internal/inspector_async_hook.js index 4bafef870b7..4d5dd1b2fbe 100644 --- a/lib/internal/inspector_async_hook.js +++ b/lib/internal/inspector_async_hook.js @@ -1,15 +1,41 @@ 'use strict'; +const { + ArrayPrototypeIncludes, + ArrayPrototypeIndexOf, + ArrayPrototypePush, + ArrayPrototypeSplice, +} = primordials; + +const { + getHookArrays, + enableHooks, + disableHooks, + symbols: { + init_symbol, + before_symbol, + after_symbol, + destroy_symbol, + }, + constants: { + kInit, + kBefore, + kAfter, + kDestroy, + kTotals, + }, + kNoPromiseHook, +} = require('internal/async_hooks'); + let hook; let config; function lazyHookCreation() { const inspector = internalBinding('inspector'); - const { createHook } = require('internal/async_hooks'); config = internalBinding('config'); - hook = createHook({ - init(asyncId, type, triggerAsyncId, resource) { + hook = { + [init_symbol](asyncId, type, triggerAsyncId, resource) { // It's difficult to tell which tasks will be recurring and which won't, // therefore we mark all tasks as recurring. Based on the discussion // in https://github.com/nodejs/node/pull/13870#discussion_r124515293, @@ -18,19 +44,59 @@ function lazyHookCreation() { inspector.asyncTaskScheduled(type, asyncId, recurring); }, - before(asyncId) { + [before_symbol](asyncId) { inspector.asyncTaskStarted(asyncId); }, - after(asyncId) { + [after_symbol](asyncId) { inspector.asyncTaskFinished(asyncId); }, - destroy(asyncId) { + [destroy_symbol](asyncId) { inspector.asyncTaskCanceled(asyncId); }, - trackPromises: false, - }); + + // The inspector does not want promise hook callbacks. + [kNoPromiseHook]: true, + + enable() { + const { 0: hooks_array, 1: hook_fields } = getHookArrays(); + + // Each hook is only allowed to be added once. + if (ArrayPrototypeIncludes(hooks_array, this)) + return; + + // This hook always registers all four callbacks, so each count is + // incremented exactly once. + const prev_kTotals = hook_fields[kTotals]; + hook_fields[kTotals] = hook_fields[kInit] += 1; + hook_fields[kTotals] += hook_fields[kBefore] += 1; + hook_fields[kTotals] += hook_fields[kAfter] += 1; + hook_fields[kTotals] += hook_fields[kDestroy] += 1; + ArrayPrototypePush(hooks_array, this); + + if (prev_kTotals === 0 && hook_fields[kTotals] > 0) + enableHooks(); + }, + + disable() { + const { 0: hooks_array, 1: hook_fields } = getHookArrays(); + + const index = ArrayPrototypeIndexOf(hooks_array, this); + if (index === -1) + return; + + const prev_kTotals = hook_fields[kTotals]; + hook_fields[kTotals] = hook_fields[kInit] -= 1; + hook_fields[kTotals] += hook_fields[kBefore] -= 1; + hook_fields[kTotals] += hook_fields[kAfter] -= 1; + hook_fields[kTotals] += hook_fields[kDestroy] -= 1; + ArrayPrototypeSplice(hooks_array, index, 1); + + if (prev_kTotals > 0 && hook_fields[kTotals] === 0) + disableHooks(); + }, + }; } function enable() { From 39857ff886ba45c478402e9eac014349d71f324d Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 30 Oct 2026 17:42:50 +0000 Subject: [PATCH 3/8] doc: fix link ordering in errors.md The nodejs-links remark lint rule requires link definitions to be listed in alphabetical order. The `--enable-async-hooks` link added for the new flag was inserted before `--disable-proto=throw`; move it after so `make lint-md` passes. Refs: https://github.com/nodejs/node/pull/61030 Signed-off-by: Matteo Collina Assisted-by: pi --- doc/api/errors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index c25838ca7b6..f09dc1a7a22 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -4784,8 +4784,8 @@ An error occurred trying to allocate memory. This should never happen. [`"exports"`]: packages.md#exports [`"imports"`]: packages.md#imports [`'uncaughtException'`]: process.md#event-uncaughtexception -[`--enable-async-hooks`]: cli.md#--enable-async-hooks [`--disable-proto=throw`]: cli.md#--disable-protomode +[`--enable-async-hooks`]: cli.md#--enable-async-hooks [`--force-fips`]: cli.md#--force-fips [`--no-addons`]: cli.md#--no-addons [`--unhandled-rejections`]: cli.md#--unhandled-rejectionsmode From 71aadf1b10264e9b8cc9a3cf38cef034f40b4031 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sat, 31 Oct 2026 08:37:06 +0000 Subject: [PATCH 4/8] doc: keep async-hooks flag docs in alphabetical order The CLI options, the NODE_OPTIONS allowed-flags list, the config schema and the node.1 man page must list options in alphabetical order. Reorder the `--enable-async-hooks` entries added for the new flag so the ordering tests pass, and regenerate the config schema and node.1 man page. Refs: https://github.com/nodejs/node/pull/61030 Signed-off-by: Matteo Collina Assisted-by: pi --- doc/api/cli.md | 82 ++++++++++++++++++------------------- doc/node-config-schema.json | 4 ++ doc/node.1 | 64 ++++++++++++++--------------- 3 files changed, 77 insertions(+), 73 deletions(-) diff --git a/doc/api/cli.md b/doc/api/cli.md index b6e05e57316..45ed6ed18d7 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -139,46 +139,6 @@ Error: Cannot load native addon because loading addons is disabled. } ``` -### `--enable-async-hooks` - - - -Enable use of `async_hooks.createHook()`. This flag is required to enable -the `createHook` API, which is disabled by default. - -Example: - -```mjs -import { createHook } from 'node:async_hooks'; - -createHook({ init() {} }).enable(); -``` - -```cjs -const { createHook } = require('node:async_hooks'); - -createHook({ init() {} }).enable(); -``` - -```console -$ node index.js -node:async_hooks:160 - throw new ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED(); - ^ - -Error [ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED]: async_hooks.createHook() is disabled. Use --enable-async-hooks to enable it. - at createHook (node:async_hooks:160:11) - at Object. (/home/user/index.js:3:1) - ... -``` - -```console -$ node --enable-async-hooks index.js -# Works without error -``` - ### `--allow-child-process` + +Enable use of `async_hooks.createHook()`. This flag is required to enable +the `createHook` API, which is disabled by default. + +Example: + +```mjs +import { createHook } from 'node:async_hooks'; + +createHook({ init() {} }).enable(); +``` + +```cjs +const { createHook } = require('node:async_hooks'); + +createHook({ init() {} }).enable(); +``` + +```console +$ node index.js +node:async_hooks:160 + throw new ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED(); + ^ + +Error [ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED]: async_hooks.createHook() is disabled. Use --enable-async-hooks to enable it. + at createHook (node:async_hooks:160:11) + at Object. (/home/user/index.js:3:1) + ... +``` + +```console +$ node --enable-async-hooks index.js +# Works without error +``` + ### `--enable-fips` * `--allow-addons` -* `--enable-async-hooks` * `--allow-child-process` * `--allow-env` * `--allow-ffi` @@ -4333,6 +4332,7 @@ one is included in the list below. * `--disable-warning` * `--disable-wasm-trap-handler` * `--dns-result-order` +* `--enable-async-hooks` * `--enable-fips-indicator-events` * `--enable-fips` * `--enable-network-family-autoselection` diff --git a/doc/node-config-schema.json b/doc/node-config-schema.json index 0d2cb83b517..3e4910da0cb 100644 --- a/doc/node-config-schema.json +++ b/doc/node-config-schema.json @@ -311,6 +311,10 @@ "type": "string", "description": "set default value of verbatim in dns.lookup. Options are 'ipv4first' (IPv4 addresses are placed before IPv6 addresses) 'ipv6first' (IPv6 addresses are placed before IPv4 addresses) 'verbatim' (addresses are in the order the DNS resolver returned)" }, + "enable-async-hooks": { + "type": "boolean", + "description": "enable use of async_hooks.createHook" + }, "enable-fips": { "type": "boolean", "description": "enable FIPS crypto at startup" diff --git a/doc/node.1 b/doc/node.1 index 2d5a3b98efc..75e7e4c3d60 100644 --- a/doc/node.1 +++ b/doc/node.1 @@ -86,36 +86,6 @@ Error: Cannot load native addon because loading addons is disabled. } .Ed . -.It Fl -enable-async-hooks -Enable use of \fBasync_hooks.createHook()\fR. This flag is required to enable -the \fBcreateHook\fR API, which is disabled by default. -Example: -.Bd -literal -import { createHook } from 'node:async_hooks'; - -createHook({ init() {} }).enable(); -.Ed -.Bd -literal -const { createHook } = require('node:async_hooks'); - -createHook({ init() {} }).enable(); -.Ed -.Bd -literal -$ node index.js -node:async_hooks:160 - throw new ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED(); - ^ - -Error [ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED]: async_hooks.createHook() is disabled. Use --enable-async-hooks to enable it. - at createHook (node:async_hooks:160:11) - at Object. (/home/user/index.js:3:1) - ... -.Ed -.Bd -literal -$ node --enable-async-hooks index.js -# Works without error -.Ed -. .It Fl -allow-child-process When using the Permission Model, the process will not be able to spawn any child process by default. @@ -774,6 +744,36 @@ Set the default value of \fBorder\fR in \fBdns.lookup()\fR and The default is \fBverbatim\fR and \fBdns.setDefaultResultOrder()\fR have higher priority than \fB--dns-result-order\fR. . +.It Fl -enable-async-hooks +Enable use of \fBasync_hooks.createHook()\fR. This flag is required to enable +the \fBcreateHook\fR API, which is disabled by default. +Example: +.Bd -literal +import { createHook } from 'node:async_hooks'; + +createHook({ init() {} }).enable(); +.Ed +.Bd -literal +const { createHook } = require('node:async_hooks'); + +createHook({ init() {} }).enable(); +.Ed +.Bd -literal +$ node index.js +node:async_hooks:160 + throw new ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED(); + ^ + +Error [ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED]: async_hooks.createHook() is disabled. Use --enable-async-hooks to enable it. + at createHook (node:async_hooks:160:11) + at Object. (/home/user/index.js:3:1) + ... +.Ed +.Bd -literal +$ node --enable-async-hooks index.js +# Works without error +.Ed +. .It Fl -enable-fips Enable FIPS mode at startup. A configured provider named \fBfips\fR must be available and initialize successfully. @@ -2216,8 +2216,6 @@ one is included in the list below. .It \fB--allow-addons\fR .It -\fB--enable-async-hooks\fR -.It \fB--allow-child-process\fR .It \fB--allow-env\fR @@ -2274,6 +2272,8 @@ one is included in the list below. .It \fB--dns-result-order\fR .It +\fB--enable-async-hooks\fR +.It \fB--enable-fips-indicator-events\fR .It \fB--enable-fips\fR From 185dcf704c981d9c7bdaac8557d1be0facbf405d Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sat, 31 Oct 2026 08:37:06 +0000 Subject: [PATCH 5/8] test: enable async hooks in async_hooks benchmark The async_hooks benchmarks call async_hooks.createHook(), which now requires `--enable-async-hooks`. Pass it through NODE_OPTIONS when running the benchmark test. Refs: https://github.com/nodejs/node/pull/61030 Signed-off-by: Matteo Collina Assisted-by: pi --- test/benchmark/test-benchmark-async-hooks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/benchmark/test-benchmark-async-hooks.js b/test/benchmark/test-benchmark-async-hooks.js index c9ea2c1e86d..22a0edd2893 100644 --- a/test/benchmark/test-benchmark-async-hooks.js +++ b/test/benchmark/test-benchmark-async-hooks.js @@ -10,4 +10,4 @@ if (!common.enoughTestMem) const runBenchmark = require('../common/benchmark'); -runBenchmark('async_hooks'); +runBenchmark('async_hooks', { NODE_OPTIONS: '--enable-async-hooks' }); From 301ad5b3416ca28de09bdcdbbcc0088f1a2c02b4 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sat, 31 Oct 2026 12:31:40 +0000 Subject: [PATCH 6/8] test: enable async hooks in zlib async tests These tests call async_hooks.createHook() directly to count round trips or track the internal status, so they now need --enable-async-hooks. Refs: https://github.com/nodejs/node/pull/61030 Signed-off-by: Matteo Collina Assisted-by: pi --- test/parallel/test-zlib-convenience-async-round-trip.js | 1 + test/parallel/test-zlib-sync-without-options.js | 1 + 2 files changed, 2 insertions(+) diff --git a/test/parallel/test-zlib-convenience-async-round-trip.js b/test/parallel/test-zlib-convenience-async-round-trip.js index e7b9fb217b0..02cad394654 100644 --- a/test/parallel/test-zlib-convenience-async-round-trip.js +++ b/test/parallel/test-zlib-convenience-async-round-trip.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks // The asynchronous convenience methods know the whole input up front, so they // process it with the finish flush in a single write. Check that a call whose // output fits in one output buffer takes exactly one threadpool round trip, diff --git a/test/parallel/test-zlib-sync-without-options.js b/test/parallel/test-zlib-sync-without-options.js index d155f926442..23c79f09050 100644 --- a/test/parallel/test-zlib-sync-without-options.js +++ b/test/parallel/test-zlib-sync-without-options.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --enable-async-hooks // Synchronous convenience methods called without options skip the stream and // reuse an internal output buffer. Check that they behave exactly like calls // that pass an empty options object, which go through the stream. From d099eb9a7fd358837828924e0bc2c2ff2e105b69 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sat, 31 Oct 2026 12:31:40 +0000 Subject: [PATCH 7/8] build: pass --enable-async-hooks in test-with-async-hooks Running tests with NODE_TEST_WITH_ASYNC_HOOKS=1 installs an async hook in test/common to verify ids. Now that async_hooks.createHook() requires --enable-async-hooks, pass the flag through the test runner so this target keeps working. Refs: https://github.com/nodejs/node/pull/61030 Signed-off-by: Matteo Collina Assisted-by: pi --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 0af75b42c29..eb7458b70bc 100644 --- a/Makefile +++ b/Makefile @@ -807,6 +807,7 @@ test-with-async-hooks: ## Run tests with async hooks. $(MAKE) build-node-api-tests $(MAKE) cctest NODE_TEST_WITH_ASYNC_HOOKS=1 $(PYTHON) tools/test.py $(PARALLEL_ARGS) --mode=$(BUILDTYPE_LOWER) \ + --node-args=--enable-async-hooks \ $(JS_SUITES) \ $(NATIVE_SUITES) From 945bd9219bd5ae111e2fc30fb3de40fc9af629ea Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 2 Nov 2026 08:24:47 +0000 Subject: [PATCH 8/8] benchmark: pass --enable-async-hooks to async_hooks benchmarks The benchmarks under benchmark/async_hooks/ call async_hooks.createHook(), which is disabled by default and requires --enable-async-hooks. Pass the flag explicitly to the benchmark process from benchmark/run.js instead of relying on NODE_OPTIONS, which is not reliably honored by the spawned benchmark processes. Refs: https://github.com/nodejs/node/pull/61030 Signed-off-by: Matteo Collina Assisted-by: pi --- benchmark/run.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/benchmark/run.js b/benchmark/run.js index 455ddc4f2df..46e8b3f8481 100644 --- a/benchmark/run.js +++ b/benchmark/run.js @@ -54,11 +54,24 @@ function runBenchmark(filename) { const args = cli.test ? ['--test'] : cli.optional.set; const cpuCore = cli.getCpuCoreSetting(); + + // Benchmarks under async_hooks/ call createHook(), which is disabled by + // default and requires --enable-async-hooks. + const nodeArgs = filename.startsWith('async_hooks/') ? + ['--enable-async-hooks'] : + []; + let child; if (cpuCore !== null) { - child = spawn('taskset', ['-c', cpuCore, process.execPath, scriptPath, ...args], { + child = spawn('taskset', ['-c', cpuCore, process.execPath, ...nodeArgs, scriptPath, ...args], { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], }); + } else if (nodeArgs.length > 0) { + child = fork( + scriptPath, + args, + { execArgv: nodeArgs }, + ); } else { child = fork( scriptPath,