This is an automated email from the ASF dual-hosted git repository.
acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new e9eefaa0e9a drivers/serial: drain xmit buffer before direct write on
panic.
e9eefaa0e9a is described below
commit e9eefaa0e9ab3ddbbe1ac0507600164a240c5c7f
Author: yukangzhi <[email protected]>
AuthorDate: Fri Jul 24 11:42:01 2026 +0800
drivers/serial: drain xmit buffer before direct write on panic.
When a panic occurs, TX interrupts are disabled and can no longer drain
the serial xmit ring buffer. Panic-context syslog output bypasses the
xmit buffer (via uart_irqwritev()) and writes directly to the hardware.
As a result the log data that was buffered before the panic is lost and
the log ordering is broken.
Fix by draining the xmit buffer in uart_writev() before
uart_irqwritev() when OSINIT_IS_PANIC(), preserving the chronological
log order without requiring callers to flush explicitly. The drain is
skipped in normal interrupt context to avoid adding latency there.
Assisted-by: Kiro
Signed-off-by: yukangzhi <[email protected]>
---
drivers/serial/serial.c | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/drivers/serial/serial.c b/drivers/serial/serial.c
index 98019cf93f3..be3345126e5 100644
--- a/drivers/serial/serial.c
+++ b/drivers/serial/serial.c
@@ -45,6 +45,7 @@
#include <nuttx/ascii.h>
#include <nuttx/arch.h>
#include <nuttx/clock.h>
+#include <nuttx/init.h>
#include <nuttx/sched.h>
#include <nuttx/signal.h>
#include <nuttx/fs/fs.h>
@@ -1698,6 +1699,28 @@ static ssize_t uart_writev(FAR struct file *filep, FAR
struct uio *uio)
#endif
flags = uart_spinlock(dev, false);
+
+ /* In panic state, drain xmit buffer first to preserve log order.
+ * Earlier buffered output must appear before new direct-write data.
+ * Skip in normal interrupt context to avoid excessive latency.
+ */
+
+ if (OSINIT_IS_PANIC())
+ {
+ while (dev->xmit.tail != dev->xmit.head)
+ {
+ while (!uart_txready(dev))
+ {
+ }
+
+ uart_send(dev, dev->xmit.buffer[dev->xmit.tail]);
+ if (++(dev->xmit.tail) >= dev->xmit.size)
+ {
+ dev->xmit.tail = 0;
+ }
+ }
+ }
+
ret = uart_irqwritev(dev, uio, oktoblock);
uart_spinunlock(dev, false, flags);