[PATCH v2 2/2] virnetdaemon: Wait for "daemon-stop" thread to finish before quitting

Michal Privoznik mprivozn at redhat.com
Fri Nov 13 13:12:58 UTC 2020


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=1895359

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 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;
 
 
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 = g_new0(virThread, 1);
+
+    if (virThreadCreateFull(thr, true,
+                            daemonStopWorker,
+                            "daemon-stop", false, dmn) < 0) {
         virObjectUnref(dmn);
+        g_free(thr);
+        return;
+    }
+
+    virNetDaemonSetStateStopWorkerThread(dmn, &thr);
 }
 
 
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 {
 
     virNetDaemonShutdownCallback shutdownPrepareCb;
     virNetDaemonShutdownCallback shutdownWaitCb;
+    virThreadPtr stateStopThread;
     int finishTimer;
     bool quit;
     bool finished;
@@ -108,6 +109,7 @@ virNetDaemonDispose(void *obj)
 #endif /* !WIN32 */
 
     VIR_FORCE_CLOSE(dmn->autoShutdownInhibitFd);
+    VIR_FREE(dmn->stateStopThread);
 
     virHashFree(dmn->servers);
 
@@ -773,6 +775,9 @@ daemonShutdownWait(void *opaque)
     if (dmn->shutdownWaitCb && dmn->shutdownWaitCb() < 0)
         goto finish;
 
+    if (dmn->stateStopThread)
+        virThreadJoin(dmn->stateStopThread);
+
     graceful = true;
 
  finish:
@@ -891,6 +896,18 @@ virNetDaemonRun(virNetDaemonPtr dmn)
 }
 
 
+void
+virNetDaemonSetStateStopWorkerThread(virNetDaemonPtr dmn,
+                                     virThreadPtr *thr)
+{
+    virObjectLock(dmn);
+
+    VIR_DEBUG("Setting state stop worker thread on dmn=%p to thr=%p", dmn, thr);
+    dmn->stateStopThread = 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);
 
+void virNetDaemonSetStateStopWorkerThread(virNetDaemonPtr dmn,
+                                          virThreadPtr *thr);
+
 void virNetDaemonRun(virNetDaemonPtr dmn);
 
 void virNetDaemonQuit(virNetDaemonPtr dmn);
-- 
2.26.2




More information about the libvir-list mailing list