When USB transfer errors occur (e.g., dongle movement in socket), applications using rtlsdr_read_async() would hang indefinitely instead of terminating gracefully.
The issue had two parts:
1. _libusb_callback() was calling rtlsdr_cancel_async() from within the libusb event handler, creating a race condition by modifying dev->async_cancel while libusb_handle_events_timeout_completed() was actively using it as a parameter.
2. Even when dev->dev_lost was set, the async read loop would not properly exit, and applications like rtl_fm had no way to detect the device loss.
This patch: - Removes the problematic rtlsdr_cancel_async() call from the callback - Adds proper dev->dev_lost detection in the main event loop to transition to RTLSDR_CANCELING state - Returns -1 from rtlsdr_read_async() when device is lost - Updates rtl_fm to check the return value and terminate gracefully with an error message
Tested with RTL2832U/FC0012 dongle by deliberately triggering USB errors (dongle movement). Applications now terminate cleanly instead of hanging indefinitely.
Signed-off-by: Ramon Smits <ramon.smits@gmail.com>