Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6c249c0

Browse filesBrowse files
committed
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: #30643 PR-URL: #30666 Refs: #30374 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent bea2501 commit 6c249c0
Copy full SHA for 6c249c0

File tree

Expand file treeCollapse file tree

3 files changed

+30
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+30
-3
lines changed
Open diff view settings
Collapse file

‎src/env.cc‎

Copy file name to clipboardExpand all lines: src/env.cc
+6-2Lines changed: 6 additions & 2 deletions
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);
Collapse file

‎src/env.h‎

Copy file name to clipboardExpand all lines: src/env.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ class Environment : public MemoryRetainer {
14171417
std::unique_ptr<NativeImmediateCallback> native_immediate_callbacks_head_;
14181418
NativeImmediateCallback* native_immediate_callbacks_tail_ = nullptr;
14191419

1420-
void RunAndClearNativeImmediates();
1420+
void RunAndClearNativeImmediates(bool only_refed = false);
14211421
static void CheckImmediate(uv_check_t* handle);
14221422

14231423
// Use an unordered_set, so that we have efficient insertion and removal.
Collapse file

‎test/cctest/test_environment.cc‎

Copy file name to clipboardExpand all lines: test/cctest/test_environment.cc
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,26 @@ static void at_exit_js(void* arg) {
185185
assert(obj->IsObject());
186186
called_at_exit_js = true;
187187
}
188+
189+
TEST_F(EnvironmentTest, SetImmediateCleanup) {
190+
int called = 0;
191+
int called_unref = 0;
192+
193+
{
194+
const v8::HandleScope handle_scope(isolate_);
195+
const Argv argv;
196+
Env env {handle_scope, argv};
197+
198+
(*env)->SetImmediate([&](node::Environment* env_arg) {
199+
EXPECT_EQ(env_arg, *env);
200+
called++;
201+
});
202+
(*env)->SetUnrefImmediate([&](node::Environment* env_arg) {
203+
EXPECT_EQ(env_arg, *env);
204+
called_unref++;
205+
});
206+
}
207+
208+
EXPECT_EQ(called, 1);
209+
EXPECT_EQ(called_unref, 0);
210+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.