Fix serial device disconnects: concurrent, exactly-once URB handling#15
Open
kkkkk1k1 wants to merge 2 commits into
Open
Fix serial device disconnects: concurrent, exactly-once URB handling#15kkkkk1k1 wants to merge 2 commits into
kkkkk1k1 wants to merge 2 commits into
Conversation
The URB traffic loop processed submissions sequentially and returned
ETIMEDOUT (-110) whenever an IN endpoint had no data ready. For serial
devices (CDC-ACM, CH34x, ...) this broke the whole chain:
- Interrupt/bulk IN endpoints that legitimately have no data pending were
completed with -110 instead of staying pending like real hardware, which
made the CH340 modem-status read fail and ttyUSB/ttyACM open() return EIO.
- A single blocking IN read head-of-line blocked control and OUT transfers,
so open() timed out.
- During flashing the client frequently unlinks slow URBs; the worker still
sent a RET_SUBMIT for an already-unlinked seqnum, which makes the Linux
vhci_hcd abort the connection ("cannot find a urb of seqnum ...") and drop
the serial port.
Rework the loop so the receive thread only does socket I/O and dispatches
each URB to a worker thread:
- IN endpoints stay pending (polled in coarse slices) and complete only when
data arrives or the URB is unlinked - no more spurious -110.
- Per-endpoint locks keep same-endpoint transfers ordered while different
endpoints run concurrently, so a pending IN read never stalls control/OUT.
- Exactly-once completion (per-URB lock + flag) guarantees a URB is finished
by either RET_SUBMIT or RET_UNLINK, never both, fixing the fatal
RET_UNLINK-before-RET_SUBMIT ordering.
- Socket writes are serialised; USBIP_CMD_UNLINK now actually cancels the
pending URB.
Add mocked pytest regression tests (no hardware) covering concurrency,
pending-IN unlink, and the exactly-once unlink race.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
USB/IP is a request/response protocol made up of many small PDUs. Leaving Nagle's algorithm enabled on the accepted client socket lets small URB responses be held back waiting for an ACK, which interacts badly with the peer's delayed-ACK timer. The Linux usbip userspace sets TCP_NODELAY on its sockets for this reason (usbip_net_set_nodelay); do the same here. Note: this is protocol hygiene, not a fix for any specific measured regression. On one tested setup (client reaching the server through an SSH tunnel) the dominant per-URB latency was ~48 ms and lived in the tunnel hops, not in this socket - enabling TCP_NODELAY did not change it there. Factored into a small helper so it can be unit-tested without a running server. Co-Authored-By: Claude Opus 4.8 <[email protected]>
kkkkk1k1
force-pushed
the
fix/async-urb-exactly-once
branch
from
July 21, 2026 08:13
cc52eef to
df2ad5a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Sharing a USB serial device (CDC-ACM, CH34x, …) over usbipd-python did not work reliably: the client-side port either failed to open (
EIO) or dropped mid-transfer. Root causes in the URB traffic loop:_do_bulk_interrupt_transferreturnedETIMEDOUT (-110)whenever an IN endpoint had no data ready. On real hardware an IN endpoint with nothing to send just NAKs and the URB stays pending. Returning-110poisoned the CH340 interrupt status endpoint and made thech341/cdc_acmopen()fail:open()depends on.RET_SUBMITfor an already-unlinked seqnum, sovhci_hcdaborted the whole connection and dropped the port:Change
Rework
_handle_urb_trafficso the receive thread only does socket I/O and dispatches each URB to a worker thread:-110.completedflag): a URB is finished by eitherRET_SUBMITorRET_UNLINK, never both — fixing the fatalRET_UNLINK-before-RET_SUBMITordering.USBIP_CMD_UNLINKnow actually cancels the pending URB.Tests
Adds
tests/test_urb_handling.py— mocked USB device + scripted in-memory socket (no libusb, no hardware, nosudo), covering:RET_SUBMIT,UNLINK-vs-completion race.Also adds
pytestto thedevextra and a[tool.pytest.ini_options]section.Validation
ruff format --check .,ruff check .,mypy --ignore-missing-imports .,reuse lint,pytestall pass.ttyUSB0) and CH343/CDC-ACM (ttyACM0) shared from macOS to a Linux client — serial loopback is byte-accurate and firmware flashing (cskburn) completes without the port dropping.🤖 Generated with Claude Code