From 1e21abf0c4db08548bb8bfc3088cc0816fbdcd81 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Mon, 15 Feb 2021 10:33:32 +0000 Subject: [PATCH] gtestdbus: Flush stdout and stderr before forking a monitor process MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a workaround for the fact that forking without execing is not easy to do correctly, and `GTestDBus` doesn’t do it correctly. However, `GTestDBus` is de-facto deprecated and so putting any more effort in is a waste. This fixes an issue where a test would print duplicate output when outputting to a fully-buffered FD, such as a pipe. This is because the buffer is non-empty before the `fork()`, and ends up duplicated in the parent and child processes, both of which later flush the duplicated buffer contents. Diagnosed and fix suggested by Simon McVittie. Signed-off-by: Philip Withnall Fixes: #2322 --- gio/gtestdbus.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/gio/gtestdbus.c b/gio/gtestdbus.c index 9cfaadbf0..d362f9478 100644 --- a/gio/gtestdbus.c +++ b/gio/gtestdbus.c @@ -251,6 +251,16 @@ watcher_init (void) g_assert_not_reached (); } + /* flush streams to avoid buffers being duplicated in the child and + * flushed by both the child and parent later + * + * FIXME: This is a workaround for the fact that watch_parent() uses + * non-async-signal-safe API. See + * https://gitlab.gnome.org/GNOME/glib/-/issues/2322#note_1034330 + */ + fflush (stdout); + fflush (stderr); + switch (fork ()) { case -1: