Skip to content

Commit 436c0ea

Browse files
committed
optimize no-blocking start call http_server_stop multi-process mode main program exit issues
1 parent c9f07f9 commit 436c0ea

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

base/hmain.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,15 +469,19 @@ void signal_handler(int signo) {
469469
switch (signo) {
470470
case SIGINT:
471471
case SIGNAL_TERMINATE:
472-
hlogi("killall processes");
472+
hlogi("master process is about to end");
473+
g_main_ctx.master_wait = true;
474+
case SIGNAL_KILLWORKER:
475+
hlogi("kill all worker processes");
473476
signal(SIGCHLD, SIG_IGN);
474477
// master send SIGKILL => workers
475478
for (int i = 0; i < g_main_ctx.worker_processes; ++i) {
476479
if (g_main_ctx.proc_ctxs[i].pid <= 0) break;
477480
kill(g_main_ctx.proc_ctxs[i].pid, SIGKILL);
478481
g_main_ctx.proc_ctxs[i].pid = -1;
479482
}
480-
exit(0);
483+
g_main_ctx.worker_processes = 0;
484+
if (g_main_ctx.master_wait) exit(0);
481485
break;
482486
case SIGCHLD:
483487
{
@@ -541,6 +545,7 @@ int signal_init(procedure_t reload_fn, void* reload_userdata) {
541545
signal(SIGCHLD, signal_handler);
542546
signal(SIGNAL_TERMINATE, signal_handler);
543547
signal(SIGNAL_RELOAD, signal_handler);
548+
signal(SIGNAL_KILLWORKER, signal_handler);
544549

545550
return 0;
546551
}
@@ -729,6 +734,7 @@ int master_workers_run(procedure_t worker_fn, void* worker_userdata,
729734
setproctitle("%s: master process", g_main_ctx.program_name);
730735
signal(SIGNAL_RELOAD, signal_handler);
731736
#endif
737+
g_main_ctx.master_wait = wait;
732738
g_main_ctx.worker_processes = worker_processes;
733739
int bytes = g_main_ctx.worker_processes * sizeof(proc_ctx_t);
734740
SAFE_ALLOC(g_main_ctx.proc_ctxs, bytes);

base/hmain.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ typedef struct main_ctx_s {
4545
procedure_t reload_fn;
4646
void* reload_userdata;
4747
// master workers model
48+
bool master_wait;
4849
int worker_processes;
4950
int worker_threads;
5051
procedure_t worker_fn;
@@ -95,9 +96,10 @@ HV_EXPORT pid_t getpid_from_pidfile();
9596
HV_EXPORT int signal_init(procedure_t reload_fn DEFAULT(NULL), void* reload_userdata DEFAULT(NULL));
9697
HV_EXPORT void signal_handle(const char* signal);
9798
#ifdef OS_UNIX
98-
// we use SIGTERM to quit process, SIGUSR1 to reload confile
99+
// we use SIGTERM to quit all process, SIGUSR1 to reload confile, SIGUSR2 to quit worker process
99100
#define SIGNAL_TERMINATE SIGTERM
100101
#define SIGNAL_RELOAD SIGUSR1
102+
#define SIGNAL_KILLWORKER SIGUSR2
101103
void signal_handler(int signo);
102104
#endif
103105

http/server/HttpServer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ int http_server_stop(http_server_t* server) {
244244

245245
#ifdef OS_UNIX
246246
if (server->worker_processes) {
247-
signal_handle("stop");
247+
if (g_main_ctx.pid) kill(g_main_ctx.pid, SIGNAL_KILLWORKER);
248248
return 0;
249249
}
250250
#endif

0 commit comments

Comments
 (0)