Skip to content

Commit e9ac80b

Browse files
committed
async_hooks: clean up usage in internal code
Instead of exposing internals of async_hooks & async_wrap throughout the code base, create necessary helper methods within the internal async_hooks that allows easy usage by Node.js internals. This stops every single internal user of async_hooks from importing a ton of functions, constants and internal Aliased Buffers from C++ async_wrap. Adds functions initHooksExist, afterHooksExist, and destroyHooksExist to determine whether the related emit methods need to be triggered. Adds clearDefaultTriggerAsyncId and clearAsyncIdStack on the JS side as an alternative to always calling C++. Moves async_id_symbol and trigger_async_id_symbol to internal async_hooks as they are never used in C++. Renames newUid to newAsyncId for added clarity of its purpose. Adjusts usage throughout the codebase, as well as in a couple of tests. PR-URL: #18720 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 7748865 commit e9ac80b

20 files changed

+141
-122
lines changed

lib/_http_agent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const net = require('net');
2525
const util = require('util');
2626
const EventEmitter = require('events');
2727
const debug = util.debuglog('http');
28-
const { async_id_symbol } = process.binding('async_wrap');
28+
const { async_id_symbol } = require('internal/async_hooks').symbols;
2929
const { nextTick } = require('internal/process/next_tick');
3030

3131
// New Agent code.

lib/_http_outgoing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const common = require('_http_common');
3131
const checkIsHttpToken = common._checkIsHttpToken;
3232
const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
3333
const { outHeadersKey } = require('internal/http');
34-
const { async_id_symbol } = process.binding('async_wrap');
34+
const { async_id_symbol } = require('internal/async_hooks').symbols;
3535
const { nextTick } = require('internal/process/next_tick');
3636
const errors = require('internal/errors');
3737

lib/async_hooks.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,31 @@ const internal_async_hooks = require('internal/async_hooks');
99
// resource gets gced.
1010
const { registerDestroyHook } = async_wrap;
1111
const {
12+
executionAsyncId,
13+
triggerAsyncId,
1214
// Private API
1315
getHookArrays,
1416
enableHooks,
1517
disableHooks,
1618
// Internal Embedder API
17-
newUid,
19+
newAsyncId,
1820
getDefaultTriggerAsyncId,
1921
emitInit,
2022
emitBefore,
2123
emitAfter,
2224
emitDestroy,
2325
} = internal_async_hooks;
2426

25-
// Get fields
26-
const { async_id_fields } = async_wrap;
27-
2827
// Get symbols
2928
const {
29+
async_id_symbol, trigger_async_id_symbol,
3030
init_symbol, before_symbol, after_symbol, destroy_symbol,
3131
promise_resolve_symbol
3232
} = internal_async_hooks.symbols;
3333

34-
const { async_id_symbol, trigger_async_id_symbol } = async_wrap;
35-
3634
// Get constants
3735
const {
3836
kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
39-
kExecutionAsyncId, kTriggerAsyncId
4037
} = async_wrap.constants;
4138

4239
// Listener API //
@@ -125,16 +122,6 @@ function createHook(fns) {
125122
}
126123

127124

128-
function executionAsyncId() {
129-
return async_id_fields[kExecutionAsyncId];
130-
}
131-
132-
133-
function triggerAsyncId() {
134-
return async_id_fields[kTriggerAsyncId];
135-
}
136-
137-
138125
// Embedder API //
139126

140127
const destroyedSymbol = Symbol('destroyed');
@@ -170,7 +157,7 @@ class AsyncResource {
170157
triggerAsyncId);
171158
}
172159

173-
this[async_id_symbol] = newUid();
160+
this[async_id_symbol] = newAsyncId();
174161
this[trigger_async_id_symbol] = triggerAsyncId;
175162
// this prop name (destroyed) has to be synchronized with C++
176163
this[destroyedSymbol] = { destroyed: false };

