Skip to content

libusb: fail hid_open_path() if the read thread can't be started - #843

Merged
Youw merged 3 commits into
masterfrom
fix-libusb-thread-create
Nov 8, 2026
Merged

Youw merged 3 commits into
masterfrom
fix-libusb-thread-create

Conversation

@Youw

@Youw Youw commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

On the libusb backend, failed read-thread creation left hid_open_path() waiting forever at the startup barrier (Coverity CID 1706123). The thread wrapper now returns the creation status, and failure follows the existing cleanup path: free the input ring, release the interface, restore the kernel driver and return NULL with an error.

Custom HIDAPI_THREAD_MODEL_INCLUDE implementations must return int from hidapi_thread_create() (0 on success, nonzero on failure); see #829.

Validation: the existing Linux ASan build and test suite pass; four virtual-device tests skip because the devices are unavailable. Codex reviewed the completed production change.

Fixes #842

Related to #825, #829 and #841.

Generated by Claude Code (Claude Opus 5.5); reviewed by Codex (GPT-6).

Assisted-by: claude-code:claude-opus-5-5
Assisted-by: Codex:GPT-6

Youw added 2 commits November 8, 2026 21:29
hidapi_thread_create() discarded the pthread_create() result, so
hidapi_initialize_device() went on to wait on the two-party startup
barrier even when no read thread existed, and hid_open_path() blocked
forever (e.g. on EAGAIN under thread/resource limits).

hidapi_thread_create() now returns the creation status (0 on success).
On failure, hidapi_initialize_device() skips the startup barrier and the
join, and goes through the existing error path that destroys the input
ring, releases the interface and reattaches the kernel driver;
hid_open_path() then reports "failed to initialize device".

A custom thread model provided via HIDAPI_THREAD_MODEL_INCLUDE must now
declare hidapi_thread_create() as returning int (0 on success,
non-zero on failure).

Add LibUsbReadThreadStartupTest, which compiles libusb/hid.c with
pthread and device-level libusb calls interposed to cover both the
injected creation failure and normal startup/close, without hardware.

Fixes: #842
Related: #825, #829
Coverity CID: 1706123

Assisted-by: claude-code:claude-opus-5-5
On Android before API 24, hidapi_thread_pthread.h provides its own
pthread_barrier_t and pthread_barrier_wait(), which the test's
interposition of pthread_barrier_wait() collides with. Add the test only
where a native pthread_barrier_wait() exists (checked with the thread
library linked), and make the test say so explicitly if it is built
elsewhere.

The test also now always uses the bundled pthread thread model, even if
a custom HIDAPI_THREAD_MODEL_INCLUDE is configured for the library.

Reword the hidapi_thread_create() comment: pthread_create() doesn't
guarantee the thread handle is left unchanged on failure.

Assisted-by: claude-code:claude-opus-5-5
Remove the regression test and restore the test CMake file to the base
version, leaving only the production thread-creation failure handling.

Assisted-by: Codex:GPT-6
@Youw
Youw merged commit 5e7b6a0 into master Nov 8, 2026
28 checks passed
@Youw
Youw deleted the fix-libusb-thread-create branch November 8, 2026 22:28
Youw added a commit that referenced this pull request Nov 8, 2026
Give the macOS and libusb private interrupt flags distinct member names
so merged static analysis can distinguish their locking requirements.
Keep layout, API and synchronization unchanged.

Related to #841 and #843.

Assisted-by: Codex:GPT-6
Assisted-by: claude-code:claude-fable-5-1
@mcuee mcuee added the libusb Related to libusb backend label Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libusb Related to libusb backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libusb: avoid hanging when read-thread creation fails

2 participants