gtestdbus: Flush stdout and stderr before forking a monitor process

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 <pwithnall@endlessos.org>

Fixes: #2322
This commit is contained in:
Philip Withnall 2021-02-15 10:33:32 +00:00
parent b9248b0e24
commit 1e21abf0c4

View File

@ -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: