From f2ee0195defa3a98c86d891b03939c611605c3d7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 28 Sep 2025 18:53:31 -0500 Subject: [PATCH 1/2] fix retry --- esphome/components/usb_host/usb_host_client.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/esphome/components/usb_host/usb_host_client.cpp b/esphome/components/usb_host/usb_host_client.cpp index a5dd1457ba..a83d7d83d2 100644 --- a/esphome/components/usb_host/usb_host_client.cpp +++ b/esphome/components/usb_host/usb_host_client.cpp @@ -382,9 +382,10 @@ TransferRequest *USBClient::get_trq_() { trq->transfer->device_handle = this->device_handle_; return trq; } - // Another thread claimed this slot, retry with updated mask - // Don't increment i - retry the same slot with the updated mask - mask = expected; + // CAS failed - another thread modified the bitmask + // Restart search from the beginning with the updated mask + mask = this->trq_in_use_.load(std::memory_order_relaxed); + i = 0; } ESP_LOGE(TAG, "All %d transfer slots in use", MAX_REQUESTS); From 5334ddd9f05869e9d7c20380dd069bb2287a3685 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 28 Sep 2025 18:54:14 -0500 Subject: [PATCH 2/2] Update esphome/components/usb_host/usb_host_client.cpp Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- esphome/components/usb_host/usb_host_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/usb_host/usb_host_client.cpp b/esphome/components/usb_host/usb_host_client.cpp index a83d7d83d2..d18b90b790 100644 --- a/esphome/components/usb_host/usb_host_client.cpp +++ b/esphome/components/usb_host/usb_host_client.cpp @@ -543,7 +543,7 @@ void USBClient::release_trq(TransferRequest *trq) { // Atomically clear bit i to mark slot as available // fetch_and with inverted bitmask clears the bit atomically uint16_t bit = 1U << index; - this->trq_in_use_.fetch_and(~bit, std::memory_order_release); + this->trq_in_use_.fetch_and(static_cast(~bit), std::memory_order_release); } } // namespace usb_host