Skip to content

fix(uart): make Uart::write() TX start atomic - #3128

Open
fpistm wants to merge 2 commits into
stm32duino:mainfrom
fpistm:uart-write-race
Open

fpistm wants to merge 2 commits into
stm32duino:mainfrom
fpistm:uart-write-race

Conversation

@fpistm

@fpistm fpistm commented Nov 30, 2026

Copy link
Copy Markdown
Member

Summary

This PR fixes the following bugs

  • Bug 1: Uart::write() can restart a TX transfer from an empty buffer
    and send the last SERIAL_TX_BUFFER_SIZE bytes twice

Uart::write() queued the data, then started a HAL transfer if none was
ongoing, in two non-atomic steps. If write() was preempted between the
tx_head update and the serial_tx_active() check for longer than the new
data takes to send, the TX complete IRQ sent the data and left the UART idle
with tx_tail == tx_head. write() then started a transfer from the empty
buffer (a stale byte), and at its end tx_tail went past tx_head, so the
last SERIAL_TX_BUFFER_SIZE bytes were sent again.

  • The tx_head update and the transfer start are now a critical section.
    PRIMASK is saved and restored, as write() can be called with interrupts
    disabled.
  • The transfer size is computed from tx_head/tx_tail inside the critical
    section and a transfer is only started if data are pending.
  • A second commit refreshes the free space after the "buffer full" wait, which
    removes a useless zero-length recursive write().

Validation

Tested on NUCLEO_C562RE at 921600 baud with an echo sketch and a host script
checking that no byte is lost, repeated or corrupted. A timer interrupt of lower
priority than the UART one (30 us at 5 kHz) holds the main loop to trigger the
race, with -DSERIAL_RX_BUFFER_SIZE=1024 to avoid unrelated RX overflow.

  • main: FAIL 6 loops out of 10 (corrupted bytes, stale data sent)
  • this PR: PASS 50 loops out of 50; also PASS with 200 bytes write() calls
    (20 loops) and with a text file

Code formatting

  • AStyle to be checked by CI

Closing issues

Fixes #3125

Uart::write() updated tx_head and then checked whether a HAL transfer
was ongoing, without protection. If write() was preempted between the
two steps for longer than the new data takes to send, the TX complete
IRQ sent the data and left the UART idle with tx_tail == tx_head.
write() then started a transfer from the empty buffer, and at its end
tx_tail went past tx_head, so the last SERIAL_TX_BUFFER_SIZE bytes were
sent a second time.

Update tx_head and start the transfer in a critical section, restoring
PRIMASK afterwards as write() may be called with interrupts disabled.
Compute the transfer size from tx_head and tx_tail inside the critical
section, and only start a transfer if data are pending.

Fixes: stm32duino#3125
Assisted-by: GitHub Copilot: claude-sonnet-5.5
Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
When the TX buffer was full, Uart::write() waited for the TX interrupt
to free space but kept the free space value read before the wait, so
it was 0. The split loop then recursed with a zero-length write before
the real one.

Read the free space again in the wait loop so the data are queued
directly.

Tested on NUCLEO_C562RE at 921600 baud with an echo using single byte
and 200 bytes write() calls, with and without interrupt load.

Assisted-by: GitHub Copilot: claude-sonnet-5.5
Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix 🩹 Bug fix

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

HardwareSerial: race in Uart::write() restarts TX from an empty buffer and resends the whole TX buffer

1 participant