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

+6
View File
@@ -2482,6 +2482,12 @@ void Reflector::trunkClientConnected(Async::FramedTcpConnection* con)
} /* Reflector::trunkClientConnected */
void Reflector::forgetInboundTrunkConnection(Async::FramedTcpConnection* con)
{
m_trunk_inbound_map.erase(con);
} /* Reflector::forgetInboundTrunkConnection */
void Reflector::trunkClientDisconnected(Async::FramedTcpConnection* con,
Async::FramedTcpConnection::DisconnectReason reason)
{
+6
View File
@@ -379,6 +379,12 @@ class Reflector : public sigc::trackable
const std::string& host = "",
uint16_t port = 0);
// Drop a no-longer-tracked inbound trunk connection from the inbound map.
// Used by TrunkLink when it proactively disconnects an inbound peer (e.g.
// a heartbeat timeout), since TcpConnection::disconnect() does not emit the
// disconnected signal that would otherwise trigger trunkClientDisconnected.
void forgetInboundTrunkConnection(Async::FramedTcpConnection* con);
// Triggered when a local client logs in/out or changes TG. Schedules
// a debounced node-list emission to all trunk peers and to MQTT.
void scheduleNodeListUpdate(void);
+4
View File
@@ -453,6 +453,10 @@ void SatelliteLink::handleMsgPeerFlush(std::istream& is)
void SatelliteLink::sendMsg(const ReflectorMsg& msg)
{
// Defense in depth: never write to a closed socket — AsyncTcpConnection::
// write() asserts sock >= 0. Mirrors the guard on the satellite (client)
// side (SatelliteClient gates every sender on isConnected()).
if (m_con == nullptr || !m_con->isConnected()) return;
ostringstream ss;
ReflectorMsg header(msg.type());
if (!header.pack(ss) || !msg.pack(ss))
+22 -3
View File
@@ -1289,6 +1289,10 @@ void TrunkLink::sendMsg(const ReflectorMsg& msg)
void TrunkLink::sendMsgOnOutbound(const ReflectorMsg& msg)
{
// Defense in depth: callers gate on isOutboundReady() (which checks
// isConnected()), but never write to a closed socket regardless —
// AsyncTcpConnection::write() asserts sock >= 0.
if (!m_con.isConnected()) return;
ostringstream ss;
ReflectorMsg header(msg.type());
if (!header.pack(ss) || !msg.pack(ss))
@@ -1304,7 +1308,11 @@ void TrunkLink::sendMsgOnOutbound(const ReflectorMsg& msg)
void TrunkLink::sendMsgOnInbound(const ReflectorMsg& msg)
{
if (m_inbound_con == nullptr) return;
// Never write to a closed socket: AsyncTcpConnection::write() asserts
// sock >= 0. The pointer can outlive its socket when we disconnect() the
// inbound ourselves (heartbeat timeout) — disconnect() closes the fd
// without emitting the disconnected signal that clears m_inbound_con.
if (m_inbound_con == nullptr || !m_inbound_con->isConnected()) return;
ostringstream ss;
ReflectorMsg header(msg.type());
if (!header.pack(ss) || !msg.pack(ss))
@@ -1432,7 +1440,17 @@ void TrunkLink::heartbeatTick(Async::Timer* t)
if (--m_ib_hb_rx_cnt == 0)
{
geulog::error("trunk", "[", m_section, "] Inbound heartbeat timeout");
m_inbound_con->disconnect();
// disconnect() closes the socket but does NOT emit the disconnected
// signal, so neither the trunk server's onDisconnected nor
// onInboundDisconnected runs on its own. Drive the cleanup ourselves so
// the link stops advertising a dead inbound (otherwise isInboundReady()
// lies and acceptInboundConnection rejects every reconnect), and drop
// the now-dead connection from the reflector's inbound map.
Async::FramedTcpConnection* dead = m_inbound_con;
dead->disconnect();
m_reflector->forgetInboundTrunkConnection(dead);
onInboundDisconnected(
dead, Async::FramedTcpConnection::DR_ORDERED_DISCONNECT);
}
else if (m_ib_hb_rx_cnt <= 5)
{
@@ -1497,7 +1515,8 @@ bool TrunkLink::isInboundReady(void) const
}
return false;
}
return m_inbound_con != nullptr && m_ib_hello_received;
return m_inbound_con != nullptr && m_ib_hello_received &&
m_inbound_con->isConnected();
} /* TrunkLink::isInboundReady */