Skip to content

Commit 1edd618

Browse files
authored
Update server.cpp
For correct processing ctrl+c signal
1 parent c026ba3 commit 1edd618

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

examples/server/server.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3388,6 +3388,24 @@ int main(int argc, char ** argv) {
33883388
// struct that contains llama context and inference
33893389
server_context ctx_server;
33903390

3391+
shutdown_handler = [&](int) {
3392+
ctx_server.queue_tasks.terminate();
3393+
};
3394+
3395+
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
3396+
struct sigaction sigint_action;
3397+
sigint_action.sa_handler = signal_handler;
3398+
sigemptyset (&sigint_action.sa_mask);
3399+
sigint_action.sa_flags = 0;
3400+
sigaction(SIGINT, &sigint_action, NULL);
3401+
sigaction(SIGTERM, &sigint_action, NULL);
3402+
#elif defined (_WIN32)
3403+
auto console_ctrl_handler = +[](DWORD ctrl_type) -> BOOL {
3404+
return (ctrl_type == CTRL_C_EVENT) ? (signal_handler(SIGINT), true) : false;
3405+
};
3406+
SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);
3407+
#endif
3408+
33913409
llama_backend_init();
33923410
llama_numa_init(params.numa);
33933411

@@ -4484,27 +4502,10 @@ int main(int argc, char ** argv) {
44844502
ctx_server.update_slots();
44854503
});
44864504

4487-
shutdown_handler = [&](int) {
4488-
ctx_server.queue_tasks.terminate();
4489-
};
4490-
44914505
LOG_INF("%s: server is listening on http://%s:%d - starting the main loop\n", __func__, params.hostname.c_str(), params.port);
44924506

44934507
ctx_server.queue_tasks.start_loop();
44944508

4495-
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
4496-
struct sigaction sigint_action;
4497-
sigint_action.sa_handler = signal_handler;
4498-
sigemptyset (&sigint_action.sa_mask);
4499-
sigint_action.sa_flags = 0;
4500-
sigaction(SIGINT, &sigint_action, NULL);
4501-
sigaction(SIGTERM, &sigint_action, NULL);
4502-
#elif defined (_WIN32)
4503-
auto console_ctrl_handler = +[](DWORD ctrl_type) -> BOOL {
4504-
return (ctrl_type == CTRL_C_EVENT) ? (signal_handler(SIGINT), true) : false;
4505-
};
4506-
SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);
4507-
#endif
45084509

45094510
clean_up();
45104511
t.join();

0 commit comments

Comments
 (0)