← index #18029PR #15634
Off-topic · high · value 1.584
QUERY · ISSUE

ESP32 CDC print("0123" * 64) causes missing beginning of the string

openby liusidaopened 2025-09-03updated 2026-02-06
bugport-esp32needs-info

Port, board and/or hardware

ESP32-S3-Box-3

MicroPython version

Tested on v1.24.0 and v1.25.0.
After built and CDC to the REPL:

>>> print("0123"*64)
23012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123

missing the beginning part of the string. The string happens to be 255 long. So if we print more, there will be missing more in the beginning.

also in C, functions like mp_printf are also affected. If the string from multiple mp_printf exceeds 255, the beginning would sometimes be missing as well.

Switching to JTAG solves the problem, also unix port doesn't have this problem.

Reproduction

  1. git clone from repo, pick a recent version that compiles.
  2. cd ports/esp32/ ; make BOARD=ESP32_GENERIC_S3 VARIANT=SPIRAM_OCT
  3. after flash, connect to device using mpremote connect COM26
  4. with >>> prompt, run print("0123" * 64)

Expected behaviour

0123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123

Observed behaviour

23012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123

Additional Information

No, I've provided everything above.

Code of Conduct

Yes, I agree

CANDIDATE · PULL REQUEST

esp32/mphalport: Print debug strings even before the GIL is ready.

mergedby agattiopened 2024-08-10updated 2024-08-16
port-esp32

Summary

If verbose debugging is enabled there is some stdout output happening before the GIL is ready, and the code assumed that no string printing occurred before the interpreter was fully initialised. Printing long strings would operate without holding the GIL, which would crash if string output would happen too early in the startup process.

This commit addresses that issue, making sure verbose debugging output will work even before the interpreter is fully initialised (as if it is not yet ready there's no GIL to take care of).

Also, the threshold that would indicate whether a string is "long" (and thus requiring a GIL release/lock operation) or not was hardcoded to 20 bytes. This commit makes that configurable, maintaining 20 bytes as a default.

This fixes bug #15408.

Testing

Testing was done with an ESP32C3 board, with both MICROPY_DEBUG_PRINTERS and MICROPY_DEBUG_VERBOSE enabled.

Trade-offs and Alternatives

This patch adds around 32 bytes of code, but only when MICROPY_DEBUG_PRINTERS, MICROPY_DEBUG_VERBOSE, and MICROPY_PY_THREAD_GIL are enabled. Given that the first two flags are off by default, and enabling them means printing extra debugging information, a minor code size increase and even minor slowdown when printing strings is not only tolerated but expected.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied