From 41761cc4a6e8a3390212fa577c425ef5f18a7d72 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 23 Mar 2019 01:33:33 +0800 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/26866 Fixes: https://github.com/nodejs/node/issues/26798 Refs: https://github.com/nodejs/node/pull/26859 Reviewed-By: Rich Trott Reviewed-By: Refael Ackermann --- lib/internal/async_hooks.js | 17 +++++++---------- lib/internal/bootstrap/node.js | 10 ++++++++++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index 165d44b7022289..eeec0e6f82f36c 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -93,16 +93,6 @@ const emitDestroyNative = emitHookFactory(destroy_symbol, 'emitDestroyNative'); const emitPromiseResolveNative = emitHookFactory(promise_resolve_symbol, 'emitPromiseResolveNative'); -// Setup the callbacks that node::AsyncWrap will call when there are hooks to -// process. They use the same functions as the JS embedder API. These callbacks -// are setup immediately to prevent async_wrap.setupHooks() from being hijacked -// and the cost of doing so is negligible. -async_wrap.setupHooks({ init: emitInitNative, - before: emitBeforeNative, - after: emitAfterNative, - destroy: emitDestroyNative, - promise_resolve: emitPromiseResolveNative }); - // Used to fatally abort the process if a callback throws. function fatalError(e) { if (typeof e.stack === 'string') { @@ -462,4 +452,11 @@ module.exports = { emitAfter: emitAfterScript, emitDestroy: emitDestroyScript, registerDestroyHook, + nativeHooks: { + init: emitInitNative, + before: emitBeforeNative, + after: emitAfterNative, + destroy: emitDestroyNative, + promise_resolve: emitPromiseResolveNative + } }; diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index aff746f9716032..bb076ba9694330 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -169,6 +169,16 @@ if (isMainThread) { setupProcessStdio(getStdout, getStdin, getStderr); } +// Setup the callbacks that node::AsyncWrap will call when there are hooks to +// process. They use the same functions as the JS embedder API. These callbacks +// are setup immediately to prevent async_wrap.setupHooks() from being hijacked +// and the cost of doing so is negligible. +const { nativeHooks } = NativeModule.require('internal/async_hooks'); +internalBinding('async_wrap').setupHooks(nativeHooks); + +// XXX(joyeecheung): this has to be done after the initial load of +// `internal/async_hooks` otherwise `async_hooks` cannot require +// `internal/async_hooks`. Investigate why. if (config.hasInspector) { const { enable,