From adbf75f76b6303c44d7e480d73a0f33a7bef7c18 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Fri, 9 Dec 2016 17:08:04 +0100 Subject: [PATCH] dnsdist: Better handling of failures during TCP worker thread creation --- pdns/dnsdist-tcp.cc | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/pdns/dnsdist-tcp.cc b/pdns/dnsdist-tcp.cc index 4b22236801..a805b8906e 100644 --- a/pdns/dnsdist-tcp.cc +++ b/pdns/dnsdist-tcp.cc @@ -81,19 +81,32 @@ void TCPClientCollection::addTCPClientThread() vinfolog("Adding TCP Client thread"); int pipefds[2] = { -1, -1}; - if(pipe(pipefds) < 0) - unixDie("Creating pipe"); + if (pipe(pipefds) < 0) { + errlog("Error creating the TCP thread communication pipe: %s", strerror(errno)); + return; + } if (!setNonBlocking(pipefds[1])) { close(pipefds[0]); close(pipefds[1]); - unixDie("Setting pipe non-blocking"); + errlog("Error setting the TCP thread communication pipe non-blocking: %s", strerror(errno)); + return; + } + + try { + thread t1(tcpClientThread, pipefds[0]); + t1.detach(); + } + catch(const std::runtime_error& e) { + /* the thread creation failed, don't leak */ + errlog("Error creating a TCP thread: %s", e.what()); + close(pipefds[0]); + close(pipefds[1]); + return; } d_tcpclientthreads.push_back(pipefds[1]); ++d_numthreads; - thread t1(tcpClientThread, pipefds[0]); - t1.detach(); } static bool getNonBlockingMsgLen(int fd, uint16_t* len, int timeout) -- 2.47.2