Skip to content

Commit 1e6d002

Browse files
committed
server : correct signal handler
1 parent d2fe216 commit 1e6d002

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
});
@@ -4425,6 +4429,7 @@ int main(int argc, char ** argv) {
44254429

44264430
// clean up function, to be called before exit
44274431
auto clean_up = [&svr]() {
4432+
SRV_INF("%s: cleaning up before exit...\n", __func__);
44284433
svr->stop();
44294434
llama_backend_free();
44304435
};
@@ -4441,10 +4446,6 @@ int main(int argc, char ** argv) {
44414446
}
44424447

44434448
if (!was_bound) {
4444-
//LOG_ERROR("couldn't bind HTTP server socket", {
4445-
// {"hostname", params.hostname},
4446-
// {"port", params.port},
4447-
//});
44484449
LOG_ERR("%s: couldn't bind HTTP server socket, hostname: %s, port: %d\n", __func__, params.hostname.c_str(), params.port);
44494450
clean_up();
44504451
return 1;
@@ -4461,7 +4462,7 @@ int main(int argc, char ** argv) {
44614462

44624463
if (!ctx_server.load_model(params)) {
44634464
clean_up();
4464-
t.join();
4465+
// t.join(); // FIXME: see below
44654466
LOG_ERR("%s: exiting due to model loading error\n", __func__);
44664467
return 1;
44674468
}
@@ -4485,13 +4486,10 @@ int main(int argc, char ** argv) {
44854486
});
44864487

44874488
shutdown_handler = [&](int) {
4489+
// this will unblock start_loop()
44884490
ctx_server.queue_tasks.terminate();
44894491
};
44904492

4491-
LOG_INF("%s: server is listening on http://%s:%d - starting the main loop\n", __func__, params.hostname.c_str(), params.port);
4492-
4493-
ctx_server.queue_tasks.start_loop();
4494-
44954493
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
44964494
struct sigaction sigint_action;
44974495
sigint_action.sa_handler = signal_handler;
@@ -4506,8 +4504,13 @@ int main(int argc, char ** argv) {
45064504
SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);
45074505
#endif
45084506

4507+
LOG_INF("%s: server is listening on http://%s:%d - starting the main loop\n", __func__, params.hostname.c_str(), params.port);
4508+
4509+
// this call blocks the main thread until queue_tasks.terminate() is called
4510+
ctx_server.queue_tasks.start_loop();
4511+
45094512
clean_up();
4510-
t.join();
4513+
// 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
45114514

45124515
return 0;
45134516
}

0 commit comments

Comments
 (0)