From d757384c9c1ba59ed5abdf38f19ebb2258a101fc Mon Sep 17 00:00:00 2001 From: Audric Date: Sun, 17 May 2026 02:20:50 +0200 Subject: [PATCH] fix(satellite): defer heartbeat until auth; reject hello triggers cleanup SatelliteLink's heartbeat timer was constructed with the default enabled=true, so it started ticking at connect time. When a hello was rejected, m_con->disconnect() closed the socket but the timer kept running; at the t=10s tx tick it called sendMsg, which hit assert(sock >= 0) in TcpConnection::write and crashed the reflector. TcpConnection::disconnect() intentionally does not emit the disconnected signal (header comment), so the TcpServer's clientDisconnected handler never ran -- the SatelliteLink stayed in m_satellite_con_map with no path to cleanup. TrunkLink avoids this by constructing its heartbeat with enabled=false and enabling it only after auth (TrunkLink.cpp:197); SatelliteLink just missed the pattern. Mirror TrunkLink: construct the heartbeat disabled, enable it after successful authentication in handleMsgPeerHello. On all four rejection branches, fire linkFailed(this) after m_con->disconnect() so the existing onSatelliteLinkFailed -> processSatelliteCleanup path deletes the leaked link. Add test_rejection_does_not_crash_parent regression test that sends a hello with a wrong-for-pinned-id secret, waits past the 10s heartbeat threshold, and confirms /status still responds and shows no phantom satellite entry. --- src/svxlink/reflector/SatelliteLink.cpp | 15 ++++++++++++++- tests/test_satellite_secrets.py | 24 +++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/svxlink/reflector/SatelliteLink.cpp b/src/svxlink/reflector/SatelliteLink.cpp index 0d76828..e2a0610 100644 --- a/src/svxlink/reflector/SatelliteLink.cpp +++ b/src/svxlink/reflector/SatelliteLink.cpp @@ -90,7 +90,11 @@ SatelliteLink::SatelliteLink(Reflector* reflector, Async::FramedTcpConnection* con) : m_reflector(reflector), m_con(con), m_hello_received(false), - m_heartbeat_timer(1000, Timer::TYPE_PERIODIC), + // Heartbeat starts disabled; handleMsgPeerHello turns it on after + // successful authentication (mirrors TrunkLink). If a hello is + // rejected the timer never ticks, so it cannot try to write to a + // socket that m_con->disconnect() has already closed. + m_heartbeat_timer(1000, Timer::TYPE_PERIODIC, false), m_hb_tx_cnt(HEARTBEAT_TX_CNT_RESET), m_hb_rx_cnt(HEARTBEAT_RX_CNT_RESET) { @@ -287,6 +291,7 @@ void SatelliteLink::handleMsgPeerHello(std::istream& is) { geulog::error("satellite", "Satellite sent empty ID"); m_con->disconnect(); + linkFailed(this); return; } @@ -295,6 +300,7 @@ void SatelliteLink::handleMsgPeerHello(std::istream& is) geulog::error("satellite", "Expected ROLE_SATELLITE from '", msg.id(), "' but got role=", (int)msg.role()); m_con->disconnect(); + linkFailed(this); return; } @@ -307,6 +313,7 @@ void SatelliteLink::handleMsgPeerHello(std::istream& is) msg.id(), "' (no SECRET_= match and no fallback " "SECRET=) — rejecting"); m_con->disconnect(); + linkFailed(this); return; } @@ -316,12 +323,18 @@ void SatelliteLink::handleMsgPeerHello(std::istream& is) "Authentication failed for satellite '", msg.id(), "' (pinned=", (pinned ? "true" : "false"), ")"); m_con->disconnect(); + linkFailed(this); return; } m_resolved_secret = secret; m_satellite_id = msg.id(); m_hello_received = true; + // Now that we're authenticated, start ticking the heartbeat. The + // timer is constructed disabled (see ctor) so that a rejected hello + // cannot leave it ticking against a socket m_con->disconnect() has + // already closed. + m_heartbeat_timer.setEnable(true); geulog::info("satellite", "Satellite '", m_satellite_id, "' authenticated (pinned=", (pinned ? "true" : "false"), ")"); diff --git a/tests/test_satellite_secrets.py b/tests/test_satellite_secrets.py index bff860d..3e50218 100644 --- a/tests/test_satellite_secrets.py +++ b/tests/test_satellite_secrets.py @@ -102,7 +102,29 @@ class SatelliteSecretsTest(unittest.TestCase): finally: sat.close() - # 4. Malformed SECRET_= is ignored at startup; the id still + # 4. Regression: a rejected hello must not leave a SatelliteLink whose + # heartbeat timer keeps ticking against a closed socket. Pre-fix, + # the parent asserted in TcpConnection::write ~10s after rejection + # (because the heartbeat timer was constructed enabled and never + # got disabled when the hello was rejected). Send a bad hello, + # wait past the 10s tx-heartbeat threshold, then confirm /status + # still responds (i.e. the parent process is alive). + def test_rejection_does_not_crash_parent(self): + peer = SatellitePeer() + peer.connect_satellite() + priority = struct.unpack("!I", os.urandom(4))[0] + hello = build_trunk_hello(T.SATELLITE_PINNED_ID, "", priority, + SAT_SECRET, role=ROLE_SATELLITE) + send_frame(peer.sock, hello) + peer.close() + # Past the t=10s heartbeat tx tick where the pre-fix crash fired. + time.sleep(11.0) + status = get_status(*_http(PRIMARY)) + self.assertIsInstance(status, dict, "parent /status not responding") + self.assertNotIn(T.SATELLITE_PINNED_ID, status.get("satellites", {}), + "phantom satellite entry after rejection") + + # 5. Malformed SECRET_= is ignored at startup; the id still # authenticates via the default fallback. # # We don't grep docker logs for the startup warning here (rolling