ESP32 CDC print("0123" * 64) causes missing beginning of the string
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
- git clone from repo, pick a recent version that compiles.
- cd ports/esp32/ ; make BOARD=ESP32_GENERIC_S3 VARIANT=SPIRAM_OCT
- after flash, connect to device using
mpremote connect COM26 - with
>>>prompt, runprint("0123" * 64)
Expected behaviour
0123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123
Observed behaviour
23012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123012301230123
Additional Information
No, I've provided everything above.
Code of Conduct
Yes, I agree
esp32/mphalport: Print debug strings even before the GIL is ready.
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.