From 6187e81a8ecb5ba784e985ff47c1cd3c055dae57 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 26 Dec 2021 20:28:17 +0100 Subject: [PATCH] src: guard slightly costly check in MakeCallback more strongly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/41331 Reviewed-By: Gerhard Stöbich Reviewed-By: Tobias Nießen Reviewed-By: Darshan Sen --- src/api/callback.cc | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/api/callback.cc b/src/api/callback.cc index fb0e5586eb400a..1287eb466fddd3 100644 --- a/src/api/callback.cc +++ b/src/api/callback.cc @@ -64,10 +64,17 @@ InternalCallbackScope::InternalCallbackScope(Environment* env, Isolate* isolate = env->isolate(); HandleScope handle_scope(isolate); - // If you hit this assertion, you forgot to enter the v8::Context first. - CHECK_EQ(Environment::GetCurrent(isolate), env); + Local current_context = isolate->GetCurrentContext(); + // If you hit this assertion, the caller forgot to enter the right Node.js + // Environment's v8::Context first. + // We first check `env->context() != current_context` because the contexts + // likely *are* the same, in which case we can skip the slightly more + // expensive Environment::GetCurrent() call. + if (UNLIKELY(env->context() != current_context)) { + CHECK_EQ(Environment::GetCurrent(isolate), env); + } - env->isolate()->SetIdle(false); + isolate->SetIdle(false); env->async_hooks()->push_async_context( async_context_.async_id, async_context_.trigger_async_id, object);