Skip to content

Commit 4a1c05c

Browse files
committed
fix invalid ptr to shutdown_handler
1 parent 7b28b5e commit 4a1c05c

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

tools/server/server-models.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ server_http_res_ptr server_models::proxy_request(const server_http_req & req, co
570570
return proxy;
571571
}
572572

573-
void server_models::setup_child_server(const common_params & base_params, int router_port, const std::string & name, std::function<void(int)> & shutdown_handler) {
573+
std::thread server_models::setup_child_server(const common_params & base_params, int router_port, const std::string & name, std::function<void(int)> & shutdown_handler) {
574574
// send a notification to the router server that a model instance is ready
575575
// TODO @ngxson : use HTTP client from libcommon
576576
httplib::Client cli(base_params.hostname, router_port);
@@ -598,7 +598,7 @@ void server_models::setup_child_server(const common_params & base_params, int ro
598598
}
599599

600600
// setup thread for monitoring stdin
601-
std::thread([shutdown_handler]() {
601+
return std::thread([shutdown_handler]() {
602602
// wait for EOF on stdin
603603
SRV_INF("%s", "child server monitoring thread started, waiting for EOF on stdin...\n");
604604
bool eof = false;
@@ -619,7 +619,7 @@ void server_models::setup_child_server(const common_params & base_params, int ro
619619
SRV_INF("%s", "EOF on stdin detected, forcing shutdown...\n");
620620
exit(1);
621621
}
622-
}).detach();
622+
});
623623
}
624624

625625

tools/server/server-models.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ struct server_models {
122122
server_http_res_ptr proxy_request(const server_http_req & req, const std::string & method, const std::string & name, bool update_last_used);
123123

124124
// notify the router server that a model instance is ready
125-
static void setup_child_server(const common_params & base_params, int router_port, const std::string & name, std::function<void(int)> & shutdown_handler);
125+
// return the monitoring thread (to be joined by the caller)
126+
static std::thread setup_child_server(const common_params & base_params, int router_port, const std::string & name, std::function<void(int)> & shutdown_handler);
126127
};
127128

128129
struct server_models_routes {

tools/server/server.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,9 @@ int main(int argc, char ** argv, char ** envp) {
276276

277277
// optionally, notify router server that this instance is ready
278278
const char * router_port = std::getenv("LLAMA_SERVER_ROUTER_PORT");
279+
std::thread monitor_thread;
279280
if (router_port != nullptr) {
280-
server_models::setup_child_server(params, std::atoi(router_port), params.model_alias, shutdown_handler);
281+
monitor_thread = server_models::setup_child_server(params, std::atoi(router_port), params.model_alias, shutdown_handler);
281282
}
282283

283284
// this call blocks the main thread until queue_tasks.terminate() is called
@@ -287,6 +288,9 @@ int main(int argc, char ** argv, char ** envp) {
287288
if (ctx_http.thread.joinable()) {
288289
ctx_http.thread.join();
289290
}
291+
if (monitor_thread.joinable()) {
292+
monitor_thread.join();
293+
}
290294
llama_memory_breakdown_print(ctx_server.get_llama_context());
291295
}
292296

0 commit comments

Comments
 (0)