Skip to content

Commit 338d216

Browse files
addaleaxtargos
authored andcommitted
src: make AtExit() callbacks run in reverse order
This makes the actual behaviour match the documented (and arguably the correct) behaviour. PR-URL: #30230 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]>
1 parent bca0e0f commit 338d216

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

doc/api/addons.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1354,10 +1354,10 @@ static void sanity_check(void*) {
13541354
}
13551355

13561356
void init(Local<Object> exports) {
1357+
AtExit(sanity_check);
13571358
AtExit(at_exit_cb2, cookie);
13581359
AtExit(at_exit_cb2, cookie);
13591360
AtExit(at_exit_cb1, exports->GetIsolate());
1360-
AtExit(sanity_check);
13611361
}
13621362

13631363
NODE_MODULE(NODE_GYP_MODULE_NAME, init)

src/env.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ void Environment::RunAtExitCallbacks() {
636636
}
637637

638638
void Environment::AtExit(void (*cb)(void* arg), void* arg) {
639-
at_exit_functions_.push_back(ExitCallback{cb, arg});
639+
at_exit_functions_.push_front(ExitCallback{cb, arg});
640640
}
641641

642642
void Environment::RunAndClearNativeImmediates() {

test/cctest/test_environment.cc

+29
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ using node::RunAtExit;
1010

1111
static bool called_cb_1 = false;
1212
static bool called_cb_2 = false;
13+
static bool called_cb_ordered_1 = false;
14+
static bool called_cb_ordered_2 = false;
1315
static void at_exit_callback1(void* arg);
1416
static void at_exit_callback2(void* arg);
17+
static void at_exit_callback_ordered1(void* arg);
18+
static void at_exit_callback_ordered2(void* arg);
1519
static std::string cb_1_arg; // NOLINT(runtime/string)
1620

1721
class EnvironmentTest : public EnvironmentTestFixture {
@@ -20,6 +24,8 @@ class EnvironmentTest : public EnvironmentTestFixture {
2024
NodeTestFixture::TearDown();
2125
called_cb_1 = false;
2226
called_cb_2 = false;
27+
called_cb_ordered_1 = false;
28+
called_cb_ordered_2 = false;
2329
}
2430
};
2531

@@ -61,6 +67,19 @@ TEST_F(EnvironmentTest, AtExitWithoutEnvironment) {
6167
EXPECT_TRUE(called_cb_1);
6268
}
6369

70+
TEST_F(EnvironmentTest, AtExitOrder) {
71+
const v8::HandleScope handle_scope(isolate_);
72+
const Argv argv;
73+
Env env {handle_scope, argv};
74+
75+
// Test that callbacks are run in reverse order.
76+
AtExit(*env, at_exit_callback_ordered1);
77+
AtExit(*env, at_exit_callback_ordered2);
78+
RunAtExit(*env);
79+
EXPECT_TRUE(called_cb_ordered_1);
80+
EXPECT_TRUE(called_cb_ordered_2);
81+
}
82+
6483
TEST_F(EnvironmentTest, AtExitWithArgument) {
6584
const v8::HandleScope handle_scope(isolate_);
6685
const Argv argv;
@@ -134,3 +153,13 @@ static void at_exit_callback1(void* arg) {
134153
static void at_exit_callback2(void* arg) {
135154
called_cb_2 = true;
136155
}
156+
157+
static void at_exit_callback_ordered1(void* arg) {
158+
EXPECT_TRUE(called_cb_ordered_2);
159+
called_cb_ordered_1 = true;
160+
}
161+
162+
static void at_exit_callback_ordered2(void* arg) {
163+
EXPECT_FALSE(called_cb_ordered_1);
164+
called_cb_ordered_2 = true;
165+
}

0 commit comments

Comments
 (0)