Skip to content

Commit

Permalink
src: add callback scope for native immediates
Browse files Browse the repository at this point in the history
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 <benjamingr@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax committed Sep 23, 2020
1 parent 1474405 commit 7e2cd72
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,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
Expand Down
16 changes: 9 additions & 7 deletions test/async-hooks/test-late-hook-enable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
}];
Expand Down
26 changes: 26 additions & 0 deletions test/cctest/test_environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,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<v8::Value> {
return v8::Object::New(isolate_);
});

(*env)->SetImmediate([&](node::Environment* env_arg) {
EXPECT_EQ(env_arg, *env);
isolate_->EnqueueMicrotask([](void* arg) {
++*static_cast<int*>(arg);
}, &called);
});
uv_run(&current_loop, UV_RUN_DEFAULT);
}

EXPECT_EQ(called, 1);
}

0 comments on commit 7e2cd72

Please sign in to comment.