From 4bff8d959e290e90b790e139b94a6011a1c8d482 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Tue, 16 Feb 2021 11:43:40 +0100 Subject: [PATCH] dnsdist: Create TCP worker threads before acceptors ones Otherwise we might crash when the first TCP query arrives because we are dividing by zero when trying to select a worker. --- pdns/dnsdist.cc | 11 +++++++---- pdns/dnsdist.hh | 4 ++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pdns/dnsdist.cc b/pdns/dnsdist.cc index d6c6aa9ac5..bf68654f2c 100644 --- a/pdns/dnsdist.cc +++ b/pdns/dnsdist.cc @@ -2418,6 +2418,13 @@ int main(int argc, char** argv) } handleQueuedHealthChecks(mplexer, true); + /* we need to create the TCP worker threads before the + acceptor ones, otherwise we might crash when processing + the first TCP query */ + while (!g_tcpclientthreads->hasReachedMaxThreads()) { + g_tcpclientthreads->addTCPClientThread(); + } + for(auto& cs : g_frontends) { if (cs->dohFrontend != nullptr) { #ifdef HAVE_DNS_OVER_HTTPS @@ -2445,10 +2452,6 @@ int main(int argc, char** argv) } } - while (!g_tcpclientthreads->hasReachedMaxThreads()) { - g_tcpclientthreads->addTCPClientThread(); - } - thread carbonthread(carbonDumpThread); carbonthread.detach(); diff --git a/pdns/dnsdist.hh b/pdns/dnsdist.hh index d93340a371..a98c7324d3 100644 --- a/pdns/dnsdist.hh +++ b/pdns/dnsdist.hh @@ -857,6 +857,10 @@ public: } int getThread() { + if (d_numthreads == 0) { + throw std::runtime_error("No TCP worker thread yet"); + } + uint64_t pos = d_pos++; ++d_queued; return d_tcpclientthreads.at(pos % d_numthreads); -- 2.47.2