lib/dgram.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ const dns = require('dns');
2828
const util = require('util');
2929
const { isUint8Array } = require('internal/util/types');
3030
const EventEmitter = require('events');
31-
const { defaultTriggerAsyncIdScope } = require('internal/async_hooks');
31+
const {
32+
defaultTriggerAsyncIdScope,
33+
symbols: { async_id_symbol }
34+
} = require('internal/async_hooks');
3235
const { UV_UDP_REUSEADDR } = process.binding('constants').os;
33-
const { async_id_symbol } = process.binding('async_wrap');
3436
const { nextTick } = require('internal/process/next_tick');
3537

3638
const { UDP, SendWrap } = process.binding('udp_wrap');

lib/internal/async_hooks.js

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const async_wrap = process.binding('async_wrap');
2727
* It has a fixed size, so if that is exceeded, calls to the native
2828
* side are used instead in pushAsyncIds() and popAsyncIds().
2929
*/
30-
const { async_id_symbol, async_hook_fields, async_id_fields } = async_wrap;
30+
const { async_hook_fields, async_id_fields } = async_wrap;
3131
// Store the pair executionAsyncId and triggerAsyncId in a std::stack on
3232
// Environment::AsyncHooks::async_ids_stack_ tracks the resource responsible for
3333
// the current execution stack. This is unwound as each resource exits. In the
@@ -71,6 +71,8 @@ const { kInit, kBefore, kAfter, kDestroy, kPromiseResolve,
7171
kDefaultTriggerAsyncId, kStackLength } = async_wrap.constants;
7272

7373
// Used in AsyncHook and AsyncResource.
74+
const async_id_symbol = Symbol('asyncId');
75+
const trigger_async_id_symbol = Symbol('triggerAsyncId');
7476
const init_symbol = Symbol('init');
7577
const before_symbol = Symbol('before');
7678
const after_symbol = Symbol('after');
@@ -245,7 +247,7 @@ function disableHooks() {
245247
// Increment the internal id counter and return the value. Important that the
246248
// counter increment first. Since it's done the same way in
247249
// Environment::new_async_uid()
248-
function newUid() {
250+
function newAsyncId() {
249251
return ++async_id_fields[kAsyncIdCounter];
250252
}
251253

@@ -254,7 +256,7 @@ function getOrSetAsyncId(object) {
254256
return object[async_id_symbol];
255257
}
256258

257-
return object[async_id_symbol] = newUid();
259+
return object[async_id_symbol] = newAsyncId();
258260
}
259261

260262

@@ -270,6 +272,11 @@ function getDefaultTriggerAsyncId() {
270272
}
271273

272274

275+
function clearDefaultTriggerAsyncId() {
276+
async_id_fields[kDefaultTriggerAsyncId] = -1;
277+
}
278+
279+
273280
function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
274281
// CHECK(Number.isSafeInteger(triggerAsyncId))
275282
// CHECK(triggerAsyncId > 0)
@@ -287,6 +294,19 @@ function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
287294
}
288295

289296

