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

Add graceful exit GFLAG for SIGHUP #2272

Merged
merged 1 commit into from
Jun 7, 2023
Merged
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
20 changes: 20 additions & 0 deletions src/brpc/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ namespace brpc {

DEFINE_bool(graceful_quit_on_sigterm, false,
"Register SIGTERM handle func to quit graceful");
DEFINE_bool(graceful_quit_on_sighup, false,
"Register SIGHUP handle func to quit graceful");

const IdlNames idl_single_req_single_res = { "req", "res" };
const IdlNames idl_single_req_multi_res = { "req", "" };
Expand Down Expand Up @@ -1461,6 +1463,7 @@ typedef sighandler_t SignalHandler;
static volatile bool s_signal_quit = false;
static SignalHandler s_prev_sigint_handler = NULL;
static SignalHandler s_prev_sigterm_handler = NULL;
static SignalHandler s_prev_sighup_handler = NULL;

static void quit_handler(int signo) {
s_signal_quit = true;
Expand All @@ -1470,6 +1473,9 @@ static void quit_handler(int signo) {
if (SIGTERM == signo && s_prev_sigterm_handler) {
s_prev_sigterm_handler(signo);
}
if (SIGHUP == signo && s_prev_sighup_handler) {
s_prev_sighup_handler(signo);
}
}

static pthread_once_t register_quit_signal_once = PTHREAD_ONCE_INIT;
Expand Down Expand Up @@ -1501,6 +1507,20 @@ static void RegisterQuitSignalOrDie() {
}
}
}

if (FLAGS_graceful_quit_on_sighup) {
prev = signal(SIGHUP, quit_handler);
if (prev != SIG_DFL &&
prev != SIG_IGN) { // shell may install SIGHUP of background jobs with SIG_IGN
if (prev == SIG_ERR) {
LOG(ERROR) << "Fail to register SIGHUP, abort";
abort();
} else {
s_prev_sighup_handler = prev;
LOG(WARNING) << "SIGHUP was installed with " << prev;
}
}
}
}

bool IsAskedToQuit() {
Expand Down