Skip to content

Commit 0d21299

Browse files
joyeecheungrefack
authored andcommitted
process: load internal/async_hooks before inspector hooks registration
Otherwise the exports of `internal/async_hooks` may be undefined when the inspector async hooks are registered. PR-URL: #26866 Fixes: #26798 Refs: #26859 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent 2b89ac6 commit 0d21299

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

lib/internal/async_hooks.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,6 @@ const emitDestroyNative = emitHookFactory(destroy_symbol, 'emitDestroyNative');
8989
const emitPromiseResolveNative =
9090
emitHookFactory(promise_resolve_symbol, 'emitPromiseResolveNative');
9191

92-
// Setup the callbacks that node::AsyncWrap will call when there are hooks to
93-
// process. They use the same functions as the JS embedder API. These callbacks
94-
// are setup immediately to prevent async_wrap.setupHooks() from being hijacked
95-
// and the cost of doing so is negligible.
96-
async_wrap.setupHooks({ init: emitInitNative,
97-
before: emitBeforeNative,
98-
after: emitAfterNative,
99-
destroy: emitDestroyNative,
100-
promise_resolve: emitPromiseResolveNative });
101-
10292
// Used to fatally abort the process if a callback throws.
10393
function fatalError(e) {
10494
if (typeof e.stack === 'string') {
@@ -461,4 +451,11 @@ module.exports = {
461451
emitAfter: emitAfterScript,
462452
emitDestroy: emitDestroyScript,
463453
registerDestroyHook,
454+
nativeHooks: {
455+
init: emitInitNative,
456+
before: emitBeforeNative,
457+
after: emitAfterNative,
458+
destroy: emitDestroyNative,
459+
promise_resolve: emitPromiseResolveNative
460+
}
464461
};

lib/internal/bootstrap/node.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ if (isMainThread) {
150150
setupProcessStdio(getStdout, getStdin, getStderr);
151151
}
152152

153+
// Setup the callbacks that node::AsyncWrap will call when there are hooks to
154+
// process. They use the same functions as the JS embedder API. These callbacks
155+
// are setup immediately to prevent async_wrap.setupHooks() from being hijacked
156+
// and the cost of doing so is negligible.
157+
const { nativeHooks } = require('internal/async_hooks');
158+
internalBinding('async_wrap').setupHooks(nativeHooks);
159+
160+
// XXX(joyeecheung): this has to be done after the initial load of
161+
// `internal/async_hooks` otherwise `async_hooks` cannot require
162+
// `internal/async_hooks`. Investigate why.
153163
if (config.hasInspector) {
154164
const {
155165
enable,

0 commit comments

Comments
 (0)