Skip to content

Commit 1a34afd

Browse files
committed
async_hooks: add --allow-async-hooks flag
Add a new `--allow-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 --allow-async-hooks
1 parent 83ba6b1 commit 1a34afd

File tree

166 files changed

+246
-57
lines changed

Some content is hidden

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

166 files changed

+246
-57
lines changed

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

Lines changed: 40 additions & 0 deletions

β€Žlib/async_hooks.jsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616

1717
const {
1818
ERR_ASYNC_CALLBACK,
19+
ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED,
1920
ERR_ASYNC_TYPE,
2021
ERR_INVALID_ASYNC_ID,
2122
} = require('internal/errors').codes;
@@ -26,6 +27,7 @@ const {
2627
validateFunction,
2728
validateString,
2829
} = require('internal/validators');
30+
const { getOptionValue } = require('internal/options');
2931
const internal_async_hooks = require('internal/async_hooks');
3032

3133
const AsyncContextFrame = require('internal/async_context_frame');
@@ -154,6 +156,9 @@ class AsyncHook {
154156

155157

156158
function createHook(fns) {
159+
if (!getOptionValue('--allow-async-hooks')) {
160+
throw new ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED();
161+
}
157162
return new AsyncHook(fns);
158163
}
159164

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,8 @@ E('ERR_AMBIGUOUS_ARGUMENT', 'The "%s" argument is ambiguous. %s', TypeError);
11371137
E('ERR_ARG_NOT_ITERABLE', '%s must be iterable', TypeError);
11381138
E('ERR_ASSERTION', '%s', Error);
11391139
E('ERR_ASYNC_CALLBACK', '%s must be a function', TypeError);
1140+
E('ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED',
1141+
'async_hooks.createHook() is disabled. Use --allow-async-hooks to enable it.', Error);
11401142
E('ERR_ASYNC_LOADER_REQUEST_NEVER_SETTLED',
11411143
'Async loader request never settled', Error);
11421144
E('ERR_ASYNC_TYPE', 'Invalid name for async "type": %s', TypeError);

β€Žsrc/node_options.ccβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
653653
kAllowedInEnvvar,
654654
false,
655655
OptionNamespaces::kPermissionNamespace);
656+
AddOption("--allow-async-hooks",
657+
"allow use of async_hooks.createHook",
658+
&EnvironmentOptions::allow_async_hooks,
659+
kAllowedInEnvvar);
656660
AddOption("--experimental-repl-await",
657661
"experimental await keyword support in REPL",
658662
&EnvironmentOptions::experimental_repl_await,

β€Žsrc/node_options.hβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class EnvironmentOptions : public Options {
146146
bool allow_net = false;
147147
bool allow_wasi = false;
148148
bool allow_worker_threads = false;
149+
bool allow_async_hooks = false;
149150
bool experimental_repl_await = true;
150151
bool experimental_vm_modules = false;
151152
bool async_context_frame = true;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
// Flags: --allow-async-hooks
23

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
// Flags: --allow-async-hooks
23

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
// Flags: --expose-gc
2+
// Flags: --allow-async-hooks --expose-gc
33

44
require('../common');
55
const assert = require('assert');

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
// Flags: --allow-async-hooks
23
const common = require('../common');
34

45
// This test ensures async hooks are being properly called

β€Žtest/async-hooks/test-async-exec-resource-http-32060.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
// Flags: --allow-async-hooks
23
const common = require('../common');
34
const assert = require('assert');
45
const {

0 commit comments

Comments
Β (0)