From: Remi Gacogne Date: Mon, 5 Dec 2016 15:42:55 +0000 (+0100) Subject: Handle exceptions raised by `closesocket()` X-Git-Tag: rec-4.0.5-rc1~9^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5a38a566f9bbe3b6ae9e6620d09d673bf7a03210;p=pdns Handle exceptions raised by `closesocket()` This was not very well handled, and could cause the PowerDNS process to terminate. This is especially nasty when `closesocket()` is called from a destructor, as we could already be dealing with an exception. (cherry picked from commit a7b68ae7e414ec9f3184df70ac8008f8a310ae60) --- diff --git a/pdns/dnsproxy.cc b/pdns/dnsproxy.cc index 3f6342386..dfcc09006 100644 --- a/pdns/dnsproxy.cc +++ b/pdns/dnsproxy.cc @@ -304,6 +304,13 @@ void DNSProxy::mainloop(void) } DNSProxy::~DNSProxy() { - if (d_sock>-1) closesocket(d_sock); + if (d_sock>-1) { + try { + closesocket(d_sock); + } + catch(const PDNSException& e) { + } + } + d_sock=-1; } diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 9b2acc8b8..a53ca2f6e 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -388,7 +388,13 @@ public: if(connect(*fd, (struct sockaddr*)(&toaddr), toaddr.getSocklen()) < 0) { int err = errno; // returnSocket(*fd); - closesocket(*fd); + try { + closesocket(*fd); + } + catch(const PDNSException& e) { + L<count(d_remote) && !(*t_tcpClientCounts)[d_remote]--) t_tcpClientCounts->erase(d_remote); --s_currentConnections; @@ -1408,7 +1425,12 @@ void handleNewTCPQuestion(int fd, FDMultiplexer::funcparam_t& ) if(newsock>=0) { if(MT->numProcesses() > g_maxMThreads) { g_stats.overCapacityDrops++; - closesocket(newsock); + try { + closesocket(newsock); + } + catch(const PDNSException& e) { + L<getTid()<<"] dropping TCP query from "<count(addr) && (*t_tcpClientCounts)[addr] >= g_maxTCPPerClient) { g_stats.tcpClientOverflow++; - closesocket(newsock); // don't call TCPConnection::closeAndCleanup here - did not enter it in the counts yet! + try { + closesocket(newsock); // don't call TCPConnection::closeAndCleanup here - did not enter it in the counts yet! + } + catch(const PDNSException& e) { + L<post(); - closesocket(fd); + try { + closesocket(fd); + } + catch(const PDNSException& e) { + L<post(); - closesocket(fd); + + try { + closesocket(fd); + } + catch(const PDNSException& e) { + L< 0 on success