Skip to content

Commit 2470035

Browse files
addaleaxMylesBorins
authored andcommitted
src: run native immediates during Environment cleanup
This can be necessary, because some parts of the Node.js code base perform cleanup operations in the Immediate callbacks, e.g. HTTP/2. This resolves flakiness in an HTTP/2 test that failed when a `SetImmediate()` callback was not run or destroyed before the `Environment` destructor started, because that callback held a strong reference to the `Http2Session` object and the expectation was that no such objects exist once the `Environment` constructor starts. Another, slightly more direct, alternative would have been to clear the immediate queue rather than to run it. However, this approach seems to make more sense as code generally assumes that the `SetImmediate()` callback will always run; For example, N-API uses an immediate callback to call buffer finalization callbacks. Unref’ed immediates are skipped, as the expectation is generally that they may not run anyway. Fixes: nodejs#30643 PR-URL: nodejs#30666 Refs: nodejs#30374 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c3fff25 commit 2470035

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/env.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,8 @@ void Environment::CleanupHandles() {
543543
Isolate::DisallowJavascriptExecutionScope disallow_js(isolate(),
544544
Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE);
545545

546+
RunAndClearNativeImmediates(true /* skip SetUnrefImmediate()s */);
547+
546548
for (ReqWrapBase* request : req_wrap_queue_)
547549
request->Cancel();
548550

@@ -658,7 +660,7 @@ void Environment::AtExit(void (*cb)(void* arg), void* arg) {
658660
at_exit_functions_.push_front(ExitCallback{cb, arg});
659661
}
660662

661-
void Environment::RunAndClearNativeImmediates() {
663+
void Environment::RunAndClearNativeImmediates(bool only_refed) {
662664
TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
663665
"RunAndClearNativeImmediates", this);
664666
size_t ref_count = 0;
@@ -675,7 +677,9 @@ void Environment::RunAndClearNativeImmediates() {
675677
if (head->is_refed())
676678
ref_count++;
677679

678-
head->Call(this);
680+
if (head->is_refed() || !only_refed)
681+
head->Call(this);
682+
679683
if (UNLIKELY(try_catch.HasCaught())) {
680684
if (!try_catch.HasTerminated() && can_call_into_js())
681685
errors::TriggerUncaughtException(isolate(), try_catch);

src/env.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ class Environment : public MemoryRetainer {
14281428
std::unique_ptr<NativeImmediateCallback> native_immediate_callbacks_head_;
14291429
NativeImmediateCallback* native_immediate_callbacks_tail_ = nullptr;
14301430

1431-
void RunAndClearNativeImmediates();
1431+
void RunAndClearNativeImmediates(bool only_refed = false);
14321432
static void CheckImmediate(uv_check_t* handle);
14331433

14341434
// Use an unordered_set, so that we have efficient insertion and removal.

test/cctest/test_environment.cc

+23
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,26 @@ TEST_F(EnvironmentTest, BufferWithFreeCallbackIsDetached) {
218218
CHECK_EQ(callback_calls, 1);
219219
CHECK_EQ(ab->ByteLength(), 0);
220220
}
221+
222+
TEST_F(EnvironmentTest, SetImmediateCleanup) {
223+
int called = 0;
224+
int called_unref = 0;
225+
226+
{
227+
const v8::HandleScope handle_scope(isolate_);
228+
const Argv argv;
229+
Env env {handle_scope, argv};
230+
231+
(*env)->SetImmediate([&](node::Environment* env_arg) {
232+
EXPECT_EQ(env_arg, *env);
233+
called++;
234+
});
235+
(*env)->SetUnrefImmediate([&](node::Environment* env_arg) {
236+
EXPECT_EQ(env_arg, *env);
237+
called_unref++;
238+
});
239+
}
240+
241+
EXPECT_EQ(called, 1);
242+
EXPECT_EQ(called_unref, 0);
243+
}

0 commit comments

Comments
 (0)