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