Skip to content

Commit 41761cc

Browse files
joyeecheungtargos
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 08383a7 commit 41761cc

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

lib/internal/async_hooks.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,6 @@ const emitDestroyNative = emitHookFactory(destroy_symbol, 'emitDestroyNative');
9393
const emitPromiseResolveNative =
9494
emitHookFactory(promise_resolve_symbol, 'emitPromiseResolveNative');
9595

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

lib/internal/bootstrap/node.js

+10
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ if (isMainThread) {
169169
setupProcessStdio(getStdout, getStdin, getStderr);
170170
}
171171

172+
// Setup the callbacks that node::AsyncWrap will call when there are hooks to
173+
// process. They use the same functions as the JS embedder API. These callbacks
174+
// are setup immediately to prevent async_wrap.setupHooks() from being hijacked
175+
// and the cost of doing so is negligible.
176+
const { nativeHooks } = NativeModule.require('internal/async_hooks');
177+
internalBinding('async_wrap').setupHooks(nativeHooks);
178+
179+
// XXX(joyeecheung): this has to be done after the initial load of
180+
// `internal/async_hooks` otherwise `async_hooks` cannot require
181+
// `internal/async_hooks`. Investigate why.
172182
if (config.hasInspector) {
173183
const {
174184
enable,

0 commit comments

Comments
 (0)