Skip to content

Commit 4e44270

Browse files
metan-ucwacerv
authored andcommitted
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 <metan@ucw.cz>
1 parent 334d1ab commit 4e44270

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

ltx.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/*
33
* Copyright (c) 2022 Richard Palethorpe <rpalethorpe@suse.com>
44
* Copyright (c) 2023 Andrea Cervesato <andrea.cervesato@suse.com>
5+
* Copyright (c) 2025 Cyril Hrubis <chrubis@suse.cz>
56
*/
67

78
#define _GNU_SOURCE /* CLOCK_MONOTONIC */
@@ -213,9 +214,17 @@ static void ltx_send_message(
213214

214215
ssize_t pos = 0, ret;
215216
do {
216-
ret = write(session->stdout_fd, msg->data, msg->length);
217-
if (ret == -1)
217+
ret = write(session->stdout_fd, msg->data+pos, msg->length-pos);
218+
219+
if (ret == -1 && errno == EAGAIN) {
220+
usleep(100);
221+
continue;
222+
}
223+
224+
if (ret == -1) {
225+
fprintf(stderr, "write() failed %s", strerror(errno));
218226
break;
227+
}
219228

220229
pos += ret;
221230
} while (pos < (ssize_t)msg->length);

0 commit comments

Comments
 (0)