Skip to content

Commit 4a971f6

Browse files
Stephen Belangertargos
Stephen Belanger
authored andcommitted
async_hooks: eliminate require side effects
PR-URL: #40782 Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Andrey Pechkurov <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent 82b4226 commit 4a971f6

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lib/internal/async_hooks.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ function callbackTrampoline(asyncId, resource, cb, ...args) {
137137
return result;
138138
}
139139

140-
setCallbackTrampoline(callbackTrampoline);
141-
142140
const topLevelResource = {};
143141

144142
function executionAsyncResource() {
@@ -372,6 +370,8 @@ function promiseResolveHook(promise) {
372370
let wantPromiseHook = false;
373371
function enableHooks() {
374372
async_hook_fields[kCheck] += 1;
373+
374+
setCallbackTrampoline(callbackTrampoline);
375375
}
376376

377377
let stopPromiseHook;
@@ -398,6 +398,8 @@ function disableHooks() {
398398

399399
wantPromiseHook = false;
400400

401+
setCallbackTrampoline();
402+
401403
// Delay the call to `disablePromiseHook()` because we might currently be
402404
// between the `before` and `after` calls of a Promise.
403405
enqueueMicrotask(disablePromiseHookIfNecessary);

src/async_wrap.cc

+6-3
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,11 @@ void AsyncWrap::QueueDestroyAsyncId(const FunctionCallbackInfo<Value>& args) {
327327
void AsyncWrap::SetCallbackTrampoline(const FunctionCallbackInfo<Value>& args) {
328328
Environment* env = Environment::GetCurrent(args);
329329

330-
CHECK(args[0]->IsFunction());
331-
332-
env->set_async_hooks_callback_trampoline(args[0].As<Function>());
330+
if (args[0]->IsFunction()) {
331+
env->set_async_hooks_callback_trampoline(args[0].As<Function>());
332+
} else {
333+
env->set_async_hooks_callback_trampoline(Local<Function>());
334+
}
333335
}
334336

335337
Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(Environment* env) {
@@ -439,6 +441,7 @@ void AsyncWrap::Initialize(Local<Object> target,
439441
env->set_async_hooks_after_function(Local<Function>());
440442
env->set_async_hooks_destroy_function(Local<Function>());
441443
env->set_async_hooks_promise_resolve_function(Local<Function>());
444+
env->set_async_hooks_callback_trampoline(Local<Function>());
442445
env->set_async_hooks_binding(target);
443446
}
444447

0 commit comments

Comments
 (0)