Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async_hooks: eliminate require side effects #40782

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ function callbackTrampoline(asyncId, resource, cb, ...args) {
return result;
}

setCallbackTrampoline(callbackTrampoline);

const topLevelResource = {};

function executionAsyncResource() {
Expand Down Expand Up @@ -372,6 +370,8 @@ function promiseResolveHook(promise) {
let wantPromiseHook = false;
function enableHooks() {
async_hook_fields[kCheck] += 1;

setCallbackTrampoline(callbackTrampoline);
}

let stopPromiseHook;
Expand All @@ -398,6 +398,8 @@ function disableHooks() {

wantPromiseHook = false;

setCallbackTrampoline();

// Delay the call to `disablePromiseHook()` because we might currently be
// between the `before` and `after` calls of a Promise.
enqueueMicrotask(disablePromiseHookIfNecessary);
Expand Down
9 changes: 6 additions & 3 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,11 @@ void AsyncWrap::QueueDestroyAsyncId(const FunctionCallbackInfo<Value>& args) {
void AsyncWrap::SetCallbackTrampoline(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

CHECK(args[0]->IsFunction());

env->set_async_hooks_callback_trampoline(args[0].As<Function>());
if (args[0]->IsFunction()) {
env->set_async_hooks_callback_trampoline(args[0].As<Function>());
} else {
env->set_async_hooks_callback_trampoline(Local<Function>());
}
}

Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(Environment* env) {
Expand Down Expand Up @@ -439,6 +441,7 @@ void AsyncWrap::Initialize(Local<Object> target,
env->set_async_hooks_after_function(Local<Function>());
env->set_async_hooks_destroy_function(Local<Function>());
env->set_async_hooks_promise_resolve_function(Local<Function>());
env->set_async_hooks_callback_trampoline(Local<Function>());
env->set_async_hooks_binding(target);
}

Expand Down