diff --git a/cores/esp32/USBCDC.cpp b/cores/esp32/USBCDC.cpp index c7bb4582d4f..4eea8845b35 100644 --- a/cores/esp32/USBCDC.cpp +++ b/cores/esp32/USBCDC.cpp @@ -405,8 +405,11 @@ size_t USBCDC::write(const uint8_t *buffer, size_t size) { return 0; } size_t to_send = size, so_far = 0; + // writeTimeout will prevent that TinyUSB failure locks the while(to_send) loop + uint32_t writeTimeout = millis() + tx_timeout_ms; while (to_send) { - if (!tud_cdc_n_connected(itf)) { + if (!tud_cdc_n_connected(itf) || (int32_t)(millis() - writeTimeout) >= 0) { + log_e("USB is disconnected or CDC writing has timed out."); size = so_far; break; }