Fix shutdown race with hotplug_libusb rescan pipe #173
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.
Fix the possibility that the hotplug_libusb rescan thread remains read()'ing from the rescan pipe throughout the daemon shutdown process.
This is possible because the |rescan_pipe| is created with some delay after the hotplug mechanism is initialized:
HPEstablishUSBNotifications() first notifies about the initialization via writing to |pipefd|, and only then creates |rescan_pipe|. So if HPStopHotPluggables() is called in between, it'll end up doing nothing, letting the hotplug thread sit infinitely long on the read() call meanwhile the main daemon thread is shutting down.
The proposed fix is to create |rescan_pipe| in advance, so that the shutdown logic is guaranteed to be able to write a byte to it, notifying the background thread about the shutdown regardless of when it happens.