Skip to content

Commit 3e16834

Browse files
committed
nbd: Defer trace init until after daemonization
At least the simple trace backend works by spawning a helper thread, and setting up an atexit() handler that coordinates completion with the helper thread. But since atexit registrations survive fork() but helper threads do not, this means that qemu-nbd configured to use the simple trace will deadlock waiting for a thread that no longer exists when it has daemonized. Better is to follow the example of vl.c: don't call any setup functions that might spawn helper threads until we are in the final process that will be doing the work worth tracing. Tested by configuring with --enable-trace-backends=simple, then running qemu-nbd --fork --trace=nbd_\*,file=qemu-nbd.trace -f raw -r README.rst followed by `nbdinfo nbd://localhost`, and observing that the trace file is now created without hanging. Reported-by: Thomas Huth <[email protected]> Signed-off-by: Eric Blake <[email protected]> Message-ID: <[email protected]> Reviewed-by: Thomas Huth <[email protected]>
1 parent 57f3962 commit 3e16834

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

qemu-nbd.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -852,10 +852,6 @@ int main(int argc, char **argv)
852852
export_name = "";
853853
}
854854

855-
if (!trace_init_backends()) {
856-
exit(1);
857-
}
858-
trace_init_file();
859855
qemu_set_log(LOG_TRACE, &error_fatal);
860856

861857
socket_activation = check_socket_activation();
@@ -1045,6 +1041,18 @@ int main(int argc, char **argv)
10451041
#endif /* WIN32 */
10461042
}
10471043

1044+
/*
1045+
* trace_init must be done after daemonization. Why? Because at
1046+
* least the simple backend spins up a helper thread as well as an
1047+
* atexit() handler that waits on that thread, but the helper
1048+
* thread won't survive a fork, leading to deadlock in the child
1049+
* if we initialized pre-fork.
1050+
*/
1051+
if (!trace_init_backends()) {
1052+
exit(1);
1053+
}
1054+
trace_init_file();
1055+
10481056
if (opts.device != NULL && sockpath == NULL) {
10491057
sockpath = g_malloc(128);
10501058
snprintf(sockpath, 128, SOCKET_PATH, basename(opts.device));

0 commit comments

Comments
 (0)