Skip to content

Commit 6d1c123

Browse files
committed
reorders worker init so that cleanup is actually reached
1 parent dd09dec commit 6d1c123

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

examples/server/server.cpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,27 @@ int main(int argc, const char ** argv) {
668668
// load the model
669669
fprintf(stdout, "%s: loading model and initializing main loop\n", __func__);
670670

671+
shutdown_handler = [&](int) {
672+
// this should unblock the primary thread;
673+
terminate(pool);
674+
return;
675+
};
676+
677+
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
678+
struct sigaction sigint_action;
679+
sigint_action.sa_handler = signal_handler;
680+
sigemptyset(&sigint_action.sa_mask);
681+
sigint_action.sa_flags = 0;
682+
sigaction(SIGINT, &sigint_action, NULL);
683+
sigaction(SIGTERM, &sigint_action, NULL);
684+
#elif defined (_WIN32)
685+
auto console_ctrl_handler = +[](DWORD ctrl_type) -> BOOL {
686+
return (ctrl_type == CTRL_C_EVENT) ? (signal_handler(SIGINT), true) : false;
687+
};
688+
SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);
689+
#endif
690+
671691
// It might make sense in the long run to have the primary thread run clean up on the response map and keep the model workers parallel.
672-
// pool = initialize_workers(args, tqueue, rmap);
673692
pool = new worker_pool;
674693
for (int i = *args.get_int_param("--n-parallelism"); i > 0; i--) {
675694
if (i == 1) {
@@ -684,24 +703,9 @@ int main(int argc, const char ** argv) {
684703
pool->push_back(w);
685704
}
686705
}
687-
688-
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
689-
struct sigaction sigint_action;
690-
sigint_action.sa_handler = signal_handler;
691-
sigemptyset (&sigint_action.sa_mask);
692-
sigint_action.sa_flags = 0;
693-
sigaction(SIGINT, &sigint_action, NULL);
694-
sigaction(SIGTERM, &sigint_action, NULL);
695-
#elif defined (_WIN32)
696-
auto console_ctrl_handler = +[](DWORD ctrl_type) -> BOOL {
697-
return (ctrl_type == CTRL_C_EVENT) ? (signal_handler(SIGINT), true) : false;
698-
};
699-
SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);
700-
#endif
701-
702-
clean_up();
706+
fprintf(stdout, "HTTP server listening on hostname: %s and port: %d, is shutting down.\n", args.get_string_param("--host").c_str(), *args.get_int_param("--port"));
707+
svr->stop();
703708
t.join();
704-
terminate(pool);
705709
rmap->cleanup_thread->join();
706710

707711
return 0;

0 commit comments

Comments
 (0)