Skip to content

Commit 6d81860

Browse files
addaleaxcjihrig
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 <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6ce0dd0 commit 6d81860

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
@@ -698,6 +698,9 @@ void Environment::RunAndClearInterrupts() {
698698
void Environment::RunAndClearNativeImmediates(bool only_refed) {
699699
TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
700700
"RunAndClearNativeImmediates", this);
701+
HandleScope handle_scope(isolate_);
702+
InternalCallbackScope cb_scope(this, Object::New(isolate_), { 0, 0 });
703+
701704
size_t ref_count = 0;
702705

703706
// 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)