commit 0149464afc7100f048a2468e40e84b6a50aeb3a3
Author: Jim Fehlig <jfehlig@suse.com>
Date:   Wed Aug 29 11:11:00 2018 -0600

    libxl: fix job handling across migration phases on dst
    
    The libxlDomainMigrationDst* functions are a bit flawed in their
    handling of modify jobs. A job begins when the destination host
    begins receiving the incoming VM and ends after the VM is started.
    The finish phase contains another BeginJob/EndJob sequence.
    
    This patch changes the logic to begin a job for the incoming VM
    in the prepare phase and end the job in the finish phase.
    
    Signed-off-by: Jim Fehlig <jfehlig@suse.com>
    ACKed-by: Michal Privoznik <mprivozn@redhat.com>

Index: libvirt-2.0.0/src/libxl/libxl_driver.c
===================================================================
--- libvirt-2.0.0.orig/src/libxl/libxl_driver.c
+++ libvirt-2.0.0/src/libxl/libxl_driver.c
@@ -5663,15 +5663,8 @@ libxlDomainMigrateFinish3Params(virConne
         return NULL;
     }
 
-    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0) {
-        virDomainObjEndAPI(&vm);
-        return NULL;
-    }
-
     ret = libxlDomainMigrationFinish(dconn, vm, flags, cancelled);
 
-    libxlDomainObjEndJob(driver, vm);
-
     virDomainObjEndAPI(&vm);
 
     return ret;
Index: libvirt-2.0.0/src/libxl/libxl_migration.c
===================================================================
--- libvirt-2.0.0.orig/src/libxl/libxl_migration.c
+++ libvirt-2.0.0/src/libxl/libxl_migration.c
@@ -265,9 +265,6 @@ libxlDoMigrateReceive(void *opaque)
     size_t i;
 
     virObjectRef(vm);
-    virObjectLock(vm);
-    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
-        goto cleanup;
 
     /*
      * Always start the domain paused.  If needed, unpause in the
@@ -287,10 +284,6 @@ libxlDoMigrateReceive(void *opaque)
     args->nsocks = 0;
     VIR_FORCE_CLOSE(recvfd);
     virObjectUnref(args);
-
-    libxlDomainObjEndJob(driver, vm);
-
- cleanup:
     virDomainObjEndAPI(&vm);
 }
 
@@ -521,29 +514,36 @@ libxlDomainMigrationPrepare(virConnectPt
         goto error;
     *def = NULL;
 
+    /*
+     * Unless an error is encountered in this function, the job will
+     * be terminated in the finish phase.
+     */
+    if (libxlDomainObjBeginJob(driver, vm, LIBXL_JOB_MODIFY) < 0)
+        goto error;
+
     /* Create socket connection to receive migration data */
     if (!uri_in) {
         if ((hostname = virGetHostname()) == NULL)
-            goto error;
+            goto endjob;
 
         if (STRPREFIX(hostname, "localhost")) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("hostname on destination resolved to localhost,"
                              " but migration requires an FQDN"));
-            goto error;
+            goto endjob;
         }
 
         if (virPortAllocatorAcquire(driver->migrationPorts, &port) < 0)
-            goto error;
+            goto endjob;
 
         if (virAsprintf(uri_out, "tcp://%s:%d", hostname, port) < 0)
-            goto error;
+            goto endjob;
     } else {
         if (!(STRPREFIX(uri_in, "tcp://"))) {
             /* not full URI, add prefix tcp:// */
             char *tmp;
             if (virAsprintf(&tmp, "tcp://%s", uri_in) < 0)
-                goto error;
+                goto endjob;
             uri = virURIParse(tmp);
             VIR_FREE(tmp);
         } else {
@@ -554,28 +554,28 @@ libxlDomainMigrationPrepare(virConnectPt
             virReportError(VIR_ERR_INVALID_ARG,
                            _("unable to parse URI: %s"),
                            uri_in);
-            goto error;
+            goto endjob;
         }
 
         if (uri->server == NULL) {
             virReportError(VIR_ERR_INVALID_ARG,
                            _("missing host in migration URI: %s"),
                            uri_in);
-            goto error;
+            goto endjob;
         } else {
             hostname = uri->server;
         }
 
         if (uri->port == 0) {
             if (virPortAllocatorAcquire(driver->migrationPorts, &port) < 0)
-                goto error;
+                goto endjob;
 
         } else {
             port = uri->port;
         }
 
         if (virAsprintf(uri_out, "tcp://%s:%d", hostname, port) < 0)
-            goto error;
+            goto endjob;
     }
 
     snprintf(portstr, sizeof(portstr), "%d", port);
@@ -585,14 +585,14 @@ libxlDomainMigrationPrepare(virConnectPt
                                  &socks, &nsocks) < 0) {
         virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                        _("Fail to create socket for incoming migration"));
-        goto error;
+        goto endjob;
     }
 
     if (libxlMigrationDstArgsInitialize() < 0)
-        goto error;
+        goto endjob;
 
     if (!(args = virObjectNew(libxlMigrationDstArgsClass)))
-        goto error;
+        goto endjob;
 
     args->conn = dconn;
     args->vm = vm;
@@ -620,11 +620,14 @@ libxlDomainMigrationPrepare(virConnectPt
     }
 
     if (!nsocks_listen)
-        goto error;
+        goto endjob;
 
     ret = 0;
     goto done;
 
+ endjob:
+    libxlDomainObjEndJob(driver, vm);
+
  error:
     for (i = 0; i < nsocks; i++) {
         virNetSocketClose(socks[i]);
@@ -1030,6 +1033,8 @@ libxlDomainMigrationFinish(virConnectPtr
             virDomainObjListRemove(driver->domains, vm);
     }
 
+    /* EndJob for corresponding BeginJob in prepare phase */
+    libxlDomainObjEndJob(driver, vm);
     if (event)
         libxlDomainEventQueue(driver, event);
     virObjectUnref(cfg);
