Skip to content

Commit 07d71c9

Browse files
AndreasMadsenjasnell
authored andcommitted
async_hooks: enable runtime checks by default
Ref: #15454 PR-URL: #16318 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent ef238fb commit 07d71c9

File tree

8 files changed

+26
-63
lines changed

8 files changed

+26
-63
lines changed

doc/api/cli.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@ added: v2.1.0
208208
Prints a stack trace whenever synchronous I/O is detected after the first turn
209209
of the event loop.
210210

211-
### `--force-async-hooks-checks`
211+
### `--no-force-async-hooks-checks`
212212
<!-- YAML
213213
added: REPLACEME
214214
-->
215215

216-
Enables runtime checks for `async_hooks`. These can also be enabled dynamically
217-
by enabling one of the `async_hooks` hooks.
216+
Disables runtime checks for `async_hooks`. These will still be enabled
217+
dynamically when `async_hooks` is enabled.
218218

219219
### `--trace-events-enabled`
220220
<!-- YAML

doc/node.1

+3-3
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ Print a stack trace whenever synchronous I/O is detected after the first turn
153153
of the event loop.
154154

155155
.TP
156-
.BR \-\-force\-async\-hooks\-checks
157-
Enables runtime checks for `async_hooks`. These can also be enabled dynamically
158-
by enabling one of the `async_hooks` hooks.
156+
.BR \-\-no\-force\-async\-hooks\-checks
157+
Disables runtime checks for `async_hooks`. These will still be enabled
158+
dynamically when `async_hooks` is enabled.
159159

160160
.TP
161161
.BR \-\-trace\-events\-enabled

src/env-inl.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ inline Environment::AsyncHooks::AsyncHooks(v8::Isolate* isolate)
8888
async_id_fields_(isolate, kUidFieldsCount) {
8989
v8::HandleScope handle_scope(isolate_);
9090

91+
// Always perform async_hooks checks, not just when async_hooks is enabled.
92+
// TODO(AndreasMadsen): Consider removing this for LTS releases.
93+
// See discussion in https://github.com/nodejs/node/pull/15454
94+
// When removing this, do it by reverting the commit. Otherwise the test
95+
// and flag changes won't be included.
96+
fields_[kCheck] = 1;
97+
9198
// kAsyncIdCounter should start at 1 because that'll be the id the execution
9299
// context during bootstrap (code that runs before entering uv_run()).
93100
async_id_fields_[AsyncHooks::kAsyncIdCounter] = 1;
@@ -129,9 +136,9 @@ inline v8::Local<v8::String> Environment::AsyncHooks::provider_string(int idx) {
129136
return providers_[idx].Get(isolate_);
130137
}
131138

132-
inline void Environment::AsyncHooks::force_checks() {
133-
// fields_ does not have the += operator defined
134-
fields_[kCheck] = fields_[kCheck] + 1;
139+
inline void Environment::AsyncHooks::no_force_checks() {
140+
// fields_ does not have the -= operator defined
141+
fields_[kCheck] = fields_[kCheck] - 1;
135142
}
136143

137144
inline void Environment::AsyncHooks::push_async_ids(double async_id,

src/env.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ class Environment {
410410

411411
inline v8::Local<v8::String> provider_string(int idx);
412412

413-
inline void force_checks();
413+
inline void no_force_checks();
414414

415415
inline void push_async_ids(double async_id, double trigger_async_id);
416416
inline bool pop_async_id(double async_id);

src/node.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ static bool syntax_check_only = false;
173173
static bool trace_deprecation = false;
174174
static bool throw_deprecation = false;
175175
static bool trace_sync_io = false;
176-
static bool force_async_hooks_checks = false;
176+
static bool no_force_async_hooks_checks = false;
177177
static bool track_heap_objects = false;
178178
static const char* eval_string = nullptr;
179179
static std::vector<std::string> preload_modules;
@@ -3899,8 +3899,8 @@ static void PrintHelp() {
38993899
" stderr\n"
39003900
" --trace-sync-io show stack trace when use of sync IO\n"
39013901
" is detected after the first tick\n"
3902-
" --force-async-hooks-checks\n"
3903-
" enables checks for async_hooks\n"
3902+
" --no-force-async-hooks-checks\n"
3903+
" disable checks for async_hooks\n"
39043904
" --trace-events-enabled track trace events\n"
39053905
" --trace-event-categories comma separated list of trace event\n"
39063906
" categories to record\n"
@@ -4026,7 +4026,7 @@ static void CheckIfAllowedInEnv(const char* exe, bool is_env,
40264026
"--trace-warnings",
40274027
"--redirect-warnings",
40284028
"--trace-sync-io",
4029-
"--force-async-hooks-checks",
4029+
"--no-force-async-hooks-checks",
40304030
"--trace-events-enabled",
40314031
"--trace-events-categories",
40324032
"--track-heap-objects",
@@ -4165,8 +4165,8 @@ static void ParseArgs(int* argc,
41654165
trace_deprecation = true;
41664166
} else if (strcmp(arg, "--trace-sync-io") == 0) {
41674167
trace_sync_io = true;
4168-
} else if (strcmp(arg, "--force-async-hooks-checks") == 0) {
4169-
force_async_hooks_checks = true;
4168+
} else if (strcmp(arg, "--no-force-async-hooks-checks") == 0) {
4169+
no_force_async_hooks_checks = true;
41704170
} else if (strcmp(arg, "--trace-events-enabled") == 0) {
41714171
trace_enabled = true;
41724172
} else if (strcmp(arg, "--trace-event-categories") == 0) {
@@ -4815,8 +4815,8 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
48154815

48164816
env.set_abort_on_uncaught_exception(abort_on_uncaught_exception);
48174817

4818-
if (force_async_hooks_checks) {
4819-
env.async_hooks()->force_checks();
4818+
if (no_force_async_hooks_checks) {
4819+
env.async_hooks()->no_force_checks();
48204820
}
48214821

48224822
{

test/async-hooks/test-force-checks-flag.js

-25
This file was deleted.

test/async-hooks/test-no-assert-when-disabled.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
'use strict';
2-
// Flags: --expose-internals
2+
// Flags: --no-force-async-hooks-checks --expose-internals
33
const common = require('../common');
44

55
const async_hooks = require('async_hooks');
66
const internal = require('internal/process/next_tick');
77

8-
// In tests async_hooks dynamic checks are enabled by default. To verify
9-
// that no checks are enabled ordinarily disable the checks in this test.
10-
common.revert_force_async_hooks_checks();
11-
12-
// When async_hooks is diabled (or never enabled), the checks
13-
// should be disabled as well. This is important while async_hooks is
14-
// experimental and there are still critial bugs to fix.
15-
168
// Using undefined as the triggerAsyncId.
179
// Ref: https://github.com/nodejs/node/issues/14386
1810
// Ref: https://github.com/nodejs/node/issues/14381

test/common/index.js

-11
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,6 @@ exports.projectDir = path.resolve(__dirname, '..', '..');
6565

6666
exports.buildType = process.config.target_defaults.default_configuration;
6767

68-
// Always enable async_hooks checks in tests
69-
{
70-
const async_wrap = process.binding('async_wrap');
71-
const { kCheck } = async_wrap.constants;
72-
async_wrap.async_hook_fields[kCheck] += 1;
73-
74-
exports.revert_force_async_hooks_checks = function() {
75-
async_wrap.async_hook_fields[kCheck] -= 1;
76-
};
77-
}
78-
7968
// If env var is set then enable async_hook hooks for all tests.
8069
if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
8170
const destroydIdsList = {};

0 commit comments

Comments
 (0)