Skip to content

xhc-whb04b-6: bound the display control transfer timeout - #4838

Merged
grandixximo merged 1 commit into
LinuxCNC:2.9from
hansfbaier:xhc-whb04b-6-libusb-reconnect
Nov 24, 2026
Merged

grandixximo merged 1 commit into
LinuxCNC:2.9from
hansfbaier:xhc-whb04b-6-libusb-reconnect

Conversation

@hansfbaier

@hansfbaier hansfbaier commented Nov 13, 2026 •

Copy link
Copy Markdown

Problem

A WHB04B-6 handwheel stops responding after a few hours of use, with no error
anywhere: whb.pendant.is-connected stays TRUE, dmesg is clean, the component
keeps running, the mill keeps working. Unplugging and replugging the receiver
brings it back; power-cycling the pendant does not.

Usb::sendDisplayData() sends each display update with

libusb_control_transfer(deviceHandle, ..., /* transfer timeout[ms] */ 0);

and libusb reads a timeout of 0 as "wait as long as it takes". The call runs on
the thread that pumps libusb events (XhcWhb04b6Component::process()), so a
receiver that stops completing the transfer stops the component: the jog dial is
dead, the input callback never runs again, and is-connected stays TRUE until
the receiver is unplugged, which aborts the blocked transfer with NO_DEVICE.

Change

  1. Give the transfer a 1 s deadline. A healthy transfer completes in well under
    a millisecond, so the deadline costs nothing until something is wrong.
  2. Report the failure through transferFailed, not only mDoReconnect.

Both are needed. mDoReconnect is read in one place, Usb::init() (usb.cc:607),
and init() is reached only after process() returns, which requires
isDeviceOpen() — that is, deviceHandle — to be null. The error branch cleared
nothing, so with the timeout alone the loop just retried the stalled write once
per pass: a 1 s stall instead of an infinite one, connection still reported
healthy. transferFailed is the flag Usb::process() (usb.cc:750) acts on: it
closes the device, sets mDoReconnect, and the outer loop in
XhcWhb04b6Component::run() re-enumerates and reconnects.

A timed-out IN transfer (usb.cc:523) deliberately does not set transferFailed,
because for an idle dial that timeout is normal. That is also why a receiver that
stops answering the HID control pipe produced no failure at all: the only event
that would have been noticed is the 750 ms idle timeout.

Testing

Builds and runs on LinuxCNC 2.9.10, Raspberry Pi 5 (arm64), receiver 10ce:eb93:
starts, connects, updates the display, moves the axes. The freeze itself needs a
wedged receiver and was not reproduced on demand, so there is no field log yet.
The machine runs a 5 s sampler of the component's thread state plus a dump
script, and a field occurrence will be reported here.

Without hardware, the state machine was exercised against a stub libusb that
reports a phantom receiver, times the interrupt IN transfer out every 750 ms like
an idle pendant, and returns LIBUSB_ERROR_TIMEOUT from the control transfer
after 40 calls:

timeout = 1000 only
  2.58 transmission failed, try to reconnect ...
  2.78 transmission failed, try to reconnect ...
  2.98 transmission failed, try to reconnect ...     (47 in 12 s, no cleanup)

timeout = 1000 + transferFailed
  2.58 transmission failed, try to reconnect ...
  3.78 connection lost, cleaning up
  7.78 init  pausing 3s, waiting for device to be gone ....... done
  7.78 init  XHC-WHB04B-6 device found
  7.78 init  claiming interface ... ok
  7.78 init  enabling reception ... ok
  7.78 transmission failed, try to reconnect ...     (receiver still wedged)
  8.98 connection lost, cleaning up

While the receiver stays wedged that cycle repeats every ~4 s, with
is-connected dropping and returning instead of a silent freeze. A replug still
cures it as before, since NO_DEVICE reaches the same path.

Compile-checked against 2.9 after #4761: builds, warning count unchanged
compared with the same tree without the patch (all -Wvolatile from
hal_float_t).

@grandixximo

Copy link
Copy Markdown
Contributor

Thanks for tracking this down. The timeout of 0 in sendDisplayData() is clearly a bug, and master has it too.

This conflicts with #4761, hdiethelm's xhc-whb04b-6 backport to 2.9. That PR already removes these asserts and brings in master's reconnect handling, so once it is merged your first commit isn't needed. Could you wait for #4761, then rebase and keep only the timeout commit? I'll take it to master with the next merge-up.

@hansfbaier

Copy link
Copy Markdown
Author

Agreed on both counts. I checked #4761's head (xhc-whb04b-6-fix-v2_backport-2.9):

  • The libusb asserts are gone and run() uses master's Usb::InitStatus loop, so my first commit — assert removal plus the failed-submit retry — is superseded, and I'll drop it. Our abort reproduction was with the packaged 2.9.10 binary, so that path disappearing in xhc-whb04b-6 fixes backport to 2.9 #4761 is expected.
  • sendDisplayData() there still passes timeout = 0, and the timeout commit applies to that tree unchanged: identical hunk context, git apply --check clean, nothing to rework on rebase.
  • Built xhc-whb04b-6 fixes backport to 2.9 #4761 plus the timeout commit to be sure of the base: compiles with gcc 16.2.1 against the HAL headers in /usr/include/linuxcnc, and the warning count is the same with and without the patch (124, all -Wvolatile from hal_float_t in hal.h).

