Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: make AtExit() callbacks run in reverse order #30230

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/api/addons.md
Original file line number Diff line number Diff line change
Expand Up @@ -1354,10 +1354,10 @@ static void sanity_check(void*) {
}

void init(Local<Object> exports) {
AtExit(sanity_check);
AtExit(at_exit_cb2, cookie);
AtExit(at_exit_cb2, cookie);
AtExit(at_exit_cb1, exports->GetIsolate());
AtExit(sanity_check);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, init)
Expand Down
2 changes: 1 addition & 1 deletion src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ void Environment::RunAtExitCallbacks() {
}

void Environment::AtExit(void (*cb)(void* arg), void* arg) {
at_exit_functions_.push_back(ExitCallback{cb, arg});
at_exit_functions_.push_front(ExitCallback{cb, arg});
}

void Environment::RunAndClearNativeImmediates() {
Expand Down
29 changes: 29 additions & 0 deletions test/cctest/test_environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ using node::RunAtExit;

static bool called_cb_1 = false;
static bool called_cb_2 = false;
static bool called_cb_ordered_1 = false;
static bool called_cb_ordered_2 = false;
static void at_exit_callback1(void* arg);
static void at_exit_callback2(void* arg);
static void at_exit_callback_ordered1(void* arg);
static void at_exit_callback_ordered2(void* arg);
static std::string cb_1_arg; // NOLINT(runtime/string)

class EnvironmentTest : public EnvironmentTestFixture {
Expand All @@ -20,6 +24,8 @@ class EnvironmentTest : public EnvironmentTestFixture {
NodeTestFixture::TearDown();
called_cb_1 = false;
called_cb_2 = false;
called_cb_ordered_1 = false;
called_cb_ordered_2 = false;
}
};

Expand Down Expand Up @@ -61,6 +67,19 @@ TEST_F(EnvironmentTest, AtExitWithoutEnvironment) {
EXPECT_TRUE(called_cb_1);
}

TEST_F(EnvironmentTest, AtExitOrder) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env env {handle_scope, argv};

// Test that callbacks are run in reverse order.
AtExit(*env, at_exit_callback_ordered1);
AtExit(*env, at_exit_callback_ordered2);
RunAtExit(*env);
EXPECT_TRUE(called_cb_ordered_1);
EXPECT_TRUE(called_cb_ordered_2);
}

TEST_F(EnvironmentTest, AtExitWithArgument) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Expand Down Expand Up @@ -134,3 +153,13 @@ static void at_exit_callback1(void* arg) {
static void at_exit_callback2(void* arg) {
called_cb_2 = true;
}

static void at_exit_callback_ordered1(void* arg) {
EXPECT_TRUE(called_cb_ordered_2);
called_cb_ordered_1 = true;
}

static void at_exit_callback_ordered2(void* arg) {
EXPECT_FALSE(called_cb_ordered_1);
called_cb_ordered_2 = true;
}