Skip to content

Commit c9715bb

Browse files
AndreasMadsenMylesBorins
authored andcommitted
async_hooks: skip runtime checks when disabled
PR-URL: #15454 Ref: #14387 Ref: #14722 Ref: #14717 Ref: #15448 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 49a41d9 commit c9715bb

15 files changed

+192
-87
lines changed

doc/api/cli.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,14 @@ added: v2.1.0
215215
Prints a stack trace whenever synchronous I/O is detected after the first turn
216216
of the event loop.
217217

218+
### `--force-async-hooks-checks`
219+
<!-- YAML
220+
added: REPLACEME
221+
-->
222+
223+
Enables runtime checks for `async_hooks`. These can also be enabled dynamically
224+
by enabling one of the `async_hooks` hooks.
225+
218226
### `--trace-events-enabled`
219227
<!-- YAML
220228
added: v7.7.0

doc/node.1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ Write process warnings to the given file instead of printing to stderr.
152152
Print a stack trace whenever synchronous I/O is detected after the first turn
153153
of the event loop.
154154

155+
.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.
159+
155160
.TP
156161
.BR \-\-trace\-events\-enabled
157162
Enables the collection of trace event tracing information.

lib/_http_outgoing.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,15 @@ function _writeRaw(data, encoding, callback) {
262262
this._flushOutput(conn);
263263
} else if (!data.length) {
264264
if (typeof callback === 'function') {
265-
nextTick(this.socket[async_id_symbol], callback);
265+
let socketAsyncId = this.socket[async_id_symbol];
266+
// If the socket was set directly it won't be correctly initialized
267+
// with an async_id_symbol.
268+
// TODO(AndreasMadsen): @trevnorris suggested some more correct
269+
// solutions in:
270+
// https://github.com/nodejs/node/pull/14389/files#r128522202
271+
if (socketAsyncId === undefined) socketAsyncId = null;
272+
273+
nextTick(socketAsyncId, callback);
266274
}
267275
return true;
268276
}

lib/async_hooks.js

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const active_hooks = {
6161
// async execution. These are tracked so if the user didn't include callbacks
6262
// for a given step, that step can bail out early.
6363
const { kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
64-
kExecutionAsyncId, kTriggerAsyncId, kAsyncIdCounter,
64+
kCheck, kExecutionAsyncId, kTriggerAsyncId, kAsyncIdCounter,
6565
kInitTriggerAsyncId } = async_wrap.constants;
6666

6767
// Symbols used to store the respective ids on both AsyncResource instances and
@@ -157,8 +157,10 @@ class AsyncHook {
157157
hook_fields[kPromiseResolve] += +!!this[promise_resolve_symbol];
158158
hooks_array.push(this);
159159

160-
if (prev_kTotals === 0 && hook_fields[kTotals] > 0)
160+
if (prev_kTotals === 0 && hook_fields[kTotals] > 0) {
161161
enablePromiseHook();
162+
hook_fields[kCheck] += 1;
163+
}
162164

163165
return this;
164166
}
@@ -181,8 +183,10 @@ class AsyncHook {
181183
hook_fields[kPromiseResolve] -= +!!this[promise_resolve_symbol];
182184
hooks_array.splice(index, 1);
183185

184-
if (prev_kTotals > 0 && hook_fields[kTotals] === 0)
186+
if (prev_kTotals > 0 && hook_fields[kTotals] === 0) {
185187
disablePromiseHook();
188+
hook_fields[kCheck] -= 1;
189+
}
186190

187191
return this;
188192
}
@@ -244,6 +248,15 @@ function triggerAsyncId() {
244248
return async_id_fields[kTriggerAsyncId];
245249
}
246250

251+
function validateAsyncId(asyncId, type) {
252+
// Skip validation when async_hooks is disabled
253+
if (async_hook_fields[kCheck] <= 0) return;
254+
255+
if (!Number.isSafeInteger(asyncId) || asyncId < -1) {
256+
fatalError(new errors.RangeError('ERR_INVALID_ASYNC_ID', type, asyncId));
257+
}
258+
}
259+
247260

248261
// Embedder API //
249262

@@ -349,10 +362,16 @@ function setInitTriggerId(triggerAsyncId) {
349362

350363

351364
function emitInitScript(asyncId, type, triggerAsyncId, resource) {
365+
validateAsyncId(asyncId, 'asyncId');
366+
if (triggerAsyncId !== null)
367+
validateAsyncId(triggerAsyncId, 'triggerAsyncId');
368+
if (async_hook_fields[kCheck] > 0 &&
369+
(typeof type !== 'string' || type.length <= 0)) {
370+
throw new errors.TypeError('ERR_ASYNC_TYPE', type);
371+
}
372+
352373
// Short circuit all checks for the common case. Which is that no hooks have
353374
// been set. Do this to remove performance impact for embedders (and core).
354-
// Even though it bypasses all the argument checks. The performance savings
355-
// here is critical.
356375
if (async_hook_fields[kInit] === 0)
357376
return;
358377

@@ -366,18 +385,6 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) {
366385
async_id_fields[kInitTriggerAsyncId] = 0;
367386
}
368387

369-
if (!Number.isSafeInteger(asyncId) || asyncId < -1) {
370-
throw new errors.RangeError('ERR_INVALID_ASYNC_ID', 'asyncId', asyncId);
371-
}
372-
if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < -1) {
373-
throw new errors.RangeError('ERR_INVALID_ASYNC_ID',
374-
'triggerAsyncId',
375-
triggerAsyncId);
376-
}
377-
if (typeof type !== 'string' || type.length <= 0) {
378-
throw new errors.TypeError('ERR_ASYNC_TYPE', type);
379-
}
380-
381388
emitInitNative(asyncId, type, triggerAsyncId, resource);
382389
}
383390

