When compiling QEMU with --enable-trace-backends=simple , the iotest 233 is currently hanging. This happens because qemu-nbd calls trace_init_backends() first - which causes simpletrace to install its writer thread and the atexit() handler - before calling fork(). But the simpletrace writer thread is then only available in the parent process, not in the child process anymore. Thus when the child process exits, its atexit handler waits forever on the trace_empty_cond condition to be set by the non-existing writer thread, so the process never finishes.
Fix it by installing a pthread_atfork() handler, too, which makes sure that the trace_writeout_enabled variable gets set to false again in the child process, so we can use it in the atexit() handler to check whether we still need to wait on the writer thread or not. Signed-off-by: Thomas Huth <th...@redhat.com> --- trace/simple.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/trace/simple.c b/trace/simple.c index c0aba00cb7f..269bbda69f1 100644 --- a/trace/simple.c +++ b/trace/simple.c @@ -380,8 +380,22 @@ void st_print_trace_file_status(void) void st_flush_trace_buffer(void) { - flush_trace_file(true); + flush_trace_file(trace_writeout_enabled); +} + +#ifndef _WIN32 +static void trace_thread_atfork(void) +{ + /* + * If we fork, the writer thread does not exist in the child, so + * make sure to allow st_flush_trace_buffer() to clean up correctly. + */ + g_mutex_lock(&trace_lock); + trace_writeout_enabled = false; + g_cond_signal(&trace_empty_cond); + g_mutex_unlock(&trace_lock); } +#endif /* Helper function to create a thread with signals blocked. Use glib's * portable threads since QEMU abstractions cannot be used due to reentrancy in @@ -396,6 +410,7 @@ static GThread *trace_thread_create(GThreadFunc fn) sigfillset(&set); pthread_sigmask(SIG_SETMASK, &set, &oldset); + pthread_atfork(NULL, NULL, trace_thread_atfork); #endif thread = g_thread_new("trace-thread", fn, NULL); -- 2.48.1