From e687c9b4d9ffcbc1c03e76405d58dc77366797b3 Mon Sep 17 00:00:00 2001 From: Andrew Wood Date: Fri, 25 Jul 2025 20:13:49 +0100 Subject: [PATCH] When EOF on input and output is reached but the output pipe buffer is not empty, sleep for a short time before looping while waiting for the buffer to empty, to avoid a busy-wait (#164). --- src/pv/loop.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/pv/loop.c b/src/pv/loop.c index bd89c72..3a967ff 100644 --- a/src/pv/loop.c +++ b/src/pv/loop.c @@ -400,6 +400,17 @@ int pv_main_loop(pvstate_t state) } } + /* + * If we've read everything and written everything, and the + * output pipe buffer is NOT empty, then pause a short while + * so we don't spin in a tight loop waiting for the output + * buffer to empty (#164). + */ + if (eof_in && eof_out && state->transfer.written_but_not_consumed > 0) { + debug("%s", "EOF but bytes remain in output pipe - sleeping"); + pv_nanosleep(50000000); + } + /* * Just go round the loop again if there's no display and * we're not reporting statistics.