Fix data race in GamepadManager::updateAll() with hotplug thread#1
Merged
Conversation
…ect crash GamepadManagerImpl::updateAll() iterated gamepads_ and dereferenced each device every frame without holding mutex_, while the hotplug thread could simultaneously destroy a device (gamepads_[i].reset()) in check_for_disconnected_devices(). That data race / use-after-free crashed the host whenever a controller dropped mid-frame. Take the same timed_mutex every other accessor uses. The hotplug thread already uses try_lock_for and skips a cycle when it can't acquire the lock, so this cannot deadlock game launch. Also dropped the empty dead if-block around updateState().
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
Fixed a critical data race condition in
GamepadManager::updateAll()that could cause use-after-free crashes when gamepads disconnect during frame updates.Key Changes
updateAll()to synchronize access to thegamepads_collection with the hotplug threadcheck_for_disconnected_devices()can destroy gamepad devices while the polling thread iterates and dereferences them, causing a race conditionstd::lock_guard<std::timed_mutex>to protect the gamepad iterationupdateState()Implementation Details
try_lock_for()and gracefully skips cycles when the lock is unavailable, preventing potential deadlocks during game launchhttps://claude.ai/code/session_01Rht2wE4RuR9k63DGF1eUKW