Hotplug: hid_exit() from inside callback should fail fast, not deadlock #795

Open
opened 2026-04-24 09:18:50 -05:00 by Youw · 0 comments
Youw commented 2026-04-24 09:18:50 -05:00 (Migrated from github.com)

Summary

The hotplug callback documentation added in #790 states:

Calling hid_exit() from within the callback has undefined behavior: hid_exit() joins the hotplug thread, which would be joining itself.

Rather than rely entirely on documentation, the implementation should detect this case and fail fast (e.g. log an error via the standard error-string path and return without attempting the join) so misuse surfaces as observable errors rather than silent hangs.

Proposed fix

At the start of `hid_exit` (and/or the shared cleanup entrypoint), detect if the current thread is the hotplug thread using `pthread_equal(pthread_self(), hid_hotplug_context.thread)` (or the Windows / IOHID equivalent) and:

  • Set a registered error string making the misuse diagnosable.
  • Return early without attempting the join.

This is a defence-in-depth measure layered on top of the documented UB. It overlaps structurally with #794 (macOS self-join in the internal cleanup path); fixing both can share the same "is-self-thread" helper.

Scope

  • All four backends: `libusb/hid.c`, `linux/hid.c`, `mac/hid.c`, `windows/hid.c`.
  • Documentation: #790
  • macOS-specific instance of the same class of bug: #794
  • Original RFC: #674

Drafted with Claude Code.

## Summary The hotplug callback documentation added in #790 states: > Calling hid_exit() from within the callback has undefined behavior: hid_exit() joins the hotplug thread, which would be joining itself. Rather than rely entirely on documentation, the implementation should detect this case and fail fast (e.g. log an error via the standard error-string path and return without attempting the join) so misuse surfaces as observable errors rather than silent hangs. ## Proposed fix At the start of \`hid_exit\` (and/or the shared cleanup entrypoint), detect if the current thread is the hotplug thread using \`pthread_equal(pthread_self(), hid_hotplug_context.thread)\` (or the Windows / IOHID equivalent) and: - Set a registered error string making the misuse diagnosable. - Return early without attempting the join. This is a defence-in-depth measure layered on top of the documented UB. It overlaps structurally with #794 (macOS self-join in the internal cleanup path); fixing both can share the same \"is-self-thread\" helper. ## Scope - All four backends: \`libusb/hid.c\`, \`linux/hid.c\`, \`mac/hid.c\`, \`windows/hid.c\`. ## Related - Documentation: #790 - macOS-specific instance of the same class of bug: #794 - Original RFC: #674 --- _Drafted with Claude Code._
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-libusb/hidapi#795
No description provided.