Skip to content
This repository was archived by the owner on Dec 1, 2026. It is now read-only.

fix(log): don't block loop() on USB CDC when the host isn't reading - #189

Closed
LordMike wants to merge 1 commit into
OpenDisplay:mainfrom
LordMike:fix/esp32-usb-cdc-log-stall
Closed

LordMike wants to merge 1 commit into
OpenDisplay:mainfrom
LordMike:fix/esp32-usb-cdc-log-stall

Conversation

@LordMike

Copy link
Copy Markdown
Contributor

Closes #188

Problem

On ESP32 builds that log over the native USB serial port, HWCDC::write() retries a full TX ring up to 20 times at tx_timeout_ms (100 ms by default). With a USB host attached but no program reading the port, the 256-byte ring stays full and every write waits up to 2 s. od_emit() issues two writes per record (text, then "\r\n"), so each log line stalls loop() for about 4 s. Details and measurements are in #188.

Change

One call after Serial.begin() in setup():

#if defined(TARGET_ESP32) && ARDUINO_USB_MODE && ARDUINO_USB_CDC_ON_BOOT
Serial.setTxTimeoutMs(0);
#endif

With a zero timeout, a write to a full ring returns immediately and the bytes are dropped instead of waited for. No other logging behaviour changes.

Guard

The call is compiled only when Serial is the chip's native USB serial (HWCDC), i.e. TARGET_ESP32 with both ARDUINO_USB_MODE and ARDUINO_USB_CDC_ON_BOOT set:

  • ESP32-S3 / C3 / C6 USB-logging envs: applied.
  • esp32-s3-N32R8-extuart (USB serial off, logs over UART): not compiled. It also never reaches this Serial.begin() branch because OPENDISPLAY_LOG_UART is defined.
  • esp32-s3-N16R8-extuart: logs go to the UART, so this code path is not taken.
  • nRF52840: not compiled; its log path is already non-blocking (fix(nrf): stop a stalled USB host and stuck transfers from wedging loop() #133).

Trade-off

Log output is now dropped rather than delayed whenever the ring is full. With no reader that was already the outcome, just 2 s later per write. With a reader attached, a burst that outruns the host draining 256 bytes can lose or truncate lines. A small non-zero timeout (a few ms) would reduce that at the cost of a short stall; happy to change it if preferred.

Testing

On a Seeed EE03 (ESP32-S3) + ED103TC2, esp32-s3-N16R8, plugged into a PC with the COM port not opened, py-opendisplay over BLE (PIPE_WRITE):

Phase Before After
0x80 START → ACK 14.3 s 2.4 s
0x82 END → ACK, partial transfer incl. refresh 23.8 s 4.1 s

A full-frame push from Home Assistant to the same device took 19.7 s after the change.

Not covered: the binary that was measured carried this line on top of an unrelated local change to the partial-write path, not this exact commit. Only esp32-s3-N16R8 was built locally; the C3 / C6 envs were not. Log completeness under bursts with a reader attached was not stress-tested.

🤖 Generated with Claude Code

HWCDC::write() retries a full TX ring up to 20 times at tx_timeout_ms
(100 ms by default), so with a USB host attached but no program reading
the port every write waits up to 2 s. od_emit() issues two writes per
record, which stalls loop() about 4 s per log line and adds roughly 32 s
to a BLE image upload.

Set the HWCDC TX timeout to 0 so a write to a full ring returns at once
and the bytes are dropped instead of waited for. Guarded to ESP32 builds
where Serial is the native USB serial (ARDUINO_USB_MODE and
ARDUINO_USB_CDC_ON_BOOT); nRF and the UART-only log build are unchanged.

Closes OpenDisplay#188

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@jonasniesner

Copy link
Copy Markdown
Member

This was fixed in the unified firmware, I will close it here now for housekeeping reasons

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ESP32 USB-CDC logging stalls BLE image transfers ~4 s per log line when a USB host is attached but not reading (follow-up to #162)

2 participants