fix(trunk): guard inbound send paths against closed sockets; clean up on heartbeat timeout

A non-paired trunk link could abort the reflector (assert sock >= 0 in
AsyncTcpConnection::write) when an inbound peer was timed out by the
heartbeat. disconnect() closes the socket but does not emit the disconnected
signal, so onInboundDisconnected never runs and m_inbound_con /
m_ib_hello_received stay set. isInboundReady() then reports the dead link as
ready, and the next send — e.g. a MsgPeerTgInterest scheduled by a client
publishing its monitored-TG list — falls through to sendMsgOnInbound and
writes to the closed socket. Reproduces the production abort seen on v1.3.12;
TrunkLink is unchanged through v1.3.13.

- sendMsgOnInbound / isInboundReady: bail when the inbound socket is closed.
- inbound heartbeat timeout: drive the full cleanup (onInboundDisconnected +
  drop the dead connection from the reflector inbound map) so the link
  self-heals and /status is honest, instead of leaving a phantom inbound that
  makes acceptInboundConnection reject every reconnect until restart.
- defense in depth: same isConnected() guard on sendMsgOnOutbound and
  SatelliteLink::sendMsg (currently unreachable, but never write to a closed
  socket regardless).
- tests: test_46 (no crash on timeout + TG-interest send), test_47 (inbound
  recovers after a timeout).
This commit is contained in:
Audric committed 2026-05-21 14:27:27 +02:00
1 parent 8f00196821
commit b268bfe595
5 files changed
+147 -3

No files matched your search

+109
View File
@@ -3119,6 +3119,115 @@ class TestTrunkIntegration(unittest.TestCase):
a_client.close()
c_client.close()
def test_46_inbound_heartbeat_timeout_no_crash(self):
"""Regression: a non-paired trunk link whose inbound socket the
reflector closes on heartbeat timeout must not abort the process on
the next send (assert sock>=0 in AsyncTcpConnection::write).
Mirrors the production incident. TRUNK_TEST's outbound leg points at
the unroutable 192.0.2.1, so only the inbound leg (this mock peer) is
ever up. The peer goes silent; ~15s later the reflector times out the
inbound RX heartbeat and calls m_inbound_con->disconnect(), which does
NOT emit the disconnected signal -> onInboundDisconnected never runs,
so m_inbound_con stays non-null and m_ib_hello_received stays true and
isInboundReady() lies. A client then publishes a monitored-TG set that
is shared toward TRUNK_TEST (prefix 9); the resulting MsgPeerTgInterest
send falls through to sendMsgOnInbound, which writes to the closed
socket -> SIGABRT. With no restart policy the container then exits, so
/status becomes unreachable.
"""
target = self.PRIMARY
# 1. Inbound-only trunk peer on TRUNK_TEST (its outbound leg is down).
peer = TrunkPeer()
peer.connect(*_trunk(target))
msg_type, _ = peer.handshake() # TRUNK_TEST, prefix 9, test_secret
self.assertEqual(msg_type, MSG_TRUNK_HELLO)
wait_until(
lambda: get_status(*_http(target))["trunks"]["TRUNK_TEST"]
.get("inbound_connected", False),
timeout=10.0, msg="TRUNK_TEST inbound did not connect")
client = None
try:
# 2. Go silent (send no heartbeats, keep the socket open) so the
# reflector times out the inbound RX (HEARTBEAT_RX_CNT_RESET=15
# @ 1s) and self-disconnects it. Do NOT peer.close() here: a
# clean close fires the disconnected signal and cleans up.
# The timeout log line has no /status equivalent on the buggy
# build (inbound_connected stays true), so gate on the log.
wait_until(
lambda: "Inbound heartbeat timeout" in
docker_logs_since(target, since="40s"),
timeout=30.0, interval=1.0,
msg="reflector did not time out the silent inbound trunk")
# 3. Trigger: a client publishes a monitored-TG set shared toward
# TRUNK_TEST (prefix 9), scheduling a MsgPeerTgInterest on it.
shared_tg = int(TEST_PREFIX_FIRST + "123") # e.g. 9123, owned by peer
client = ClientPeer()
client.connect(HOST, T.mapped_client_port(target))
client.authenticate()
client.monitor_tgs([shared_tg])
# 4. The 500ms TG-interest debounce fires the send. The reflector
# must still be alive afterwards (no assert abort / exit).
time.sleep(2.0)
try:
get_status(*_http(target))
except Exception as e:
self.fail(
f"reflector '{target}' crashed after inbound heartbeat "
f"timeout + TG-interest send (write to closed socket): {e}")
finally:
if client is not None:
client.close()
peer.close()
def test_47_inbound_recovers_after_heartbeat_timeout(self):
"""Robustness: after the reflector times out and drops a silent inbound
trunk peer, the same peer must be able to reconnect its inbound leg.
Guarding the send path alone stops the crash (test_46) but would leave
m_inbound_con dangling: /status would keep reporting a phantom inbound
and acceptInboundConnection (one inbound per non-paired link) would
reject every reconnect until the reflector restarts. The timeout path
must clear the inbound state so the link self-heals.
"""
target = self.PRIMARY
# First inbound peer, then go silent until the reflector times it out.
peer1 = TrunkPeer()
peer1.connect(*_trunk(target))
self.assertEqual(peer1.handshake()[0], MSG_TRUNK_HELLO)
wait_until(
lambda: get_status(*_http(target))["trunks"]["TRUNK_TEST"]
.get("inbound_connected", False),
timeout=10.0, msg="TRUNK_TEST inbound did not connect")
try:
# The reflector must clear the inbound from /status after timing it
# out — not leave a phantom that blocks reconnects.
wait_until(
lambda: not get_status(*_http(target))["trunks"]["TRUNK_TEST"]
.get("inbound_connected", True),
timeout=30.0, interval=0.5,
msg="reflector left a phantom inbound after heartbeat timeout")
finally:
peer1.close()
# A fresh inbound from the same peer must now be accepted.
peer2 = TrunkPeer()
peer2.connect(*_trunk(target))
try:
self.assertEqual(peer2.handshake()[0], MSG_TRUNK_HELLO)
wait_until(
lambda: get_status(*_http(target))["trunks"]["TRUNK_TEST"]
.get("inbound_connected", False),
timeout=10.0,
msg="TRUNK_TEST inbound did not recover after a timeout")
finally:
peer2.close()
# ---------------------------------------------------------------------------
# Custom test runner with clean, readable output