From df3e52b14ef9dd746edc474ddbc2275e7e1fbb45 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Fri, 29 Mar 2024 15:08:29 +0100 Subject: [PATCH] dnsdist: Release failed TCP backend connections more quickly After a timeout we cannot reuse the TCP connection to the backend anyway, so let's release it immediately. (cherry picked from commit 7b5f590ee72fecf54c0c40b24e98ba03a406af53) --- pdns/dnsdistdist/dnsdist-tcp-downstream.cc | 7 +++++++ pdns/dnsdistdist/test-dnsdisttcp_cc.cc | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pdns/dnsdistdist/dnsdist-tcp-downstream.cc b/pdns/dnsdistdist/dnsdist-tcp-downstream.cc index 684c46fcee..e72d9ac180 100644 --- a/pdns/dnsdistdist/dnsdist-tcp-downstream.cc +++ b/pdns/dnsdistdist/dnsdist-tcp-downstream.cc @@ -146,6 +146,13 @@ void TCPConnectionToBackend::release(){ if (d_ioState) { d_ioState.reset(); } + + auto shared = std::dynamic_pointer_cast(shared_from_this()); + if (!willBeReusable(true)) { + /* remove ourselves from the connection cache, this might mean that our + reference count drops to zero after that, so we need to be careful */ + t_downstreamTCPConnectionsManager.removeDownstreamConnection(shared); + } } static uint32_t getSerialFromRawSOAContent(const std::vector& raw) diff --git a/pdns/dnsdistdist/test-dnsdisttcp_cc.cc b/pdns/dnsdistdist/test-dnsdisttcp_cc.cc index 22a27c692c..3428950f4b 100644 --- a/pdns/dnsdistdist/test-dnsdisttcp_cc.cc +++ b/pdns/dnsdistdist/test-dnsdisttcp_cc.cc @@ -3624,8 +3624,8 @@ BOOST_FIXTURE_TEST_CASE(test_IncomingConnectionOOOR_BackendOOOR, TestFixture) g_tcpRecvTimeout = 2; /* we need to clear them now, otherwise we end up with dangling pointers to the steps via the TLS context, etc */ - /* we have one connection to clear, no proxy protocol */ - BOOST_CHECK_EQUAL(IncomingTCPConnectionState::clearAllDownstreamConnections(), 1U); + /* we have no connection to clear, because there was a timeout! */ + BOOST_CHECK_EQUAL(IncomingTCPConnectionState::clearAllDownstreamConnections(), 0U); } { -- 2.47.2