Skip to content

Hotplug docs: clarify thread-safety, safe-to-call list, device->next contract#790

Open
Youw wants to merge 1 commit intoconnection-callbackfrom
connection-doc-v2
Open

Hotplug docs: clarify thread-safety, safe-to-call list, device->next contract#790
Youw wants to merge 1 commit intoconnection-callbackfrom
connection-doc-v2

Conversation

@Youw
Copy link
Copy Markdown
Member

@Youw Youw commented Apr 24, 2026

Expands the hotplug API documentation to spell out the thread-safety contract and adjacent behaviour in line with the Multi-threading Notes wiki. Supersedes #784.

What changes

Doc-only changes to hidapi/hidapi.h:

  • hid_hotplug_callback_fn — explicit "Execution context", "What the callback may call", "device parameter", and "Return value" sections.
  • hid_hotplug_register_callback — new "Thread safety" section documenting that the hotplug register/deregister pair is thread-safe (the one deliberate exception to HIDAPI's general "not thread-safe" rule) and describing the lifetime of the internal hotplug thread.
  • hid_hotplug_deregister_callback — strengthened to document full thread-safety and the already-deregistered no-op behaviour.

Why not #784's wording

#784's patch reads as a blanket "NOT safe to call hid_open/hid_open_path or any other global/non-device functions from within this callback." As @k1-801 pointed out in the review of that PR, this is overly strict: the issue is concurrency between the hotplug callback thread and any other thread that calls init/enumerate/open/close, not the callback itself. The new text follows the wiki's "may be used under conditions Y" style and enumerates the calls that are always safe from inside the callback.

Implied behaviour changes — tracked as follow-up issues

This PR documents the target semantics. Two minor implementation changes are required to bring the code in line with the docs; they are intentionally out of scope here (per maintainer decision — connection-callback is still WIP and a temporary doc/impl gap is acceptable):

  1. device->next == NULL inside the callback — Hotplug: device->next must be NULL inside callback #792
  2. HID_API_HOTPLUG_ENUMERATE honours the callback return value — Hotplug: HID_API_HOTPLUG_ENUMERATE should honour callback return value #793

Also captured as follow-ups — existing implementation bugs that predate this doc work:

  1. macOS self-join deadlock in hid_internal_hotplug_cleanupHotplug (macOS): self-join deadlock in hid_internal_hotplug_cleanup #794
  2. hid_exit from within a hotplug callback should fail fast rather than deadlock — Hotplug: hid_exit() from inside callback should fail fast, not deadlock #795

Not changing

  • No new public functions, enums, or flags.
  • No ABI or semantic break for correctly-written existing code.

Drafted with Claude Code.

Expand the doc comments on hid_hotplug_callback_fn,
hid_hotplug_register_callback, and hid_hotplug_deregister_callback
to make the hotplug API's thread-safety contract explicit:

- The hotplug API is thread-safe; this is a documented exception
  to HIDAPI's general "not thread-safe" rule.
- The callback runs on an internal HIDAPI thread that is distinct
  from the application's threads, and holds an internal mutex for
  the duration of each call.
- Enumerate safe-to-call functions from within the callback
  (hid_hotplug_register/deregister_callback, hid_error(dev)) and
  explain why others require application-level serialisation,
  matching the wording style in the Multi-threading Notes wiki.
- Spell out the lifetime of the device pointer, the string
  ownership rules, and the requirement that device->next is
  always NULL inside the callback.
- Document hid_exit() as UB from within the callback (self-join).
- Define the return-value semantics consistently for both live
  events and the synthetic "arrived" events delivered during
  HID_API_HOTPLUG_ENUMERATE.

Supersedes PR #784. Behaviour changes implied by this doc
(device->next=NULL, ENUMERATE honoring return value) are tracked
as follow-up issues.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation hotplug Related to hotplug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants