From: Thomas Huth <thuth@redhat.com>
To: Eric Blake <eblake@redhat.com>, qemu-devel@nongnu.org
Cc: qemu-block@nongnu.org
Subject: Re: [PATCH] nbd: Defer trace init until after daemonization
Date: Wed, 5 Mar 2025 07:55:48 +0100 [thread overview]
Message-ID: <ecfdf860-7bbd-4744-9195-2eee2c7a0d05@redhat.com> (raw)
In-Reply-To: <20250227220625.870246-2-eblake@redhat.com>
On 27/02/2025 23.06, Eric Blake wrote:
> 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 <thuth@redhat.com>
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
> qemu-nbd.c | 16 ++++++++++++----
> 1 file changed, 12 insertions(+), 4 deletions(-)
>
> diff --git a/qemu-nbd.c b/qemu-nbd.c
> index 05b61da51ea..ed5895861bb 100644
> --- a/qemu-nbd.c
> +++ b/qemu-nbd.c
> @@ -852,10 +852,6 @@ int main(int argc, char **argv)
> export_name = "";
> }
>
> - if (!trace_init_backends()) {
> - exit(1);
> - }
> - trace_init_file();
> qemu_set_log(LOG_TRACE, &error_fatal);
>
> socket_activation = check_socket_activation();
> @@ -1045,6 +1041,18 @@ int main(int argc, char **argv)
> #endif /* WIN32 */
> }
>
> + /*
> + * trace_init must be done after daemonization. Why? Because at
> + * least the simple backend spins up a helper thread as well as an
> + * atexit() handler that waits on that thread, but the helper
> + * thread won't survive a fork, leading to deadlock in the child
> + * if we initialized pre-fork.
> + */
> + if (!trace_init_backends()) {
> + exit(1);
> + }
> + trace_init_file();
This also sounds like the best option to me!
Reviewed-by: Thomas Huth <thuth@redhat.com>
next prev parent reply other threads:[~2025-03-05 6:56 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-27 22:06 [PATCH] nbd: Defer trace init until after daemonization Eric Blake
2025-03-05 6:55 ` Thomas Huth [this message]
2025-03-05 19:00 ` Eric Blake
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ecfdf860-7bbd-4744-9195-2eee2c7a0d05@redhat.com \
--to=thuth@redhat.com \
--cc=eblake@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).