From nobody Fri May 3 02:52:17 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1605273208; cv=none; d=zohomail.com; s=zohoarc; b=jd3Xspg4a8fhwFjjAMkd11YFAlaS5bvuFmbY388ofznKCSlr6FTPgJd4xvByi2TaR5azAU6G0cIA3unrt4km7gXGSNJtd3f2YYRJ5yGyoO8Sz8FMwOJ3k2gdd8mVt/xQu/fEZqbceCL6VmA1hnoD0iRklCbH3fPL5IrFl/FbzI8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1605273208; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=kGpMXf4RvJmq7aqQyKPei6PV8Wgq37vvjVS/7xj8NnE=; b=lj/tdPsOxLBM5JGYwZ4C0UzdV0jWPTNZNyCV1tYPeXGDT2tHTXfDBc2DRU1Ug0zDx0eQoOPZPLMBm2d55JH3WBgZf7cTMN+bvOgOV6ipSwn/DMwUHRJt+eGV+dZmtQYTy+WgHoHURdJN1OeBMaTmu7YOVASFQqK93eHV56Ng+4Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1605273208970654.9701536324842; Fri, 13 Nov 2020 05:13:28 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-341-ttUvANOJNjeyWQKBxCS7Ew-1; Fri, 13 Nov 2020 08:13:25 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E34E81018F7B; Fri, 13 Nov 2020 13:13:16 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B79FA62A16; Fri, 13 Nov 2020 13:13:16 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 7D6CF183D021; Fri, 13 Nov 2020 13:13:16 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0ADDD4HB003446 for ; Fri, 13 Nov 2020 08:13:04 -0500 Received: by smtp.corp.redhat.com (Postfix) id 1DD176EF52; Fri, 13 Nov 2020 13:13:04 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.194.157]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9235F6EF48 for ; Fri, 13 Nov 2020 13:13:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1605273208; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=kGpMXf4RvJmq7aqQyKPei6PV8Wgq37vvjVS/7xj8NnE=; b=eA1vS4ITxcGtPytmXMzBBme/nSnkWZXQSOEe/U7+lK+oGQtPhPgSjFwNVZHwbV90LEVo8K Rye+NeDX9vkSiwhwb3wmzhqJijtyiJayOODoH1SQLW6lMPn1LgEXyGUlD66zKP4skmcgpE 7qIapRnJiC1Nmp0RGa/1qC8nqOTMWkw= X-MC-Unique: ttUvANOJNjeyWQKBxCS7Ew-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 1/2] DO NOT MERGE Date: Fri, 13 Nov 2020 14:12:57 +0100 Message-Id: <00d72c06572b5e879471ff1787d1b068e5d9a350.1605272982.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" This is to help reproduce the race. Build and attach gdb and: handle SIGUSR1 nostop pass handle SIGINT nostop pass and then: kill -SIGUSR1 $(pgrep libvirtd); sleep 1; kill -SIGINT $(pgrep libvirtd) --- src/remote/remote_daemon.c | 64 +++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c index 7607da94be..5b68c9d2f9 100644 --- a/src/remote/remote_daemon.c +++ b/src/remote/remote_daemon.c @@ -483,6 +483,37 @@ static void daemonReloadHandler(virNetDaemonPtr dmn G_= GNUC_UNUSED, } } =20 + +static void daemonStopWorker(void *opaque) +{ + virNetDaemonPtr dmn =3D opaque; + + sleep(10); + + VIR_DEBUG("Begin stop dmn=3D%p", dmn); + + ignore_value(virStateStop()); + + VIR_DEBUG("Completed stop dmn=3D%p", dmn); + + /* Exit daemon cleanly */ + virNetDaemonQuit(dmn); +} + + +/* We do this in a thread to not block the main loop */ +static void daemonStop(virNetDaemonPtr dmn, + siginfo_t *sig G_GNUC_UNUSED, + void *opaque G_GNUC_UNUSED) +{ + virThread thr; + virObjectRef(dmn); + if (virThreadCreateFull(&thr, false, daemonStopWorker, + "daemon-stop", false, dmn) < 0) + virObjectUnref(dmn); +} + + static int daemonSetupSignals(virNetDaemonPtr dmn) { if (virNetDaemonAddSignalHandler(dmn, SIGINT, daemonShutdownHandler, N= ULL) < 0) @@ -493,6 +524,8 @@ static int daemonSetupSignals(virNetDaemonPtr dmn) return -1; if (virNetDaemonAddSignalHandler(dmn, SIGHUP, daemonReloadHandler, NUL= L) < 0) return -1; + if (virNetDaemonAddSignalHandler(dmn, SIGUSR1, daemonStop, NULL) < 0) + return -1; return 0; } =20 @@ -511,32 +544,6 @@ static void daemonInhibitCallback(bool inhibit, void *= opaque) static GDBusConnection *sessionBus; static GDBusConnection *systemBus; =20 -static void daemonStopWorker(void *opaque) -{ - virNetDaemonPtr dmn =3D opaque; - - VIR_DEBUG("Begin stop dmn=3D%p", dmn); - - ignore_value(virStateStop()); - - VIR_DEBUG("Completed stop dmn=3D%p", dmn); - - /* Exit daemon cleanly */ - virNetDaemonQuit(dmn); -} - - -/* We do this in a thread to not block the main loop */ -static void daemonStop(virNetDaemonPtr dmn) -{ - virThread thr; - virObjectRef(dmn); - if (virThreadCreateFull(&thr, false, daemonStopWorker, - "daemon-stop", false, dmn) < 0) - virObjectUnref(dmn); -} - - static GDBusMessage * handleSessionMessageFunc(GDBusConnection *connection G_GNUC_UNUSED, GDBusMessage *message, @@ -550,7 +557,7 @@ handleSessionMessageFunc(GDBusConnection *connection G_= GNUC_UNUSED, if (virGDBusMessageIsSignal(message, "org.freedesktop.DBus.Local", "Disconnected")) - daemonStop(dmn); + daemonStop(dmn, NULL, NULL); =20 return message; } @@ -569,7 +576,7 @@ handleSystemMessageFunc(GDBusConnection *connection G_G= NUC_UNUSED, =20 VIR_DEBUG("dmn=3D%p", dmn); =20 - daemonStop(dmn); + daemonStop(dmn, NULL, NULL); } =20 =20 @@ -1247,5 +1254,6 @@ int main(int argc, char **argv) { VIR_FREE(remote_config_file); daemonConfigFree(config); =20 + sleep(10); return ret; } --=20 2.26.2 From nobody Fri May 3 02:52:17 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1605273197; cv=none; d=zohomail.com; s=zohoarc; b=SDvynyoTuywZm/RctbqBIqNYtPJIezZZZF1dDur1ccJBvXEdia2FYJeoj4XbofZALqHcQQG3///Ze+HPJ/1NLmxkq86sYwyekNGd1n29AR9Pal4owsVS4FvMofWjmjSXS/kUTp/8tcosvWW8srBWDdb1X1P3iQ7aawT16sTwmCQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1605273197; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ZPAr5beFtPrbJMk5lXtNQ4vsiT0niwE5DwC0Qex8RdM=; b=BaGI+SWcrvy/1RQsLHKEvMmIlh12jOtDYYhWZCShG8+KOH0mVQcVMAOVYNYFPnvYw7ypRAURf5kD0AT6gD/fTeokVhV5zYP1Hw1KeF/ldG0Jk7cmJwRKIzwkQufCxh02EC+CZ4o8fLbZQlnb7O8pi9aZzIRR6wtv7gSYnth0ksI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1605273197606104.62259046730901; Fri, 13 Nov 2020 05:13:17 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-530-VIHJM4HuNlicaOE-KfyjxQ-1; Fri, 13 Nov 2020 08:13:14 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8D36A192AB63; Fri, 13 Nov 2020 13:13:07 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 41ED219D6C; Fri, 13 Nov 2020 13:13:07 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id CDC2E922F8; Fri, 13 Nov 2020 13:13:06 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0ADDD4mL003454 for ; Fri, 13 Nov 2020 08:13:04 -0500 Received: by smtp.corp.redhat.com (Postfix) id EE4546EF5D; Fri, 13 Nov 2020 13:13:04 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.194.157]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6F81F6EF4A for ; Fri, 13 Nov 2020 13:13:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1605273196; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=ZPAr5beFtPrbJMk5lXtNQ4vsiT0niwE5DwC0Qex8RdM=; b=BBrsyZqeuFBmfu0KJJ/zGgx/LYK2b9zpuKhrGn7v3n3KACTbCQ8an844PeP7a2X6Cfjf71 Qg/4B9zmBiLV+mAf2Z3zf/ngbOJZOX5LozkyNTwXPa5Hr6jUWmjiFxWIwDjJI4OSaTl/JS 9Ryd7861T7bFHzch7NPqEwMwaMypGos= X-MC-Unique: VIHJM4HuNlicaOE-KfyjxQ-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 2/2] virnetdaemon: Wait for "daemon-stop" thread to finish before quitting Date: Fri, 13 Nov 2020 14:12:58 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" When the host is shutting down then we get PrepareForShutdown signal on DBus to which we react by creating a thread which runs virStateStop() and thus qemuStateStop(). But if scheduling the thread is delayed just a but it may happen that we receive SIGTERM (sent by systemd) to which we respond by quitting our event loop and cleaning up everything (including drivers). And only after that the thread gets to run only to find qemu_driver being NULL. What we can do is to delay exiting event loop and join the thread that's executing virStateStop(). If the join doesn't happen in given timeout (currently 30 seconds) then libvirtd shuts down forcefully anyways (see virNetDaemonRun()). Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=3D1895359 Signed-off-by: Michal Privoznik Reviewed-by: J=C3=A1n Tomko --- src/libvirt_remote.syms | 1 + src/remote/remote_daemon.c | 15 ++++++++++++--- src/rpc/virnetdaemon.c | 17 +++++++++++++++++ src/rpc/virnetdaemon.h | 3 +++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/libvirt_remote.syms b/src/libvirt_remote.syms index d398d20880..3cd84a0854 100644 --- a/src/libvirt_remote.syms +++ b/src/libvirt_remote.syms @@ -88,6 +88,7 @@ virNetDaemonQuit; virNetDaemonRemoveShutdownInhibition; virNetDaemonRun; virNetDaemonSetShutdownCallbacks; +virNetDaemonSetStateStopWorkerThread; virNetDaemonUpdateServices; =20 =20 diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c index 5b68c9d2f9..75bb15a439 100644 --- a/src/remote/remote_daemon.c +++ b/src/remote/remote_daemon.c @@ -506,11 +506,20 @@ static void daemonStop(virNetDaemonPtr dmn, siginfo_t *sig G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { - virThread thr; + virThreadPtr thr; virObjectRef(dmn); - if (virThreadCreateFull(&thr, false, daemonStopWorker, - "daemon-stop", false, dmn) < 0) + + thr =3D g_new0(virThread, 1); + + if (virThreadCreateFull(thr, true, + daemonStopWorker, + "daemon-stop", false, dmn) < 0) { virObjectUnref(dmn); + g_free(thr); + return; + } + + virNetDaemonSetStateStopWorkerThread(dmn, &thr); } =20 =20 diff --git a/src/rpc/virnetdaemon.c b/src/rpc/virnetdaemon.c index 5f0f078fac..e337ff1fde 100644 --- a/src/rpc/virnetdaemon.c +++ b/src/rpc/virnetdaemon.c @@ -71,6 +71,7 @@ struct _virNetDaemon { =20 virNetDaemonShutdownCallback shutdownPrepareCb; virNetDaemonShutdownCallback shutdownWaitCb; + virThreadPtr stateStopThread; int finishTimer; bool quit; bool finished; @@ -108,6 +109,7 @@ virNetDaemonDispose(void *obj) #endif /* !WIN32 */ =20 VIR_FORCE_CLOSE(dmn->autoShutdownInhibitFd); + VIR_FREE(dmn->stateStopThread); =20 virHashFree(dmn->servers); =20 @@ -773,6 +775,9 @@ daemonShutdownWait(void *opaque) if (dmn->shutdownWaitCb && dmn->shutdownWaitCb() < 0) goto finish; =20 + if (dmn->stateStopThread) + virThreadJoin(dmn->stateStopThread); + graceful =3D true; =20 finish: @@ -891,6 +896,18 @@ virNetDaemonRun(virNetDaemonPtr dmn) } =20 =20 +void +virNetDaemonSetStateStopWorkerThread(virNetDaemonPtr dmn, + virThreadPtr *thr) +{ + virObjectLock(dmn); + + VIR_DEBUG("Setting state stop worker thread on dmn=3D%p to thr=3D%p", = dmn, thr); + dmn->stateStopThread =3D g_steal_pointer(thr); + virObjectUnlock(dmn); +} + + void virNetDaemonQuit(virNetDaemonPtr dmn) { diff --git a/src/rpc/virnetdaemon.h b/src/rpc/virnetdaemon.h index 6ae5305e53..fcc6e1fdff 100644 --- a/src/rpc/virnetdaemon.h +++ b/src/rpc/virnetdaemon.h @@ -69,6 +69,9 @@ int virNetDaemonAddSignalHandler(virNetDaemonPtr dmn, void virNetDaemonUpdateServices(virNetDaemonPtr dmn, bool enabled); =20 +void virNetDaemonSetStateStopWorkerThread(virNetDaemonPtr dmn, + virThreadPtr *thr); + void virNetDaemonRun(virNetDaemonPtr dmn); =20 void virNetDaemonQuit(virNetDaemonPtr dmn); --=20 2.26.2