From 7d1aeff623df6876686478d2f38a7136bfd1f2a5 Mon Sep 17 00:00:00 2001 From: Audric Date: Sun, 29 Mar 2026 19:47:17 +0200 Subject: [PATCH] Fix trunk reconnect storm with per-IP pending connection limit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When two trunk peers simultaneously reconnect, rapid retries from one peer could fill the global pending connection pool (5 slots), starving other peers and creating a reject→reconnect→reject loop. Add a per-IP limit of 2 pending (pre-hello) connections in the trunk server, so one misbehaving or rapidly-reconnecting peer cannot exhaust the pool. Also increase outbound reconnect backoff (2s min, 30s max) to reduce retry pressure after connection failures. --- src/svxlink/reflector/Reflector.cpp | 27 ++++++++++++++++++++++++--- src/svxlink/reflector/TrunkLink.cpp | 2 ++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/svxlink/reflector/Reflector.cpp b/src/svxlink/reflector/Reflector.cpp index f65f6bb..476f441 100644 --- a/src/svxlink/reflector/Reflector.cpp +++ b/src/svxlink/reflector/Reflector.cpp @@ -2045,11 +2045,32 @@ void Reflector::initTrunkServer(void) void Reflector::trunkClientConnected(Async::FramedTcpConnection* con) { - // Reject connections beyond the pending limit to prevent fd exhaustion + // Per-IP pending limit: allow at most 2 pending (pre-hello) connections per + // source IP to prevent reconnect storms where rapid retries from one peer + // fill the global pending pool and starve other peers. A limit of 2 (rather + // than 1) tolerates one overlapping connection during normal reconnects. + const Async::IpAddress remote_ip = con->remoteHost(); + unsigned ip_pending = 0; + for (const auto& kv : m_trunk_pending_cons) + { + if (kv.first->remoteHost() == remote_ip) + { + ++ip_pending; + } + } + if (ip_pending >= 2) + { + std::cerr << "*** WARNING: TRUNK inbound from " << remote_ip + << ": too many pending connections from this IP — rejecting" + << std::endl; + con->disconnect(); + return; + } + + // Reject connections beyond the global pending limit to prevent fd exhaustion if (m_trunk_pending_cons.size() >= TRUNK_MAX_PENDING_CONS) { - std::cerr << "*** WARNING: TRUNK inbound from " - << con->remoteHost() + std::cerr << "*** WARNING: TRUNK inbound from " << remote_ip << ": too many pending connections — rejecting" << std::endl; con->disconnect(); return; diff --git a/src/svxlink/reflector/TrunkLink.cpp b/src/svxlink/reflector/TrunkLink.cpp index d6efd20..9ffe5f0 100644 --- a/src/svxlink/reflector/TrunkLink.cpp +++ b/src/svxlink/reflector/TrunkLink.cpp @@ -189,6 +189,8 @@ bool TrunkLink::initialize(void) << " remote_prefix=" << joinPrefixes(m_remote_prefix) << endl; m_con.addStaticSRVRecord(0, 0, 0, m_peer_port, m_peer_host); + m_con.setReconnectMinTime(2000); + m_con.setReconnectMaxTime(30000); m_con.connect(); return true;