Repository navigation
Description
Uart::write() queues the data, then starts a HAL transfer if none is ongoing. The two steps are not atomic
(Serial.cpp L641-L651):
// Data are copied to buffer, move head pointer accordingly
_serial.tx_head = (_serial.tx_head + size) % SERIAL_TX_BUFFER_SIZE;
// Transfer data with HAL only is there is no TX data transfer ongoing
// otherwise, data transfer will be done asynchronously from callback
if (!serial_tx_active(&_serial)) {
_serial.tx_size = size_intermediate;
uart_attach_tx_callback(&_serial, _tx_complete_irq, size_intermediate);
}If I read the code correctly, when the main loop is preempted between the tx_head update and the
serial_tx_active() check for longer than the new data takes to send:
- the TX complete interrupt (
_tx_complete_irq(),
L372-L391)
sees the new data, sends it, and leaves the UART idle withtx_tail == tx_head; write()then finds no transfer ongoing and starts one from&tx_buff[tx_tail]
(uart_serial.c L87-L109),
although the buffer is empty: a stale byte is sent;- at the end of that transfer
tx_tailhas passedtx_head, so_tx_complete_irq()sees
SERIAL_TX_BUFFER_SIZE - 1bytes pending and sends them.
The result is that the last SERIAL_TX_BUFFER_SIZE bytes (64 by default) are sent a second time. Less often, one
byte is replaced by the byte written SERIAL_TX_BUFFER_SIZE bytes earlier. Nothing is lost, so the output just looks
like repeated or garbled text.
It seems to need the main loop to be held by interrupts for about a character time at that exact point, so it
probably only shows up under heavy interrupt load: full-duplex traffic at high baud rates, or a busy application ISR.
This was already seen in Marlin
Marlin identified the same race on STM32F0 and STM32F1 boards and fixed it on their side in
MarlinFirmware/Marlin#28904 ("Make STM32 serial write atomic"), by calling the core's write() with interrupts
disabled. As far as I could find, no issue was opened here, and the code still looks the same on main.
To Reproduce
Any sketch that writes while receiving at a high rate, for example an echo:
void setup() {
Serial.begin(921600);
}
void loop() {
while (Serial.available()) {
Serial.write(Serial.read());
}
}From the host, stream random bytes and compare what comes back. With an echo sketch along these lines (ours also
buffers and throttles the echo), 64 KB echoed at full rate come back with a few blocks of exactly 64 bytes repeated,
each one a copy of the 64 bytes just before it. To check where they come from:
- built with
-D SERIAL_TX_BUFFER_SIZE=128, the repeated blocks become exactly 128 bytes long; - with
Serial.write()called with interrupts disabled (Marlin's workaround, from the sketch), there are no repeated
blocks at all, and no lost bytes either.
Possible fix
I may be missing something, but making these lines a critical section in Uart::write() might be enough. I think
it would need PRIMASK, saved and restored (#1834), since uart_attach_tx_callback() re-enables the UART IRQ itself:
uint32_t primask = __get_PRIMASK();
__disable_irq();
_serial.tx_head = (_serial.tx_head + size) % SERIAL_TX_BUFFER_SIZE;
if (!serial_tx_active(&_serial)) {
_serial.tx_size = size_intermediate;
uart_attach_tx_callback(&_serial, _tx_complete_irq, size_intermediate);
}
__set_PRIMASK(primask);I have only tested the equivalent from the sketch, not a patched core. If it seems reasonable, I would be happy to
try a PR.
Related
- HartdwareSerial: recover original Arduino asynchronous write API #1503 and HardwareSerial: avoid waiting loop #1492: introduced the current asynchronous
write(). The
review comment on 552a133
already noted that restarting the transfer from the TX callback "would require some careful review of the code for
race conditions, though (probably disabling interrupts in some places)". - Hardware serial ports issue with full-duplex usage #467 and [PR 496 rework] Disable USART IRQ in uart_write and uart_debug_write #502: an earlier full-duplex race, which led to masking the UART IRQ in
uart_attach_*(). - HardwareSerial/Uart write and flush lock up when interrupts are disabled #472 #1834:
write()andflush()when interrupts are disabled. - 🐛 Make STM32 serial write atomic MarlinFirmware/Marlin#28904: the same race seen from Marlin, and its fix.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsTo do
Activity
fpistm commentedon Nov 30, 2026
Hi @nedseb,
Thank you for the very detailed report: the analysis of the race, the links to
the related issues and the Marlin fix, and the reproduction hints made it easy
to confirm and fix.
I reproduced it on a NUCLEO_C562RE and opened #3128 with a fix along the lines
you proposed (the
tx_headupdate and the transfer start in a critical sectionwith PRIMASK saved/restored). The transfer size is also computed from
tx_head/tx_tailinside the critical section, and a transfer is started onlyif data are pending.
Could you please review the PR and test it with your setup (the full-rate echo
sketch that showed the repeated 64-byte blocks)? Feedback on other boards or
baud rates would be welcome.
Thanks!
nedseb commentedon Nov 30, 2026
Thank you so much!
I'll try it tomorrow. I am on this bug for several weeks and I investigate mainly on Daplink without finding the reason of this repeated 64 bytes.
Best regards
Sébastien