Transport loop deadlocks when a resource transfer is interrupted
What's happening
I ran into a deadlock when a peer disappears mid-resource transfer. The receiving node gets stuck in its transport loop and stops responding to announces or new links until restarted. This could affect any long-running node that receives resources.
Reproduction
Connect two nodes over UDP (loopback works), start a resource transfer (~30 KB), then SIGKILL the sender mid-transfer. The receiving node gets stuck on its next resource tick.
Backtrace:
#0 nanosleep
#1 OS::sleep
#2 RNS::Transport::outbound(RNS::Packet&)
#3 RNS::Packet::receipt_send()
#4 RNS::Resource::request_next()
#5 RNS::Link::tick_resources()
#6 RNS::Transport::jobs()
#7 RNS::Transport::loop()
#8 RNS::Reticulum::loop()
Cause
It looks like a reentrancy issue with _jobs_running. Transport::jobs() sets the flag to true, then calls Link::tick_resources(), which can send a RESOURCE_REQ through Transport::outbound().
Since outbound() waits for _jobs_running to become false, the transport loop ends up waiting for itself indefinitely. inbound() has the same potential issue.
Possible fix
I've been testing a small thread-local guard that lets calls originating from the transport cycle skip the wait while preserving the existing behavior for other threads:
// In Transport::start() and Transport::jobs(),
// after setting _jobs_running = true:
TransportCycleGuard guard;
// In Transport::outbound() and Transport::inbound():
while (_jobs_running && !t_in_transport_cycle) {
OS::sleep(0.0005);
}
With this crude patch, repeated mid-transfer kills leave an incomplete resource, but the receiving node keeps accepting new links.
A more elegant option might be to queue the RESOURCE_REQ instead of sending it directly from jobs().
Keep up the great work!