Skip to content

On Linux, connect again after a KVM leaves the USB bus and returns - #1

Merged
adamshiervani merged 4 commits into
mainfrom
fix-usb-reconnect-hang
Nov 16, 2026
Merged

adamshiervani merged 4 commits into
mainfrom
fix-usb-reconnect-hang

Conversation

@adamshiervani

@adamshiervani adamshiervani commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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 REAPURBNDELAY that the kernel answers with ENODEV. 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.

  • The wait for cancelled transfers ends after 2 s (cancel_and_reap() in src/protocol/usb.rs). This is a backstop for a reap that does not occur.
  • vendor/nusb is nusb 0.2.7 with one change, selected by [patch.crates-io]: after the first ENODEV, 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.md names the source, and vendor/nusb/reap-after-disconnect.patch is the diff: pristine 0.2.7 plus the patch equals vendor/nusb/src.

The production release 0.4.5 has the same fault.

How you tested it

  • Unit tests for the teardown: it ends when transfers never complete, and it waits for transfers that do complete.
  • On a Linux VM with a passed-through xHCI controller and a JetKVM Mini, where the fault showed: the mount test that makes the KVM leave and return passed 5 of 5 times with the first two commits. Before, the sender hung in this test, and after five such tests it held 21 device files; with the nusb change it held none that were stale.
  • On a Linux PC (Intel N100) with a JetKVM Mini, with the build of this branch:
    • 40 of 40 times the sender claimed the KVM again within 1.2 s after the KVM left the bus and returned (authorized 0 and 1). It held one device file throughout.
    • The Mini's integration tests that mount virtual media: 57 of 58 passed. The one failure was an upload that the Mini refused for low memory; that test passed 3 of 3 times when run again.
    • The published sender 0.4.5 did not show the fault on this PC in the same runs, so these runs show no regression but do not show the fix.

…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 adamshiervani left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 adamshiervani left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@adamshiervani
adamshiervani merged commit fb13efe into main Nov 16, 2026
4 checks passed
@adamshiervani
adamshiervani deleted the fix-usb-reconnect-hang branch November 16, 2026 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant