Repository navigation
Conversation
onur-ozkan
left a comment
There was a problem hiding this comment.
Thanks for working on this!
| Some(self.writer.lock().unwrap()) | ||
| } | ||
|
|
||
| fn lock_writer_for_nonblocking_read(&self) -> Option<std::sync::MutexGuard<'_, Self::Writer>> { |
There was a problem hiding this comment.
Creating another lock function next to lovk_writer seems very weak design choice to me. We should do something better than this.
There was a problem hiding this comment.
Agreed, I dropped both hooks. SyncTransport now has one method, try_read, that runs the read for try_recv; TCP and UDP override it and switch the nonblocking flag through their writer guard, so the generic code no longer knows about the shared socket and the flag can't change while a send is running. I tried a short read timeout first so writes would be untouched, but the shortest one costs about 8 ms per empty try_recv on Linux and 15.6 ms on Windows versus about 1 us nonblocking, which doesn't fit a try_ call. I added tests for an empty try_recv returning fast and leaving the socket blocking, and for a frame split across two reads, and the workspace tests pass on Linux and Windows.
TCP and UDP readers and writers are clones of one socket and share its nonblocking flag. try_recv flipped that flag with only the reader lock held, so a concurrent send under backpressure could fail with WouldBlock, for TCP after part of a frame was already written. SyncTransport's set_nonblocking hook is replaced by a single try_read method that runs the read for try_recv. The default reads as before. TCP and UDP switch the flag through their writer guard, so it can only change while no send is running, and the generic connection code no longer needs to know which transports share a socket. A short read timeout instead of nonblocking mode would leave writes alone, but the shortest one costs about 8 ms per empty try_recv on Linux and 15.6 ms on Windows, against about 1 us for a nonblocking read. Tests: 20,000 FILE_TRANSFER_PROTOCOL sends while another thread polls try_recv (failed on send 616 with WouldBlock before this change), 100 empty try_recv calls return within 200 ms and leave the socket blocking, and a frame written in two pieces is decoded intact. Fixes mavlink#570
e278e89 to
1516385
Compare
Fixes #570.
TCP and UDP readers and writers are clones of one socket, so they share its nonblocking flag.
try_recvturned that flag on while holding only the reader lock. Asendrunning at the same time under backpressure could then getWouldBlock, and on TCP that can happen after part of a frame is already on the wire.Fix:
SyncTransport'sset_nonblockinghook is replaced by one method,try_read, which runs the read fortry_recv. The default just reads, so serial and file are unchanged. TCP and UDP override it and switch the flag through their writer guard, so the flag can only change while no send is running, and the generic connection code no longer needs to know which transports share a socket.Why not a read timeout: it would leave writes alone, but the shortest
SO_RCVTIMEOcosts about 8 ms per emptytry_recvon Linux and 15.6 ms on Windows, against about 1 us for a nonblocking read. That breaks the "returns immediately" contract.Tests in
mavlink/tests/tcp_try_recv_concurrent_send_test.rs:FILE_TRANSFER_PROTOCOLsends while another thread loops ontry_recv, with the server pausing 500 ms so buffers fill. Without the fix send 616 fails withWouldBlock(os error 10035 on Windows).try_recvcalls return within 200 ms, and a followingrecvstill waits for the 100 mstcpoutread timeout, so the socket is left blocking.WouldBlockuntil the rest arrives, then decodes intact.Results:
cargo test --workspace --features "dialect-all transport-tcp transport-udp std mav2-message-signing"passes on Linux (WSL Ubuntu 24.04) and the lib and integration tests pass on Windows.cargo clippy -p mavlink-core --all-targetsis clean.