Keep the job queued when a dynamic worker thread cannot be created

ThreadPool::enqueue constructed the dynamic thread without handling
std::system_error. When thread creation failed (e.g. EAGAIN under a
pids limit), the exception escaped the accept loop and destroying the
pool's joinable threads called std::terminate. Treat the failure as
"no spare capacity" and leave the job for an existing worker.
This commit is contained in:
yhirose committed 2026-11-10 16:54:53 -05:00
1 parent 6d8dec3c3e
commit bd23a7b179
1 file changed
+10 -1
+10 -1
View File
@@ -12638,7 +12638,16 @@ inline bool ThreadPool::enqueue(std::function<void()> fn) {
if (jobs_.size() > idle_thread_count_ &&
threads_.size() + dynamic_threads_.size() < max_thread_count_) {
cleanup_finished_threads();
dynamic_threads_.emplace_back(std::thread([this]() { worker(true); }));
#ifndef CPPHTTPLIB_NO_EXCEPTIONS
try {
#endif
dynamic_threads_.emplace_back(std::thread([this]() { worker(true); }));
#ifndef CPPHTTPLIB_NO_EXCEPTIONS
} catch (const std::system_error &) {
// No thread budget left: keep the job queued for an existing worker
// instead of letting the exception escape the accept loop.
}
#endif
}
}