LudovicRousseau / PCSC

pcsc-lite: PC/SC implementation
https://pcsclite.apdu.fr/
Other
258 stars 107 forks source link

Fix shutdown race with hotplug_libusb rescan pipe #173

Closed emaxx-google closed 5 months ago

emaxx-google commented 5 months ago

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.

LudovicRousseau commented 5 months ago

Please rebase

emaxx-google commented 5 months ago

Please rebase

done

LudovicRousseau commented 5 months ago

Merged. Thanks