Skip to content

Commit 0893e01

Browse files
authored
server : correct signal handler (#11795)
1 parent d7b31a9 commit 0893e01

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

examples/server/server.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,10 @@ struct server_queue {
16001600

16011601
while (true) {
16021602
std::unique_lock<std::mutex> lock(mutex_tasks);
1603+
if (!running) {
1604+
QUE_DBG("%s", "terminate\n");
1605+
return;
1606+
}
16031607
if (queue_tasks.empty()) {
16041608
lock.unlock();
16051609
break;
@@ -1620,11 +1624,11 @@ struct server_queue {
16201624
QUE_DBG("%s", "waiting for new tasks\n");
16211625
{
16221626
std::unique_lock<std::mutex> lock(mutex_tasks);
1627+
if (!running) {
1628+
QUE_DBG("%s", "terminate\n");
1629+
return;
1630+
}
16231631
if (queue_tasks.empty()) {
1624-
if (!running) {
1625-
QUE_DBG("%s", "terminate\n");
1626-
return;
1627-
}
16281632
condition_tasks.wait(lock, [&]{
16291633
return (!queue_tasks.empty() || !running);
16301634
});
@@ -4430,6 +4434,7 @@ int main(int argc, char ** argv) {
44304434

44314435
// clean up function, to be called before exit
44324436
auto clean_up = [&svr]() {
4437+
SRV_INF("%s: cleaning up before exit...\n", __func__);
44334438
svr->stop();
44344439
llama_backend_free();
44354440
};
@@ -4446,10 +4451,6 @@ int main(int argc, char ** argv) {
44464451
}
44474452

44484453
if (!was_bound) {
4449-
//LOG_ERROR("couldn't bind HTTP server socket", {
4450-
// {"hostname", params.hostname},
4451-
// {"port", params.port},
4452-
//});
44534454
LOG_ERR("%s: couldn't bind HTTP server socket, hostname: %s, port: %d\n", __func__, params.hostname.c_str(), params.port);
44544455
clean_up();
44554456
return 1;
@@ -4466,7 +4467,7 @@ int main(int argc, char ** argv) {
44664467

44674468
if (!ctx_server.load_model(params)) {
44684469
clean_up();
4469-
t.join();
4470+
// t.join(); // FIXME: see below
44704471
LOG_ERR("%s: exiting due to model loading error\n", __func__);
44714472
return 1;
44724473
}
@@ -4490,13 +4491,10 @@ int main(int argc, char ** argv) {
44904491
});
44914492

44924493
shutdown_handler = [&](int) {
4494+
// this will unblock start_loop()
44934495
ctx_server.queue_tasks.terminate();
44944496
};
44954497

4496-
LOG_INF("%s: server is listening on http://%s:%d - starting the main loop\n", __func__, params.hostname.c_str(), params.port);
4497-
4498-
ctx_server.queue_tasks.start_loop();
4499-
45004498
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
45014499
struct sigaction sigint_action;
45024500
sigint_action.sa_handler = signal_handler;
@@ -4511,8 +4509,13 @@ int main(int argc, char ** argv) {
45114509
SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);
45124510
#endif
45134511

4512+
LOG_INF("%s: server is listening on http://%s:%d - starting the main loop\n", __func__, params.hostname.c_str(), params.port);
4513+
4514+
// this call blocks the main thread until queue_tasks.terminate() is called
4515+
ctx_server.queue_tasks.start_loop();
4516+
45144517
clean_up();
4515-
t.join();
4518+
// t.join(); // FIXME: http thread may stuck if there is an on-going request. we don't need to care about this for now as the HTTP connection will already be closed at this point, but it's better to fix this
45164519

45174520
return 0;
45184521
}

0 commit comments

Comments
 (0)