Skip to content

Commit

Permalink
Merge pull request #2272 from apache/graceful-quit-on-sighup
Browse files Browse the repository at this point in the history
Add graceful exit GFLAG for SIGHUP
  • Loading branch information
jamesge authored Jun 7, 2023
2 parents b36ce2c + d1a2fcf commit f504c77
Showing 1 changed file with 20 additions and 0 deletions.
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

0 comments on commit f504c77

Please # to comment.