Thread (6 messages) flat view 6 messages, 3 authors, 15d ago
COLD15d

[PATCH i-g-t] lib/igt_core: buffer signal-safe log output per line

From: Jeevan B <hidden>
Date: 2026-09-07 05:21:09
Subsystem: library code, the rest · Maintainers: Andrew Morton, Linus Torvalds

xputch() wrote one byte at a time via write(STDERR_FILENO, &c, 1) from
the async-signal-safe backtrace printer. When a fatal signal hits
multiple processes/threads at once (e.g. forked test children all
dumping a backtrace after the same signal), their single-byte writes
interleave on the shared stderr/runner fd, producing garbled or
truncated output. This showed up as tests (e.g.
igt@kms_cursor_legacy@*) being reported as incomplete with no useful
logs.

Buffer output in xputch() and flush it with a single write() (or
log_to_runner_sig_safe() call) per line via a new xflush(), instead of
one syscall per character. xprintfmt() now also flushes on reaching
the end of the format string, so a trailing partial line without a
newline is still emitted.

Assisted-by: Claude:claude-sonnet-4-5
Signed-off-by: Jeevan B <redacted>
---
 lib/igt_core.c | 31 ++++++++++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)
diff --git a/lib/igt_core.c b/lib/igt_core.c
index af9d93762..e376a972c 100644
--- a/lib/igt_core.c
+++ b/lib/igt_core.c
@@ -2105,13 +2105,37 @@ static void write_stderr(const char *str)
 #ifdef HAVE_LIBUNWIND
 static const char hex[] = "0123456789abcdef";
 
+/*
+ * Buffer output and flush it in one write() per line, instead of one
+ * write() per character. Concurrent single-byte writes from multiple
+ * processes (e.g. forked children all dumping a backtrace after
+ * receiving the same fatal signal) interleave on the shared stderr/
+ * runner fd and produce unreadable, garbled logs.
+ */
+static char xputch_buf[256];
+static size_t xputch_buf_len;
+
 static void
-xputch(int c)
+xflush(void)
 {
+	if (!xputch_buf_len)
+		return;
+
 	if (runner_connected())
-		log_to_runner_sig_safe((const void *) &c, 1);
+		log_to_runner_sig_safe(xputch_buf, xputch_buf_len);
 	else
-		igt_ignore_warn(write(STDERR_FILENO, (const void *) &c, 1));
+		igt_ignore_warn(write(STDERR_FILENO, xputch_buf, xputch_buf_len));
+
+	xputch_buf_len = 0;
+}
+
+static void
+xputch(int c)
+{
+	xputch_buf[xputch_buf_len++] = c;
+
+	if (c == '\n' || xputch_buf_len == sizeof(xputch_buf))
+		xflush();
 }
 
 static int
@@ -2166,6 +2190,7 @@ xprintfmt(const char *fmt, va_list ap)
 	while (1) {
 		while ((ch = *(const unsigned char *) fmt++) != '%') {
 			if (ch == '\0') {
+				xflush();
 				return;
 			}
 			xputch(ch);
-- 
2.43.0
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help