Repository navigation
common: fail RPC receive when the peer closes mid-payload - #113
Conversation
SoapyRPCUnpacker::recv() reads the payload in a loop and only treats a negative recv() result as an error. When the server closes the connection after the header but before the whole payload has arrived, recv() keeps returning 0, bytesReceived never advances and the loop spins forever at full CPU, holding the device mutex. Every other call on that device then blocks as well. Throw a runtime_error when recv() returns 0, like the header receive already does, so the caller sees the dead link instead of hanging.
|
@AsciiWolf could you take a look pls? It's a 6-line change in |
|
Great fix, thank you! "For TCP sockets, the return value 0 means the peer has closed its half side of the connection." -- we don't get the same guarantee for UDP sockets. I don't think UDP is used for the control connection though. |
|
Thanks! I checked, and every
The UDP sockets (stream data endpoints, SSDP) don't go through the unpacker. The header read also already relies on stream semantics: |
SoapyRPCUnpacker::recv()reads the payload in a loop that only treats a negativerecv()result as an error:If the server closes the connection after sending the header but before the whole payload has arrived,
recv()returns 0 on every call.bytesReceivednever advances, and the client spins at 100% CPU forever. For device calls this happens while holding_mutex, so every other call on that device blocks too. This PR throws aruntime_errorwhenrecv()returns 0, as the header receive already does withMSG_WAITALL.It only matters when the server goes away part-way through sending a reply, which in practice means a reply longer than one TCP segment. It is a hang rather than a crash, so it's rare but hard to diagnose. The change is independent of #112: it touches a different file, and that PR doesn't cover this path.
Reproducing
A fake server that sends a valid header announcing a 4096-byte packet, then 8 payload bytes, then FIN:
The client side is a one-line program calling
SoapySDR::Device::make("driver=remote,remote=tcp://127.0.0.1:PORT"):SoapyRPCUnpacker::recv()→SoapyRPCSocket::recv()(SoapyRPCUnpacker.cpp:107), reached viafindRemote(). I killed it after 12 s.SoapyRemote::find(tcp://127.0.0.1:…) -- transact FAIL: SoapyRPCUnpacker::recv(payload) FAIL: connection closed by peer, andmake()throws.Testing
SoapySDRServerserving a fake RX device, over the default UDP stream: 100 activate/deactivate cycles at 250 kS/s plussetFrequency. Behaviour was identical to master: no errors, and samples kept flowing.Disclosure: I found and tested this while looking into gqrx-sdr/gqrx#1445. That issue is fixed by #112, not by this PR.