Skip to content

Commit 7e2cd72

Browse files
committed
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 Backport-PR-URL: #35241 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 1474405 commit 7e2cd72

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
@@ -708,6 +708,9 @@ void Environment::RunAndClearInterrupts() {
708708
void Environment::RunAndClearNativeImmediates(bool only_refed) {
709709
TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
710710
"RunAndClearNativeImmediates", this);
711+
HandleScope handle_scope(isolate_);
712+
InternalCallbackScope cb_scope(this, Object::New(isolate_), { 0, 0 });
713+
711714
size_t ref_count = 0;
712715

713716
// 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
@@ -466,3 +466,29 @@ TEST_F(EnvironmentTest, ExitHandlerTest) {
466466
node::LoadEnvironment(*env, "process.exit(42)").ToLocalChecked();
467467
EXPECT_EQ(callback_calls, 1);
468468
}
469+
470+
TEST_F(EnvironmentTest, SetImmediateMicrotasks) {
471+
int called = 0;
472+
473+
{
474+
const v8::HandleScope handle_scope(isolate_);
475+
const Argv argv;
476+
Env env {handle_scope, argv};
477+
478+
node::LoadEnvironment(*env,
479+
[&](const node::StartExecutionCallbackInfo& info)
480+
-> v8::MaybeLocal<v8::Value> {
481+
return v8::Object::New(isolate_);
482+
});
483+
484+
(*env)->SetImmediate([&](node::Environment* env_arg) {
485+
EXPECT_EQ(env_arg, *env);
486+
isolate_->EnqueueMicrotask([](void* arg) {
487+
++*static_cast<int*>(arg);
488+
}, &called);
489+
});
490+
uv_run(&current_loop, UV_RUN_DEFAULT);
491+
}
492+
493+
EXPECT_EQ(called, 1);
494+
}

0 commit comments

Comments
 (0)