diff --git a/lib/async_hooks.js b/lib/async_hooks.js index 9ff244aad7e6b4..354fb1114fb2e6 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -49,12 +49,7 @@ const before_symbol = Symbol('before'); const after_symbol = Symbol('after'); const destroy_symbol = Symbol('destroy'); -// Setup the callbacks that node::AsyncWrap will call when there are hooks to -// process. They use the same functions as the JS embedder API. -async_wrap.setupHooks({ init, - before: emitBeforeN, - after: emitAfterN, - destroy: emitDestroyN }); +let setupHooksCalled = false; // Used to fatally abort the process if a callback throws. function fatalError(e) { @@ -103,6 +98,16 @@ class AsyncHook { if (hooks_array.includes(this)) return; + if (!setupHooksCalled) { + setupHooksCalled = true; + // Setup the callbacks that node::AsyncWrap will call when there are + // hooks to process. They use the same functions as the JS embedder API. + async_wrap.setupHooks({ init, + before: emitBeforeN, + after: emitAfterN, + destroy: emitDestroyN }); + } + // createHook() has already enforced that the callbacks are all functions, // so here simply increment the count of whether each callbacks exists or // not.