tinyusb
tinyusb copied to clipboard
EHCI race condition on device disconnection
Operating System
Windows 11
Board
Teensy4.1 (EHCI)
Firmware
When unplugging a device on my Teensy4 board running the host EHCI backend I reliably hit this assert, particularing when using a USB Hub:
https://github.com/hathach/tinyusb/blob/585f37a7977c3ff7e00b1e2235eb0712b12c90d6/src/host/usbh.c#L496
I believe this is due to a race condition during device removal. Consider the following sequence:
- Device is unplugged - TinyUSB registers a detach event in IRQ. 👍
- Prior to tusb processing the removal (race), In flight transfers will fail with STALL condition - TinyUSB registers a XFER_COMPLETE with STALL condition. 👍
- Eventually TinyUSB task loop will process the detach event and remove the associated driver. 👍
- TinyUSB will then process the XFER_COMPLETE against a now cleared device and will hit the above assert. 👎
Im not sure if this is present in other backends.
I think the fix would be to
- Not assert on this error assuming this only occurs if the device is disconnected
- Prior to creating the detach event, cancel infight transfers. This is probably the better solution.
Happy to raise a PR for this when I dig into it a bit more
What happened ?
How to reproduce ?
- Run an example with EHCI backend
- Connect device that has a running transfer - such as USB HUB or HID device.
- Remove device
- Can hit assert https://github.com/hathach/tinyusb/blob/585f37a7977c3ff7e00b1e2235eb0712b12c90d6/src/host/usbh.c#L496 if the highlighted race conditions are met.
Debug Log as txt file
No response
Screenshots
No response