Repository navigation
On Linux, connect again after a KVM leaves the USB bus and returns - #1
Merged
Merged
Conversation
…never complete When a KVM left the bus, the sender sometimes logged "USB writer failed: USB session cancelled" and then nothing more. The process stayed alive and claimed no later instance of the KVM. The reader thread drops its half of the link when the session ends, and that drop waited until every cancelled read completed. On Linux, nusb stops reaping a device at the first REAPURBNDELAY that the kernel answers with ENODEV. The kernel answers so from the moment it marks the device as not attached, which is before it completes the device's transfers. The reads that completed after that were never reaped, the drop waited without an end, and the session thread waited in its join of the reader. A cancelled write had the same wait. The wait for cancelled transfers now ends after 2 s. The transfers that are left belong to a device that is gone, so no session can overlap them. nusb keeps their buffers and the device file open.
Each time a KVM left the bus, the sender kept the device file of that KVM open, and up to four read buffers. On a host with a direct xHCI connection this happened at nearly every disconnect: after five mount tests the sender held 21 device files. nusb stopped reaping a device at the first REAPURBNDELAY that the kernel answered with ENODEV. The kernel answers ENODEV once the device is marked as not attached and no completed URB waits, and usb_disconnect sets that mark before it completes the device's URBs. The URBs that completed afterwards were never reaped, so their transfers stayed pending and held the endpoint, the interface and the device file. vendor/nusb is nusb 0.2.7 with one change, and [patch.crates-io] selects it. At the first ENODEV, nusb changes its epoll watch on the device file to edge-triggered and keeps it. The kernel wakes the file for each URB that completes and when it removes the device, and nusb reaps every completed URB at each wake-up. The watch is edge-triggered because the file of a disconnected device is always ready, and a level-triggered watch makes the event thread spin. vendor/nusb/UPSTREAM.md names the source, and vendor/nusb/reap-after-disconnect.patch is the diff. The 2 s limit on the wait for cancelled transfers stays as a backstop.
adamshiervani
commented
Nov 16, 2026
adamshiervani
left a comment
Contributor
Author
There was a problem hiding this comment.
No findings.
Validation: Inspected the changes, relevant call sites, and tests. The vendored patch passed a reverse/apply check. Rust tests could not run because Cargo and rustc are unavailable. USB hardware tests were not run.
Reviewed commit 119794479375fd6417079b368d2901039c1b95e3.
…pected reap error The watch on the file of a disconnected device is edge-triggered, so the event thread gets one event for each wake-up of the file. An unexpected error from REAPURBNDELAY ended the pass, and the completed URBs that waited behind it were reaped only at the next wake-up. After the kernel removes the device, no wake-up comes. Those transfers then stayed pending and kept the device file open. The pass now continues after an unexpected error. It ends when no URB is left (EAGAIN or ENODEV), or after eight errors, so an error that repeats does not keep the event thread in the pass. vendor/nusb/reap-after-disconnect.patch is the new diff against nusb 0.2.7, and vendor/nusb/UPSTREAM.md describes the rule.
…fers to complete The two tests of the wait for cancelled transfers used an endpoint that never completes a transfer. A teardown that cancels and returns at once passed both. Two more tests use an endpoint that completes its transfers a few polls after the cancel. They check that no transfer is in flight when the drop of the read half returns, and when the cancelled write returns. The test of the cancelled write asserted inside the thread that it starts, so a failure showed as a time-out. The thread now returns the value, and the test thread asserts. The comment on cancel_and_reap described nusb before the change in vendor/nusb. It now says what the limit is for, what nusb does with the transfers that are left, and why a later session cannot overlap them. It also says that the control transfers and the claim of the interface have no limit of their own.
adamshiervani
commented
Nov 16, 2026
adamshiervani
left a comment
Contributor
Author
There was a problem hiding this comment.
No findings.
Validation: Reviewed the revision, relevant call sites, tests, and prior review context. The vendor patch passed an exact reverse/apply check. Rust tests could not run because Cargo and rustc are unavailable. USB hardware tests were not run.
Reviewed commit b0af1cc9ce7b775c085481b661d7523160a392f7.
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.
What this changes
Problem. On Linux, when a KVM leaves the USB bus and comes back, the sender sometimes logs "USB writer failed: USB session cancelled" and then does nothing more. The process stays alive and never claims the KVM again. It also keeps the device file of each KVM that left.
Cause. nusb stops reaping a device at the first
REAPURBNDELAYthat the kernel answers withENODEV. The kernel answers so before it completes the device's transfers. Transfers that complete afterwards are never reaped, so the sender's wait for its cancelled reads never ends, and the device file stays open.Fix.
cancel_and_reap()insrc/protocol/usb.rs). This is a backstop for a reap that does not occur.vendor/nusbis nusb 0.2.7 with one change, selected by[patch.crates-io]: after the firstENODEV, nusb keeps an edge-triggered watch on the device file and reaps every transfer that completes, also behind an unexpected reap error.vendor/nusb/UPSTREAM.mdnames the source, andvendor/nusb/reap-after-disconnect.patchis the diff: pristine 0.2.7 plus the patch equalsvendor/nusb/src.The production release 0.4.5 has the same fault.
How you tested it
authorized0 and 1). It held one device file throughout.