Marked the PR a draft in the meantime. Once #4761 is merged I will push the branch as a single timeout-only commit on top of the new 2.9 head and flip it back to ready. Taking it up to master with the merge-up sounds good.

Caveat that still stands: the field freeze is not reproduced, so the PR claims only that a stalled display transfer becomes an error the reconnect path can handle instead of an unbounded wait. If the machine catches one, its thread-state dump goes here as a comment.

@hansfbaier
hansfbaier marked this pull request as draft November 14, 2026 08:12
@LinuxCNC LinuxCNC deleted a comment from KentChenY Nov 14, 2026
@grandixximo

Copy link
Copy Markdown
Contributor

@hansfbaier #4761 merged

@hansfbaier
hansfbaier force-pushed the xhc-whb04b-6-libusb-reconnect branch from a7f14d8 to d211a91 Compare November 24, 2026 09:41
@hansfbaier hansfbaier changed the title xhc-whb04b-6: bound the display transfer timeout, reconnect on libusb errors xhc-whb04b-6: bound the display control transfer timeout Nov 24, 2026
@hansfbaier
hansfbaier marked this pull request as ready for review November 24, 2026 09:42
@hansfbaier

Copy link
Copy Markdown
Author

Rebased onto 04d20401 (#4761) and reduced to the timeout commit only: the assert removal and the failed-submit retry arrived with #4761, so they are dropped here. Compile-checked on that base — builds, warning count unchanged versus the same tree without the patch. Marking ready for review; thanks for carrying it up to master.

@grandixximo

Copy link
Copy Markdown
Contributor

The 1 s deadline makes sense to me. I tried to follow what happens after it fires, and I'm stuck on a few questions:

  1. When libusb_control_transfer returns LIBUSB_ERROR_TIMEOUT, sendDisplayData() sets mDoReconnect and returns. Where is mDoReconnect read, and when does that code run?
  2. XhcWhb04b6Component::process() loops while (isRunning() && mUsb.isDeviceOpen()). After the timeout, what would make isDeviceOpen() return false?
  3. Usb::process() calls close() when transferFailed is set. Does anything on the display path set it?
  4. With an idle dial, the IN transfer times out every 750 ms as a normal event. What does the callback do with LIBUSB_TRANSFER_TIMED_OUT, and could a wedged receiver keep that path quiet?

If those answers point the way I think they do, the loop keeps running with a 1 s stall per pass and is-connected stays TRUE. Would setting transferFailed in the r < 0 branch get you to "connection lost, cleaning up" and a reconnect? If you can force a failure, for example by pulling the receiver while the display is updating, the log would show it either way.

@hansfbaier
hansfbaier force-pushed the xhc-whb04b-6-libusb-reconnect branch from d211a91 to 5d0bd58 Compare November 24, 2026 11:28
@hansfbaier

Copy link
Copy Markdown
Author

All four point the same way, and you are right about the outcome: as pushed, the timeouts made the stall finite but nothing recovered. Answers against 04d20401:

  1. mDoReconnect is read in exactly one place, Usb::init() (usb.cc:607), so it only buys the 3 s pause before re-enumerating. init() is called from the outer loop in XhcWhb04b6Component::run() (xhc-whb04b6.cc:438), which is re-entered only after process() returns.
  2. Nothing. isDeviceOpen() is deviceHandle != nullptr (usb.cc:128), and the error branch of sendDisplayData() neither closes the device nor nulls the handle. So process() (xhc-whb04b6.cc:490) kept looping: 1 s in the stalled write, 200 ms in mUsb.process(), is-connected TRUE throughout.
  3. No. transferFailed is set only by the async IN callback (usb.cc:516/528/541/546) and read only in Usb::process() (usb.cc:750), which is the code that closes the device and sets mDoReconnect.
  4. LIBUSB_TRANSFER_TIMED_OUT (usb.cc:523) resubmits and does not set transferFailed, because on an idle dial that timeout is the normal event. So yes: a receiver that stops answering the HID control pipe while its endpoints still NAK normally never reaches a failure path — which matches the symptom, a frozen dial with is-connected TRUE and a clean dmesg.

Setting transferFailed in the r < 0 branch does reach "connection lost, cleaning up" and the reconnect. I could not pull a receiver (the machine is off the network), so I built the component against a stub libusb with a phantom receiver: the IN transfer times out every 750 ms like an idle pendant, and libusb_control_transfer starts returning LIBUSB_ERROR_TIMEOUT after 40 calls.

timeout = 1000 only
  2.58 transmission failed, try to reconnect ...
  2.78 transmission failed, try to reconnect ...
  2.98 transmission failed, try to reconnect ...   (47 in 12 s, no cleanup)

timeout = 1000 + transferFailed = true
  2.58 transmission failed, try to reconnect ...
  3.78 connection lost, cleaning up
  7.78 init  pausing 3s, waiting for device to be gone ....... done
  7.78 init  XHC-WHB04B-6 device found
  7.78 init  claiming interface ... ok
  7.78 init  enabling reception ... ok
  7.78 transmission failed, try to reconnect ...   (receiver still wedged)
  8.98 connection lost, cleaning up

While the receiver stays wedged that cycle repeats every ~4 s, so is-connected drops and comes back instead of freezing silently; a pull still lands in the same path through NO_DEVICE.

Pushed 5d0bd5810a, one commit, both halves together: 1 s deadline, and transferFailed in place of mDoReconnect in that branch. The PR body has the same trace. A hardware log is still outstanding and I will add it here if the sampler catches the freeze.

sendDisplayData() sent each display block with a timeout of 0, which libusb
reads as "wait as long as it takes". That call runs on the thread that pumps
libusb events, so a receiver that stops completing the transfer freezes the
whole component - jog dial included - while whb.pendant.is-connected stays
TRUE until the receiver is unplugged.

Bound the transfer to 1 s and report the failure through transferFailed, the
flag Usb::process() already watches: it closes the device, sets mDoReconnect,
and the component's outer loop re-enumerates.

The second half is needed. The error branch only set mDoReconnect, which is
read in Usb::init(), and init() is reached only after process() returns, which
needs isDeviceOpen() - that is, deviceHandle - to be null. Nothing on the
display path cleared it, so the loop retried the stalled write once per pass
with the connection still reported healthy. A timed-out IN transfer is an idle
tick and never sets transferFailed either, so a receiver that stops answering
the HID control pipe produced no failure at all.

Checked without hardware, against a stub libusb reporting a phantom receiver
that times the IN transfer out every 750 ms like an idle pendant and returns
LIBUSB_ERROR_TIMEOUT from the control transfer after 40 calls: with the
timeout alone the log is 47 "transmission failed, try to reconnect ..." lines
in 12 s and nothing else; with transferFailed it goes to "connection lost,
cleaning up", the 3 s pause, re-enumeration, claiming the interface and
"enabling reception ... ok". Field test on hardware is still outstanding.
@hansfbaier
hansfbaier force-pushed the xhc-whb04b-6-libusb-reconnect branch from 5d0bd58 to 9784eaf Compare November 24, 2026 11:29
@hansfbaier

Copy link
Copy Markdown
Author

Correction to my previous message: 5d0bd5810a carried only the timeout hunk. I edited the file and then amended the message without staging the edit, so the transferFailed line sat in my working tree instead of the commit. The branch is now 9784eafe86 with both hunks (+6/-3):

-            mDoReconnect = true;
+            transferFailed = true;

The PR body describes what is actually in the branch now.

@grandixximo

Copy link
Copy Markdown
Contributor

Could you give it a check on actual hardware?

@hansfbaier

Copy link
Copy Markdown
Author

Hardware log, from the machine this was found on (LinuxCNC 2.9.10, Pi 5, receiver 10ce:eb93). To force the failure I took the receiver off the bus with echo 0 > /sys/bus/usb/devices/1-2.4/authorized, then back with 1, so the failure comes through the real driver stack rather than a stub.

Component log, from the display write that hit the dead device:

transfer error: 5
transmission failed, try to reconnect ...
transmission failed, try to reconnect ...
cleanup: release_interface returned -99

whb.pendant.is-connected across that event:

TRUE at t=0
-> FALSE at t=1.08
-> TRUE at t=5.58     (same pid the whole way, no restart, no replug)

Sampler on the component's main thread, 5 s cadence, over the same window:

wchan=do_sys_poll        fd=0x...d0e0  cn=TRUE
wchan=hrtimer_nanosleep  fd=0x0        cn=FALSE     <- the 3 s init pause
wchan=do_sys_poll        fd=0x...7a50  cn=TRUE      <- new handle, re-enumerated

Three forced disconnects, three recoveries, 4.5-5 s each. Before this change the same event left the component alive with is-connected TRUE and a dead dial until the receiver was unplugged.

Two limits on what this shows, so it is not read as more than it is: the build on the machine is the v2.9.10 tree, whose loop keys on getDoReconnect(), so this run proves the timeout plus the reconnect wiring on real hardware but not the transferFailed line, which is the 2.9 shape after #4761; that line is covered by the stub-libusb run above. And a deauthorized port fails transfers immediately, so the 1 s deadline itself was not the trigger here. The case that needs a receiver which accepts the transfer and then never completes it is still only reachable when one wedges on its own, which is what the sampler on this machine waits for.

sha256 of /usr/bin/xhc-whb04b-6 matches the build output from the patched source, so the running binary is the one with 1000 in that call.

@grandixximo
grandixximo merged commit 706b4a5 into LinuxCNC:2.9 Nov 24, 2026
14 checks passed
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.

2 participants