Skip to content

Commit b878e32

Browse files
addaleaxMylesBorins
authored andcommitted
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 6f9f546. Fixes: #33002 Refs: #32523 PR-URL: #34366 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 95afc2e commit b878e32

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

src/env.cc

+3
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,9 @@ void Environment::RunAndClearInterrupts() {
657657
void Environment::RunAndClearNativeImmediates(bool only_refed) {
658658
TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
659659
"RunAndClearNativeImmediates", this);
660+
HandleScope handle_scope(isolate_);
661+
InternalCallbackScope cb_scope(this, Object::New(isolate_), { 0, 0 });
662+
660663
size_t ref_count = 0;
661664

662665
// Handle interrupts first. These functions are not allowed to throw

test/async-hooks/test-late-hook-enable.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ const fnsToTest = [setTimeout, (cb) => {
1717
});
1818
});
1919
}, (cb) => {
20-
process.nextTick(() => {
21-
cb();
20+
setImmediate(() => {
21+
process.nextTick(() => {
22+
cb();
2223

23-
// We need to keep the event loop open for this to actually work
24-
// since destroy hooks are triggered in unrefed Immediates
25-
setImmediate(() => {
26-
hook.disable();
27-
assert.strictEqual(fnsToTest.length, 0);
24+
// We need to keep the event loop open for this to actually work
25+
// since destroy hooks are triggered in unrefed Immediates
26+
setImmediate(() => {
27+
hook.disable();
28+
assert.strictEqual(fnsToTest.length, 0);
29+
});
2830
});
2931
});
3032
}];

test/cctest/test_environment.cc

+26
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,29 @@ TEST_F(EnvironmentTest, ExitHandlerTest) {
533533
node::LoadEnvironment(*env, "process.exit(42)").ToLocalChecked();
534534
EXPECT_EQ(callback_calls, 1);
535535
}
536+
537+
TEST_F(EnvironmentTest, SetImmediateMicrotasks) {
538+
int called = 0;
539+
540+
{
541+
const v8::HandleScope handle_scope(isolate_);
542+
const Argv argv;
543+
Env env {handle_scope, argv};
544+
545+
node::LoadEnvironment(*env,
546+
[&](const node::StartExecutionCallbackInfo& info)
547+
-> v8::MaybeLocal<v8::Value> {
548+
return v8::Object::New(isolate_);
549+
});
550+
551+
(*env)->SetImmediate([&](node::Environment* env_arg) {
552+
EXPECT_EQ(env_arg, *env);
553+
isolate_->EnqueueMicrotask([](void* arg) {
554+
++*static_cast<int*>(arg);
555+
}, &called);
556+
}, node::CallbackFlags::kRefed);
557+
uv_run(&current_loop, UV_RUN_DEFAULT);
558+
}
559+
560+
EXPECT_EQ(called, 1);
561+
}

0 commit comments

Comments
 (0)