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.
This commit is contained in:
Audric committed 2026-05-17 02:20:50 +02:00
1 parent f51ead2d7b
commit d757384c9c
2 files changed
+37 -2

No files matched your search

+14 -1
View File
@@ -90,7 +90,11 @@ SatelliteLink::SatelliteLink(Reflector* reflector,
Async::FramedTcpConnection* con) Async::FramedTcpConnection* con)
: m_reflector(reflector), m_con(con), : m_reflector(reflector), m_con(con),
m_hello_received(false), 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_tx_cnt(HEARTBEAT_TX_CNT_RESET),
m_hb_rx_cnt(HEARTBEAT_RX_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"); geulog::error("satellite", "Satellite sent empty ID");
m_con->disconnect(); m_con->disconnect();
linkFailed(this);
return; return;
} }
@@ -295,6 +300,7 @@ void SatelliteLink::handleMsgPeerHello(std::istream& is)
geulog::error("satellite", "Expected ROLE_SATELLITE from '", msg.id(), geulog::error("satellite", "Expected ROLE_SATELLITE from '", msg.id(),
"' but got role=", (int)msg.role()); "' but got role=", (int)msg.role());
m_con->disconnect(); m_con->disconnect();
linkFailed(this);
return; return;
} }
@@ -307,6 +313,7 @@ void SatelliteLink::handleMsgPeerHello(std::istream& is)
msg.id(), "' (no SECRET_<id>= match and no fallback " msg.id(), "' (no SECRET_<id>= match and no fallback "
"SECRET=) — rejecting"); "SECRET=) — rejecting");
m_con->disconnect(); m_con->disconnect();
linkFailed(this);
return; return;
} }
@@ -316,12 +323,18 @@ void SatelliteLink::handleMsgPeerHello(std::istream& is)
"Authentication failed for satellite '", msg.id(), "Authentication failed for satellite '", msg.id(),
"' (pinned=", (pinned ? "true" : "false"), ")"); "' (pinned=", (pinned ? "true" : "false"), ")");
m_con->disconnect(); m_con->disconnect();
linkFailed(this);
return; return;
} }
m_resolved_secret = secret; m_resolved_secret = secret;
m_satellite_id = msg.id(); m_satellite_id = msg.id();
m_hello_received = true; 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, geulog::info("satellite", "Satellite '", m_satellite_id,
"' authenticated (pinned=", (pinned ? "true" : "false"), ")"); "' authenticated (pinned=", (pinned ? "true" : "false"), ")");
+23 -1
View File
@@ -102,7 +102,29 @@ class SatelliteSecretsTest(unittest.TestCase):
finally: finally:
sat.close() sat.close()
# 4. Malformed SECRET_<bad.id>= 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_<bad.id>= is ignored at startup; the id still
# authenticates via the default fallback. # authenticates via the default fallback.
# #
# We don't grep docker logs for the startup warning here (rolling # We don't grep docker logs for the startup warning here (rolling