From ca901a8eacf951b79098343a60c152215fed8833 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 7 Oct 2025 16:16:47 -0300 Subject: [PATCH 1/3] feat(tinyusb_cdc): write timeout Added a timeout check to prevent locking during CDC writes. --- cores/esp32/USBCDC.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cores/esp32/USBCDC.cpp b/cores/esp32/USBCDC.cpp index c7bb4582d4f..f024087681e 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) || millis() > writeTimeout) { + log_e("USB is disconnected or CDC writing has timed out."); size = so_far; break; } From 743b6e929117b3404aae5c4f03713b6e09b0be4f Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Tue, 7 Oct 2025 17:26:46 -0300 Subject: [PATCH 2/3] fix(usb_cdc): better timeout calculation Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- cores/esp32/USBCDC.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/USBCDC.cpp b/cores/esp32/USBCDC.cpp index f024087681e..43e1f2767a2 100644 --- a/cores/esp32/USBCDC.cpp +++ b/cores/esp32/USBCDC.cpp @@ -408,7 +408,7 @@ size_t USBCDC::write(const uint8_t *buffer, size_t size) { // 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) || millis() > writeTimeout) { + 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; From 8833edb951ed09c9a8ea8f5841d371c80ffe8267 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Wed, 8 Oct 2025 08:13:01 +0000 Subject: [PATCH 3/3] ci(pre-commit): Apply automatic fixes --- cores/esp32/USBCDC.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/USBCDC.cpp b/cores/esp32/USBCDC.cpp index 43e1f2767a2..4eea8845b35 100644 --- a/cores/esp32/USBCDC.cpp +++ b/cores/esp32/USBCDC.cpp @@ -405,7 +405,7 @@ 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 + // 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) || (int32_t)(millis() - writeTimeout) >= 0) {