Skip to content

优化无阻塞启动调用http_server_stop多进程模式主程序退出问题 #727

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions base/hmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,19 @@ void signal_handler(int signo) {
switch (signo) {
case SIGINT:
case SIGNAL_TERMINATE:
hlogi("killall processes");
hlogi("master process is about to end");
g_main_ctx.master_wait = true;
case SIGNAL_KILLWORKER:
hlogi("kill all worker processes");
signal(SIGCHLD, SIG_IGN);
// master send SIGKILL => workers
for (int i = 0; i < g_main_ctx.worker_processes; ++i) {
if (g_main_ctx.proc_ctxs[i].pid <= 0) break;
kill(g_main_ctx.proc_ctxs[i].pid, SIGKILL);
g_main_ctx.proc_ctxs[i].pid = -1;
}
exit(0);
g_main_ctx.worker_processes = 0;
if (g_main_ctx.master_wait) exit(0);
break;
case SIGCHLD:
{
Expand Down Expand Up @@ -541,6 +545,7 @@ int signal_init(procedure_t reload_fn, void* reload_userdata) {
signal(SIGCHLD, signal_handler);
signal(SIGNAL_TERMINATE, signal_handler);
signal(SIGNAL_RELOAD, signal_handler);
signal(SIGNAL_KILLWORKER, signal_handler);

return 0;
}
Expand Down Expand Up @@ -729,6 +734,7 @@ int master_workers_run(procedure_t worker_fn, void* worker_userdata,
setproctitle("%s: master process", g_main_ctx.program_name);
signal(SIGNAL_RELOAD, signal_handler);
#endif
g_main_ctx.master_wait = wait;
g_main_ctx.worker_processes = worker_processes;
int bytes = g_main_ctx.worker_processes * sizeof(proc_ctx_t);
SAFE_ALLOC(g_main_ctx.proc_ctxs, bytes);
Expand Down
4 changes: 3 additions & 1 deletion base/hmain.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ typedef struct main_ctx_s {
procedure_t reload_fn;
void* reload_userdata;
// master workers model
bool master_wait;
int worker_processes;
int worker_threads;
procedure_t worker_fn;
Expand Down Expand Up @@ -95,9 +96,10 @@ HV_EXPORT pid_t getpid_from_pidfile();
HV_EXPORT int signal_init(procedure_t reload_fn DEFAULT(NULL), void* reload_userdata DEFAULT(NULL));
HV_EXPORT void signal_handle(const char* signal);
#ifdef OS_UNIX
// we use SIGTERM to quit process, SIGUSR1 to reload confile
// we use SIGTERM to quit all process, SIGUSR1 to reload confile, SIGUSR2 to quit worker process
#define SIGNAL_TERMINATE SIGTERM
#define SIGNAL_RELOAD SIGUSR1
#define SIGNAL_KILLWORKER SIGUSR2
void signal_handler(int signo);
#endif

Expand Down
2 changes: 1 addition & 1 deletion http/server/HttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ int http_server_stop(http_server_t* server) {

#ifdef OS_UNIX
if (server->worker_processes) {
signal_handle("stop");
if (g_main_ctx.pid) kill(g_main_ctx.pid, SIGNAL_KILLWORKER);
return 0;
}
#endif
Expand Down