From 7d96727ee98fce8f3cc0e603895df0f6fa7b8ee0 Mon Sep 17 00:00:00 2001 From: Cyril Hrubis Date: Wed, 13 Aug 2025 11:54:07 +0200 Subject: [PATCH] ltx_send_message() retry write properly on EAGAIN When we are doing file transfer we can easily saturate the serial connection or pipe we write into and get EAGAIN. The original code just ignored all write errors, which caused lost bytes and broken communication with kirk. This commit fixes two bugs in the retry loop: - properly adjust the buffer position on subsequent write attempts - do a short sleep on EAGAIN and retry later on We also print message into stderr when write() failed for any other reason than EAGAIN so that there is at least some hint of a failure in that case. Signed-off-by: Cyril Hrubis --- ltx.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/ltx.c b/ltx.c index d7fbe3d..68a93c9 100644 --- a/ltx.c +++ b/ltx.c @@ -2,6 +2,7 @@ /* * Copyright (c) 2022 Richard Palethorpe * Copyright (c) 2023 Andrea Cervesato + * Copyright (c) 2025 Cyril Hrubis */ #define _GNU_SOURCE /* CLOCK_MONOTONIC */ @@ -213,9 +214,17 @@ static void ltx_send_message( ssize_t pos = 0, ret; do { - ret = write(session->stdout_fd, msg->data, msg->length); - if (ret == -1) + ret = write(session->stdout_fd, msg->data+pos, msg->length-pos); + + if (ret == -1 && errno == EAGAIN) { + usleep(100); + continue; + } + + if (ret == -1) { + fprintf(stderr, "write() failed %s", strerror(errno)); break; + } pos += ret; } while (pos < (ssize_t)msg->length);