Hello,
I'm submitting a patch that fixes a critical issue where applications using rtlsdr_read_async() hang indefinitely when USB transfer errors occur.
## Background
I encountered this issue while running long-term monitoring with rtl_fm for FLEX paging signals. When the RTL-SDR dongle would shift slightly in the USB socket (causing USB transfer errors), rtl_fm would hang indefinitely instead of exiting cleanly.
## Root Cause
The problem occurs in two parts:
1. The _libusb_callback() function calls rtlsdr_cancel_async() from within the libusb event handler callback. This creates a race condition where dev->async_cancel is modified while libusb_handle_events_timeout_completed() is actively using it as a parameter, leading to unpredictable behavior.
2. Even when the dev->dev_lost flag is set, the async read loop doesn't properly exit, and applications like rtl_fm have no way to detect that the device is lost.
## Solution
The attached patch: - Removes the problematic rtlsdr_cancel_async() call from the callback - Adds detection of dev->dev_lost in the main event loop to properly transition to RTLSDR_CANCELING state - Makes rtlsdr_read_async() return -1 when the device is lost - Updates rtl_fm to check the return value and terminate gracefully
## Testing
Tested with RTL2832U dongle (FC0012 tuner) by deliberately triggering USB errors through physical movement. The applications now terminate cleanly with an appropriate error message instead of hanging indefinitely. Normal operation (Ctrl+C termination) continues to work correctly.
This fix makes rtl-sdr more robust for long-running monitoring applications and deployment scenarios where physical interference might occur (e.g., Raspberry Pi installations).
Please review and consider merging.
Best regards, Ramon Smits