From 252540f7c55247da7a11c27c12f12536567e1b83 Mon Sep 17 00:00:00 2001 From: Xuan Son Nguyen Date: Fri, 20 Mar 2026 20:21:08 +0100 Subject: [PATCH] server: use httplib dynamic threads --- tools/server/server-http.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tools/server/server-http.cpp b/tools/server/server-http.cpp index 129022a711..1e5beffa3a 100644 --- a/tools/server/server-http.cpp +++ b/tools/server/server-http.cpp @@ -227,11 +227,16 @@ bool server_http_context::init(const common_params & params) { int n_threads_http = params.n_threads_http; if (n_threads_http < 1) { - // +2 threads for monitoring endpoints - n_threads_http = std::max(params.n_parallel + 2, (int32_t) std::thread::hardware_concurrency() - 1); + // +4 threads for monitoring, health and some threads reserved for MCP and other tasks in the future + n_threads_http = std::max(params.n_parallel + 4, (int32_t) std::thread::hardware_concurrency() - 1); } LOG_INF("%s: using %d threads for HTTP server\n", __func__, n_threads_http); - srv->new_task_queue = [n_threads_http] { return new httplib::ThreadPool(n_threads_http); }; + srv->new_task_queue = [n_threads_http] { + // spawn n_threads_http fixed thread (always alive), while allow up to 1024 max possible number of threads + // when n_threads_http is used, server will create new "dynamic" threads that will be destroyed after processing each request + // ref: https://github.com/yhirose/cpp-httplib/pull/2368 + return new httplib::ThreadPool(n_threads_http, 1024); + }; // // Web UI setup