From 6410b65a73caf10c43de0437622cb4ede5569cc9 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 14 Jul 2020 19:06:37 +0200 Subject: [PATCH 1/2] src: add callback scope for native immediates This ensures that microtasks scheduled by native immediates are run after the tasks are done. In particular, this affects the inspector integration since 6f9f546406820dc. Fixes: https://github.com/nodejs/node/issues/33002 Refs: https://github.com/nodejs/node/pull/32523 --- src/env.cc | 3 +++ test/cctest/test_environment.cc | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/env.cc b/src/env.cc index bcaa50bd0129b0..b92ea0ac99986c 100644 --- a/src/env.cc +++ b/src/env.cc @@ -657,6 +657,9 @@ void Environment::RunAndClearInterrupts() { void Environment::RunAndClearNativeImmediates(bool only_refed) { TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment), "RunAndClearNativeImmediates", this); + HandleScope handle_scope(isolate_); + InternalCallbackScope cb_scope(this, Object::New(isolate_), { 0, 0 }); + size_t ref_count = 0; // Handle interrupts first. These functions are not allowed to throw diff --git a/test/cctest/test_environment.cc b/test/cctest/test_environment.cc index 6de332b59b2fe5..27706044b800f6 100644 --- a/test/cctest/test_environment.cc +++ b/test/cctest/test_environment.cc @@ -533,3 +533,29 @@ TEST_F(EnvironmentTest, ExitHandlerTest) { node::LoadEnvironment(*env, "process.exit(42)").ToLocalChecked(); EXPECT_EQ(callback_calls, 1); } + +TEST_F(EnvironmentTest, SetImmediateMicrotasks) { + int called = 0; + + { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env env {handle_scope, argv}; + + node::LoadEnvironment(*env, + [&](const node::StartExecutionCallbackInfo& info) + -> v8::MaybeLocal { + return v8::Object::New(isolate_); + }); + + (*env)->SetImmediate([&](node::Environment* env_arg) { + EXPECT_EQ(env_arg, *env); + isolate_->EnqueueMicrotask([](void* arg) { + ++*static_cast(arg); + }, &called); + }, node::CallbackFlags::kRefed); + uv_run(¤t_loop, UV_RUN_DEFAULT); + } + + EXPECT_EQ(called, 1); +} From 1309b616d73842a162ebbed1fbe43388a966efa0 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 14 Jul 2020 20:27:30 +0200 Subject: [PATCH 2/2] fixup! src: add callback scope for native immediates --- test/async-hooks/test-late-hook-enable.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/test/async-hooks/test-late-hook-enable.js b/test/async-hooks/test-late-hook-enable.js index 0ffa6b24a3077d..82a80b24bb777c 100644 --- a/test/async-hooks/test-late-hook-enable.js +++ b/test/async-hooks/test-late-hook-enable.js @@ -17,14 +17,16 @@ const fnsToTest = [setTimeout, (cb) => { }); }); }, (cb) => { - process.nextTick(() => { - cb(); + setImmediate(() => { + process.nextTick(() => { + cb(); - // We need to keep the event loop open for this to actually work - // since destroy hooks are triggered in unrefed Immediates - setImmediate(() => { - hook.disable(); - assert.strictEqual(fnsToTest.length, 0); + // We need to keep the event loop open for this to actually work + // since destroy hooks are triggered in unrefed Immediates + setImmediate(() => { + hook.disable(); + assert.strictEqual(fnsToTest.length, 0); + }); }); }); }];