297+
function initHooksExist() {
298+
return async_hook_fields[kInit] > 0;
299+
}
300+
301+
function afterHooksExist() {
302+
return async_hook_fields[kAfter] > 0;
303+
}
304+
305+
function destroyHooksExist() {
306+
return async_hook_fields[kDestroy] > 0;
307+
}
308+
309+
290310
function emitInitScript(asyncId, type, triggerAsyncId, resource) {
291311
validateAsyncId(asyncId, 'asyncId');
292312
if (triggerAsyncId !== null)
@@ -345,6 +365,20 @@ function emitDestroyScript(asyncId) {
345365
}
346366

347367

368+
// Keep in sync with Environment::AsyncHooks::clear_async_id_stack
369+
// in src/env-inl.h.
370+
function clearAsyncIdStack() {
371+
async_id_fields[kExecutionAsyncId] = 0;
372+
async_id_fields[kTriggerAsyncId] = 0;
373+
async_hook_fields[kStackLength] = 0;
374+
}
375+
376+
377+
function hasAsyncIdStack() {
378+
return async_hook_fields[kStackLength] > 0;
379+
}
380+
381+
348382
// This is the equivalent of the native push_async_ids() call.
349383
function pushAsyncIds(asyncId, triggerAsyncId) {
350384
const offset = async_hook_fields[kStackLength];
@@ -377,20 +411,38 @@ function popAsyncIds(asyncId) {
377411
}
378412

379413

414+
function executionAsyncId() {
415+
return async_id_fields[kExecutionAsyncId];
416+
}
417+
418+
function triggerAsyncId() {
419+
return async_id_fields[kTriggerAsyncId];
420+
}
421+
422+
380423
module.exports = {
424+
executionAsyncId,
425+
triggerAsyncId,
381426
// Private API
382427
getHookArrays,
383428
symbols: {
429+
async_id_symbol, trigger_async_id_symbol,
384430
init_symbol, before_symbol, after_symbol, destroy_symbol,
385431
promise_resolve_symbol
386432
},
387433
enableHooks,
388434
disableHooks,
435+
clearDefaultTriggerAsyncId,
436+
clearAsyncIdStack,
437+
hasAsyncIdStack,
389438
// Internal Embedder API
390-
newUid,
439+
newAsyncId,
391440
getOrSetAsyncId,
392441
getDefaultTriggerAsyncId,
393442
defaultTriggerAsyncIdScope,
443+
initHooksExist,
444+
afterHooksExist,
445+
destroyHooksExist,
394446
emitInit: emitInitScript,
395447
emitBefore: emitBeforeScript,
396448
emitAfter: emitAfterScript,

lib/internal/bootstrap_node.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -431,18 +431,19 @@
431431
function noop() {}
432432

433433
function setupProcessFatal() {
434-
const async_wrap = process.binding('async_wrap');
435-
// Arrays containing hook flags and ids for async_hook calls.
436-
const { async_hook_fields, async_id_fields } = async_wrap;
437-
// Internal functions needed to manipulate the stack.
438-
const { clearAsyncIdStack } = async_wrap;
439-
const { kAfter, kExecutionAsyncId,
440-
kDefaultTriggerAsyncId, kStackLength } = async_wrap.constants;
434+
const {
435+
executionAsyncId,
436+
clearDefaultTriggerAsyncId,
437+
clearAsyncIdStack,
438+
hasAsyncIdStack,
439+
afterHooksExist,
440+
emitAfter
441+
} = NativeModule.require('internal/async_hooks');
441442

442443
process._fatalException = function(er) {
443-
// It's possible that kDefaultTriggerAsyncId was set for a constructor
444+
// It's possible that defaultTriggerAsyncId was set for a constructor
444445
// call that threw and was never cleared. So clear it now.
445-
async_id_fields[kDefaultTriggerAsyncId] = -1;
446+
clearDefaultTriggerAsyncId();
446447

447448
if (exceptionHandlerState.captureFn !== null) {
448449
exceptionHandlerState.captureFn(er);
@@ -465,11 +466,10 @@
465466
NativeModule.require('timers').setImmediate(noop);
466467

467468
// Emit the after() hooks now that the exception has been handled.
468-
if (async_hook_fields[kAfter] > 0) {
469-
const { emitAfter } = NativeModule.require('internal/async_hooks');
469+
if (afterHooksExist()) {
470470
do {
471-
emitAfter(async_id_fields[kExecutionAsyncId]);
472-
} while (async_hook_fields[kStackLength] > 0);
471+
emitAfter(executionAsyncId());
472+
} while (hasAsyncIdStack());
473473
// Or completely empty the id stack.
474474
} else {
475475
clearAsyncIdStack();

lib/internal/http2/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
require('internal/util').assertCrypto();
66

7-
const { async_id_symbol } = process.binding('async_wrap');
7+
const { async_id_symbol } = require('internal/async_hooks').symbols;
88
const http = require('http');
99
const binding = process.binding('http2');
1010
const assert = require('assert');

lib/internal/process/next_tick.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ exports.setup = setupNextTick;
55
exports.nextTick = null;
66

77
function setupNextTick() {
8-
const async_wrap = process.binding('async_wrap');
9-
const async_hooks = require('internal/async_hooks');
8+
const {
9+
getDefaultTriggerAsyncId,
10+
newAsyncId,
11+
initHooksExist,
12+
destroyHooksExist,
13+
emitInit,
14+
emitBefore,
15+
emitAfter,
16+
emitDestroy,
17+
symbols: { async_id_symbol, trigger_async_id_symbol }
18+
} = require('internal/async_hooks');
1019
const promises = require('internal/process/promises');
1120
const errors = require('internal/errors');
1221
const { emitPromiseRejectionWarnings } = promises;
13-
const getDefaultTriggerAsyncId = async_hooks.getDefaultTriggerAsyncId;
14-
// Two arrays that share state between C++ and JS.
15-
const { async_hook_fields, async_id_fields } = async_wrap;
16-
// Used to change the state of the async id stack.
17-
const { emitInit, emitBefore, emitAfter, emitDestroy } = async_hooks;
18-
// Grab the constants necessary for working with internal arrays.
19-
const { kInit, kDestroy, kAsyncIdCounter } = async_wrap.constants;
20-
const { async_id_symbol, trigger_async_id_symbol } = async_wrap;
2122

2223
// tickInfo is used so that the C++ code in src/node.cc can
2324
// have easy access to our nextTick state, and avoid unnecessary
@@ -104,7 +105,7 @@ function setupNextTick() {
104105
// that nextTick() doesn't allow the event loop to proceed, but if
105106
// any async hooks are enabled during the callback's execution then
106107
// this tock's after hook will be called, but not its destroy hook.
107-
if (async_hook_fields[kDestroy] > 0)
108+
if (destroyHooksExist())
108109
emitDestroy(asyncId);
109110

110111
const callback = tock.callback;
@@ -128,11 +129,11 @@ function setupNextTick() {
128129
this.callback = callback;
129130
this.args = args;
130131

131-
const asyncId = ++async_id_fields[kAsyncIdCounter];
132+
const asyncId = newAsyncId();
132133
this[async_id_symbol] = asyncId;
133134
this[trigger_async_id_symbol] = triggerAsyncId;
134135

135-
if (async_hook_fields[kInit] > 0) {
136+
if (initHooksExist()) {
136137
emitInit(asyncId,
137138
'TickObject',
138139
triggerAsyncId,

lib/internal/timers.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
'use strict';
22

3-
const async_wrap = process.binding('async_wrap');
4-
// Two arrays that share state between C++ and JS.
5-
const { async_hook_fields, async_id_fields } = async_wrap;
63
const {
74
getDefaultTriggerAsyncId,
8-
// The needed emit*() functions.
5+
newAsyncId,
6+
initHooksExist,
97
emitInit
108
} = require('internal/async_hooks');
11-
// Grab the constants necessary for working with internal arrays.
12-
const { kInit, kAsyncIdCounter } = async_wrap.constants;
139
// Symbols for storing async id state.
1410
const async_id_symbol = Symbol('asyncId');
1511
const trigger_async_id_symbol = Symbol('triggerId');
@@ -70,9 +66,9 @@ function Timeout(callback, after, args, isRepeat, isUnrefed) {
7066

7167
this[unrefedSymbol] = isUnrefed;
7268

73-
this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
69+
this[async_id_symbol] = newAsyncId();
7470
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
75-
if (async_hook_fields[kInit] > 0) {
71+
if (initHooksExist()) {
7672
emitInit(this[async_id_symbol],
7773
'Timeout',
7874
this[trigger_async_id_symbol],

lib/net.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');
4747
const { TCPConnectWrap } = process.binding('tcp_wrap');
4848
const { PipeConnectWrap } = process.binding('pipe_wrap');
4949
const { ShutdownWrap, WriteWrap } = process.binding('stream_wrap');
50-
const { async_id_symbol } = process.binding('async_wrap');
51-
const { newUid, defaultTriggerAsyncIdScope } = require('internal/async_hooks');
50+
const {
51+
newAsyncId,
52+
defaultTriggerAsyncIdScope,
53+
symbols: { async_id_symbol }
54+
} = require('internal/async_hooks');
5255
const { nextTick } = require('internal/process/next_tick');
5356
const errors = require('internal/errors');
5457
const dns = require('dns');
@@ -91,7 +94,7 @@ function createHandle(fd, is_server) {
9194

9295
function getNewAsyncId(handle) {
9396
return (!handle || typeof handle.getAsyncId !== 'function') ?
94-
newUid() : handle.getAsyncId();
97+
newAsyncId() : handle.getAsyncId();
9598
}
9699

97100

0 commit comments

Comments
 (0)