WiimoteReal: Fix Dolphin shutdown crash #9948
Merged
+2
−1
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.
Fix a race condition in the WiimoteScanner thread which can cause a crash on Dolphin shutdown. This crash occurs when the following happens:
This fix simply defers clearing the m_scan_thread_running flag until after the main thread has finished iterating over the backends.
I considered locking the m_backends_mutex from the main thread instead, but this would create a new problem. The reason the main thread iterates over the backends is to call RequestStopSearching(), which does nothing on most platforms but sets m_stop_scanning on macOS. This variable is used as a flag to break out of the scan loop in WiimoteScannerDarwin::FindWiimotes, and if it's not set I believe the function never returns (unless it finds a new device). If the scan thread locked the mutex first it would clear m_backends and the main thread wouldn't have any backends to call RequestStopSearching on.
The only other reason we would need to lock the mutex in the main thread would be if the call to RequestStopSearching had a race condition, but it doesn't. Once m_stop_scanning is set to true it stays set, so even if the loop goes into another iteration FindWiimotes will stop looping immediately, allowing the scan thread to exit its loop shortly after the main loop clears m_scan_thread_running.