A bit of documentation details on callback API #784
No reviewers
Labels
No labels
3rdparty
Android
API
Author does not respond
bsd
bug
build system/CI
ci-virtual-device
CMake
Contributions Welcome
Core
dependencies
device-specific
documentation
don't_merge
duplicate
enhancement
Example
Haiku
hidraw
hotplug
info required
invalid
libusb
macOS
Misc
Not an issue
question
Release
Test
Test GUI
Windows
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-libusb/hidapi!784
Loading…
Reference in a new issue
No description provided.
Delete branch "connection-doc"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@ -306,0 +306,4 @@It is NOT safe to call hid_open/hid_open_path or any other global/non-devicefunctions from within this callback. If you need to openor close a device in response to a hotplug event, queue thedevice path and handle it on your own application thread.I personally don't see any reason why opening or closing a device would be unsafe in a callback. There aren't any intersections between the hotplug subsystem and the code responsible for opening the devices, so there should be no race conditions.
@ -306,0 +306,4 @@It is NOT safe to call hid_open/hid_open_path or any other global/non-devicefunctions from within this callback. If you need to openor close a device in response to a hotplug event, queue thedevice path and handle it on your own application thread.What can I say...
For starters, check: https://github.com/libusb/hidapi/wiki/Multi%E2%80%90threading-Notes
HIDAPI is not, and never been a thread-safe API. New
register/deregisterfunctions are not thread-safe as well.It may be safe, only in case of all calls to
hid_open_path(and others), including the ones called from within a callback function - are all correctly serialized. But for that:@ -306,0 +306,4 @@It is NOT safe to call hid_open/hid_open_path or any other global/non-devicefunctions from within this callback. If you need to openor close a device in response to a hotplug event, queue thedevice path and handle it on your own application thread.So, so long as no other thread opens any device, there is nothing wrong with opening a device from a callback, right?
If so, this limitation is not any different from restricting using this call from any other 2 threads, and should be then worded accordingly :)
As in "It is NOT safe to call hid_open/hid_open_path from within a callback IF any other thread may be calling those functions at the same time (concurrently). It may be safe to do so if it is known that no other thread will be calling hid_open/hid_open_path, otherwise opening the device should be queued, and the device path should be copied by the application to be used and opened later in the queue."
@ -306,0 +306,4 @@It is NOT safe to call hid_open/hid_open_path or any other global/non-devicefunctions from within this callback. If you need to openor close a device in response to a hotplug event, queue thedevice path and handle it on your own application thread.There are well-known approaches to how the wording for multithreading safety should look like.
I'll get back to this later, one I re-check that it is really safe, and in which cases.
Apparently, not really, here is one unresolved issue: https://github.com/libusb/hidapi/pull/674#discussion_r3015415035
Opened #790 to expand this doc patch — keeps the intent (document the callback contract) but reworks the wording to match the Multi-threading Notes wiki style, enumerates what is safe to call, fixes the
device->next/ return-value wording, and spins off implementation follow-ups into #792, #793, #794, #795. Suggest closing this one in favour of #790.Drafted with Claude Code.
I already asked Claude to mark all messages as "generated" when thsose are posted on my behalf.
Pull request closed