Fix signal handling and add atomic operations for thread safety#800
Merged
Youw merged 1 commit intoread-interruptfrom May 5, 2026
Merged
Fix signal handling and add atomic operations for thread safety#800Youw merged 1 commit intoread-interruptfrom
Youw merged 1 commit intoread-interruptfrom
Conversation
- windows/hid.c: fix typos in hid_is_read_interrupted comment. - linux/hid.c: skip close() in hid_close() when device_handle < 0 to avoid clobbering errno on the open-failure path. - linux/hid.c: preserve eventfd() errno across free() in new_hid_device(); report the real errno from hid_open_path() instead of overwriting with ENOMEM. - linux/hid.c, netbsd/hid.c: replace `volatile int interrupted` with __atomic_load_n/__atomic_store_n (acquire/release) so the cross-thread guarantee actually holds. The pipe/eventfd handles the wakeup; the atomic covers hid_is_read_interrupted and the early-out check. - hid_read_test/main.cpp: signal handler no longer calls hid_read_interrupt() (not async-signal-safe on libusb/mac). Handler now sets an atomic flag; main thread, woken by EINTR from cin.get(), calls hid_read_interrupt(). Use sigaction without SA_RESTART on POSIX. - hid_read_test/CMakeLists.txt: lower cmake_minimum_required to 3.1.3 to match the rest of the repo; switch from target_compile_features(cxx_std_11) (CMake 3.8+) to set_target_properties(CXX_STANDARD 11) (CMake 3.1+). https://claude.ai/code/session_01JB9WGTp5xipxBFMgcJa8XT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR improves thread safety and signal handling across the HIDAPI library by replacing volatile variables with proper atomic operations and refactoring signal handling to be async-signal-safe.
Key Changes
Signal Handling Improvements
hid_read_interrupt()calls in signal handler with atomic flag settingsigaction()on non-Windows platforms withoutSA_RESTARTto allowcin.get()to return on signalstd::signal()for Windows compatibilityAtomic Operations
volatile int interruptedwith proper atomic operations:dev->interrupted = 1to__atomic_store_n(&dev->interrupted, 1, __ATOMIC_RELEASE)if (dev->interrupted)toif (__atomic_load_n(&dev->interrupted, __ATOMIC_ACQUIRE))__atomic_load_n(&dev->interrupted, __ATOMIC_ACQUIRE)Bug Fixes
new_hid_device()by saving errno beforefree()callhid_open_path()to preserve and report the actual error fromnew_hid_device()hid_close()to only close device_handle if it's valid (>= 0)Build System
target_compile_features()withset_target_properties()for more explicit C++ standard configuration (CXX_STANDARD, CXX_STANDARD_REQUIRED, CXX_EXTENSIONS)Implementation Details
The atomic operations use acquire/release semantics to ensure proper synchronization between the signal handler thread and the main reading thread without requiring full sequential consistency, which improves performance while maintaining correctness.
https://claude.ai/code/session_01JB9WGTp5xipxBFMgcJa8XT