From: Remi Gacogne Date: Tue, 16 Feb 2021 10:43:40 +0000 (+0100) Subject: dnsdist: Create TCP worker threads before acceptors ones X-Git-Tag: dnsdist-1.6.0-alpha2~37^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F10088%2Fhead;p=thirdparty%2Fpdns.git 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. --- 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);