@@ -423,15 +430,8 @@ function emitBeforeScript(asyncId, triggerAsyncId) {
423430
// Validate the ids. An id of -1 means it was never set and is visible on the
424431
// call graph. An id < -1 should never happen in any circumstance. Throw
425432
// on user calls because async state should still be recoverable.
426-
if (!Number.isSafeInteger(asyncId) || asyncId < -1) {
427-
fatalError(
428-
new errors.RangeError('ERR_INVALID_ASYNC_ID', 'asyncId', asyncId));
429-
}
430-
if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < -1) {
431-
fatalError(new errors.RangeError('ERR_INVALID_ASYNC_ID',
432-
'triggerAsyncId',
433-
triggerAsyncId));
434-
}
433+
validateAsyncId(asyncId, 'asyncId');
434+
validateAsyncId(triggerAsyncId, 'triggerAsyncId');
435435

436436
pushAsyncIds(asyncId, triggerAsyncId);
437437

@@ -441,10 +441,7 @@ function emitBeforeScript(asyncId, triggerAsyncId) {
441441

442442

443443
function emitAfterScript(asyncId) {
444-
if (!Number.isSafeInteger(asyncId) || asyncId < -1) {
445-
fatalError(
446-
new errors.RangeError('ERR_INVALID_ASYNC_ID', 'asyncId', asyncId));
447-
}
444+
validateAsyncId(asyncId, 'asyncId');
448445

449446
if (async_hook_fields[kAfter] > 0)
450447
emitAfterNative(asyncId);
@@ -454,10 +451,7 @@ function emitAfterScript(asyncId) {
454451

455452

456453
function emitDestroyScript(asyncId) {
457-
if (!Number.isSafeInteger(asyncId) || asyncId < -1) {
458-
fatalError(
459-
new errors.RangeError('ERR_INVALID_ASYNC_ID', 'asyncId', asyncId));
460-
}
454+
validateAsyncId(asyncId, 'asyncId');
461455

462456
// Return early if there are no destroy callbacks, or invalid asyncId.
463457
if (async_hook_fields[kDestroy] === 0 || asyncId <= 0)

lib/internal/process/next_tick.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ function setupNextTick() {
281281
if (process._exiting)
282282
return;
283283

284-
if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId <= 0) {
284+
if (triggerAsyncId === null) {
285285
triggerAsyncId = async_hooks.initTriggerId();
286286
}
287287

src/async-wrap.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ void AsyncWrap::Initialize(Local<Object> target,
526526
SET_HOOKS_CONSTANT(kDestroy);
527527
SET_HOOKS_CONSTANT(kPromiseResolve);
528528
SET_HOOKS_CONSTANT(kTotals);
529+
SET_HOOKS_CONSTANT(kCheck);
529530
SET_HOOKS_CONSTANT(kExecutionAsyncId);
530531
SET_HOOKS_CONSTANT(kTriggerAsyncId);
531532
SET_HOOKS_CONSTANT(kAsyncIdCounter);

src/env-inl.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,19 @@ inline v8::Local<v8::String> Environment::AsyncHooks::provider_string(int idx) {
129129
return providers_[idx].Get(isolate_);
130130
}
131131

132+
inline void Environment::AsyncHooks::force_checks() {
133+
// fields_ does not have the += operator defined
134+
fields_[kCheck] = fields_[kCheck] + 1;
135+
}
136+
132137
inline void Environment::AsyncHooks::push_async_ids(double async_id,
133138
double trigger_async_id) {
134-
CHECK_GE(async_id, -1);
135-
CHECK_GE(trigger_async_id, -1);
139+
// Since async_hooks is experimental, do only perform the check
140+
// when async_hooks is enabled.
141+
if (fields_[kCheck] > 0) {
142+
CHECK_GE(async_id, -1);
143+
CHECK_GE(trigger_async_id, -1);
144+
}
136145

137146
async_ids_stack_.push({ async_id_fields_[kExecutionAsyncId],
138147
async_id_fields_[kTriggerAsyncId] });
@@ -145,9 +154,11 @@ inline bool Environment::AsyncHooks::pop_async_id(double async_id) {
145154
// stack was multiple MakeCallback()'s deep.
146155
if (async_ids_stack_.empty()) return false;
147156

148-
// Ask for the async_id to be restored as a sanity check that the stack
157+
// Ask for the async_id to be restored as a check that the stack
149158
// hasn't been corrupted.
150-
if (async_id_fields_[kExecutionAsyncId] != async_id) {
159+
// Since async_hooks is experimental, do only perform the check
160+
// when async_hooks is enabled.
161+
if (fields_[kCheck] > 0 && async_id_fields_[kExecutionAsyncId] != async_id) {
151162
fprintf(stderr,
152163
"Error: async hook stack has become corrupted ("
153164
"actual: %.f, expected: %.f)\n",
@@ -185,7 +196,9 @@ inline Environment::AsyncHooks::InitScope::InitScope(
185196
Environment* env, double init_trigger_async_id)
186197
: env_(env),
187198
async_id_fields_ref_(env->async_hooks()->async_id_fields()) {
188-
CHECK_GE(init_trigger_async_id, -1);
199+
if (env_->async_hooks()->fields()[AsyncHooks::kCheck] > 0) {
200+
CHECK_GE(init_trigger_async_id, -1);
201+
}
189202
env->async_hooks()->push_async_ids(
190203
async_id_fields_ref_[AsyncHooks::kExecutionAsyncId],
191204
init_trigger_async_id);

src/env.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ class Environment {
387387
kDestroy,
388388
kPromiseResolve,
389389
kTotals,
390+
kCheck,
390391
kFieldsCount,
391392
};
392393

@@ -408,6 +409,8 @@ class Environment {
408409

409410
inline v8::Local<v8::String> provider_string(int idx);
410411

412+
inline void force_checks();
413+
411414
inline void push_async_ids(double async_id, double trigger_async_id);
412415
inline bool pop_async_id(double async_id);
413416
inline size_t stack_size();

src/node.cc

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ static bool syntax_check_only = false;
169169
static bool trace_deprecation = false;
170170
static bool throw_deprecation = false;
171171
static bool trace_sync_io = false;
172+
static bool force_async_hooks_checks = false;
172173
static bool track_heap_objects = false;
173174
static const char* eval_string = nullptr;
174175
static std::vector<std::string> preload_modules;
@@ -1448,8 +1449,10 @@ void InternalCallbackScope::Close() {
14481449

14491450
// Make sure the stack unwound properly. If there are nested MakeCallback's
14501451
// then it should return early and not reach this code.
1451-
CHECK_EQ(env_->execution_async_id(), 0);
1452-
CHECK_EQ(env_->trigger_async_id(), 0);
1452+
if (env_->async_hooks()->fields()[AsyncHooks::kTotals]) {
1453+
CHECK_EQ(env_->execution_async_id(), 0);
1454+
CHECK_EQ(env_->trigger_async_id(), 0);
1455+
}
14531456

14541457
Local<Object> process = env_->process_object();
14551458

@@ -1458,8 +1461,10 @@ void InternalCallbackScope::Close() {
14581461
return;
14591462
}
14601463

1461-
CHECK_EQ(env_->execution_async_id(), 0);
1462-
CHECK_EQ(env_->trigger_async_id(), 0);
1464+
if (env_->async_hooks()->fields()[AsyncHooks::kTotals]) {
1465+
CHECK_EQ(env_->execution_async_id(), 0);
1466+
CHECK_EQ(env_->trigger_async_id(), 0);
1467+
}
14631468

14641469
if (env_->tick_callback_function()->Call(process, 0, nullptr).IsEmpty()) {
14651470
failed_ = true;
@@ -3853,6 +3858,8 @@ static void PrintHelp() {
38533858
" stderr\n"
38543859
" --trace-sync-io show stack trace when use of sync IO\n"
38553860
" is detected after the first tick\n"
3861+
" --force-async-hooks-checks\n"
3862+
" enables checks for async_hooks\n"
38563863
" --trace-events-enabled track trace events\n"
38573864
" --trace-event-categories comma separated list of trace event\n"
38583865
" categories to record\n"
@@ -3979,6 +3986,7 @@ static void CheckIfAllowedInEnv(const char* exe, bool is_env,
39793986
"--trace-warnings",
39803987
"--redirect-warnings",
39813988
"--trace-sync-io",
3989+
"--force-async-hooks-checks",
39823990
"--trace-events-enabled",
39833991
"--trace-events-categories",
39843992
"--track-heap-objects",
@@ -4117,6 +4125,8 @@ static void ParseArgs(int* argc,
41174125
trace_deprecation = true;
41184126
} else if (strcmp(arg, "--trace-sync-io") == 0) {
41194127
trace_sync_io = true;
4128+
} else if (strcmp(arg, "--force-async-hooks-checks") == 0) {
4129+
force_async_hooks_checks = true;
41204130
} else if (strcmp(arg, "--trace-events-enabled") == 0) {
41214131
trace_enabled = true;
41224132
} else if (strcmp(arg, "--trace-event-categories") == 0) {
@@ -4754,6 +4764,10 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
47544764

47554765
env.set_abort_on_uncaught_exception(abort_on_uncaught_exception);
47564766

4767+
if (force_async_hooks_checks) {
4768+
env.async_hooks()->force_checks();
4769+
}
4770+
47574771
{
47584772
Environment::AsyncCallbackScope callback_scope(&env);
47594773
env.async_hooks()->push_async_ids(1, 0);

test/async-hooks/test-emit-init.js

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
const common = require('../common');
44
const assert = require('assert');
5+
const spawnSync = require('child_process').spawnSync;
56
const async_hooks = require('async_hooks');
67
const initHooks = require('./init-hooks');
78

8-
// Verify that if there is no registered hook, then those invalid parameters
9-
// won't be checked.
10-
assert.doesNotThrow(() => async_hooks.emitInit());
11-
129
const expectedId = async_hooks.newUid();
1310
const expectedTriggerId = async_hooks.newUid();
1411
const expectedType = 'test_emit_init_type';
@@ -25,24 +22,40 @@ const hooks1 = initHooks({
2522

2623
hooks1.enable();
2724

28-
assert.throws(() => {
29-
async_hooks.emitInit();
30-
}, common.expectsError({
31-
code: 'ERR_INVALID_ASYNC_ID',
32-
type: RangeError,
33-
}));
34-
assert.throws(() => {
35-
async_hooks.emitInit(expectedId);
36-
}, common.expectsError({
37-
code: 'ERR_INVALID_ASYNC_ID',
38-
type: RangeError,
39-
}));
40-
assert.throws(() => {
41-
async_hooks.emitInit(expectedId, expectedType, -2);
42-
}, common.expectsError({
43-
code: 'ERR_INVALID_ASYNC_ID',
44-
type: RangeError,
45-
}));
25+
switch (process.argv[2]) {
26+
case 'test_invalid_async_id':
27+
async_hooks.emitInit();
28+
return;
29+
case 'test_invalid_trigger_id':
30+
async_hooks.emitInit(expectedId);
31+
return;
32+
case 'test_invalid_trigger_id_negative':
33+
async_hooks.emitInit(expectedId, expectedType, -2);
34+
return;
35+
}
36+
assert.ok(!process.argv[2]);
37+
38+
39+
const c1 = spawnSync(process.execPath, [__filename, 'test_invalid_async_id']);
40+
assert.strictEqual(
41+
c1.stderr.toString().split(/[\r\n]+/g)[0],
42+
'RangeError [ERR_INVALID_ASYNC_ID]: Invalid asyncId value: undefined');
43+
assert.strictEqual(c1.status, 1);
44+
45+
const c2 = spawnSync(process.execPath, [__filename, 'test_invalid_trigger_id']);
46+
assert.strictEqual(
47+
c2.stderr.toString().split(/[\r\n]+/g)[0],
48+
'RangeError [ERR_INVALID_ASYNC_ID]: Invalid triggerAsyncId value: undefined');
49+
assert.strictEqual(c2.status, 1);
50+
51+
const c3 = spawnSync(process.execPath, [
52+
__filename, 'test_invalid_trigger_id_negative'
53+
]);
54+
assert.strictEqual(
55+
c3.stderr.toString().split(/[\r\n]+/g)[0],
56+
'RangeError [ERR_INVALID_ASYNC_ID]: Invalid triggerAsyncId value: -2');
57+
assert.strictEqual(c3.status, 1);
58+
4659

4760
async_hooks.emitInit(expectedId, expectedType, expectedTriggerId,
4861
expectedResource);

0 commit comments

Comments
 (0)