airspy / airspyhf

Code repository for AirspyHF+
BSD 3-Clause "New" or "Revised" License
147 stars 40 forks source link

fix hang issue when device is disconnected #33

Closed jvde-github closed 2 years ago

jvde-github commented 2 years ago

Hi, I experienced that airspyhf_rx and other sofware is hanging when the airspyhf was disconnected during streaming. After some investigation I realized that kill_io_threads was not terminating the consumer_thread and then a later pthread_cond_signal call was hanging the system (undefined behavior because a thread still waiting for the signal).

I believe the issue might be resolved if the variable streaming is true if and only if the threads are running and stop_requested is used to indicate a request to stop (e.g. because there is an issue with the device).

For your kind consideration. Jasper

jvde-github commented 2 years ago

Although the first commit fixed the hanging issue, I still noticed instability on Android and a segmentation fault on Windows when disconnecting the device during streaming (not Linux). I added some counting of open transfers to properly ensure all transfer callbacks have been completed before the libusb close. The changes are similar to what has been suggested here:

https://lists.osmocom.org/hyperkitty/list/osmocom-sdr@lists.osmocom.org/thread/MN2PVFRDQUBUO6SAPLFK7EE7G6LEQJ5S/ https://lists.osmocom.org/hyperkitty/list/osmocom-sdr@lists.osmocom.org/thread/WPL5MZIX7CGVDF2NECPSTZYDLACAEXRI/ https://github.com/greatscottgadgets/hackrf/pull/1071 https://github.com/greatscottgadgets/hackrf/pull/1029

Both commits together seem to resolve the hang/crash on windows and android.

touil commented 2 years ago

Thanks! Too many moving parts in libusb. I hope this will keep our code base operational for a while until the next breaking change.