1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

fix(instances): a late cancel leaves a later job's migration or disk move alone

A cancel that comes before libvirt began a migration or disk move waits
for it. When the job ended meanwhile and another job of the VM started
its own migration or copy within the hook's second of sleep, the hook
took that for its own and aborted it. The hook now asks whether its job
is still active right before it aborts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-09 16:27:31 +03:00
parent c49ee24aae
commit e6155c855a
2 changed files with 30 additions and 11 deletions

View file

@ -99,6 +99,11 @@ def _libvirt_busy(dom):
return dom.isActive() and dom.jobInfo()[0] != VIR_DOMAIN_JOB_NONE
def _active(op):
"""The job has not ended: a cancel hook still acts for it."""
return Operation.objects.filter(pk=op.pk, state__in=Operation.ACTIVE).exists()
def _log(op, state, action, vm=None):
"""The job's row: "VM <name>: <action>", the end state added unless it succeeded."""
compute, _sep, label_vm = op.resource_label.rpartition("/") # a VM name has no "/"
@ -1159,13 +1164,13 @@ class MigrateInstance(Kind):
def cancel(self, op):
dom = _proxy(_instance(op.params["instance"])).instance
# the worker sends a cancel once: one that comes before libvirt began
# the migration waits for it, or for the job to end without it
while not _libvirt_busy(dom):
if not Operation.objects.filter(pk=op.pk, state__in=Operation.ACTIVE).exists():
return
# the migration waits for it, or for the job to end without it. The job is
# asked for last: once it ended, a migration libvirt runs is another job's
while _active(op) and not _libvirt_busy(dom):
time.sleep(1)
# an error (the migration ended meanwhile) goes to the worker log
dom.abortJob()
if _active(op):
# an error (the migration ended meanwhile) goes to the worker log
dom.abortJob()
def reconcile(self, op):
source = _domain(op.intent["compute"], op.intent["uuid"])
@ -1260,12 +1265,11 @@ class MoveDisk(Kind):
def cancel(self, op):
dom = _proxy(_instance(op.params["instance"])).instance
# the worker sends a cancel once: one before the copy began waits for it
while dom.isActive() and not dom.blockJobInfo(op.params["dev"], 0):
if not Operation.objects.filter(pk=op.pk, state__in=Operation.ACTIVE).exists():
return
# the worker sends a cancel once: one before the copy began waits for it.
# The job is asked for last: once it ended, a copy libvirt runs is another job's
while _active(op) and dom.isActive() and not dom.blockJobInfo(op.params["dev"], 0):
time.sleep(1)
if dom.isActive():
if _active(op) and dom.isActive():
dom.blockJobAbort(op.params["dev"], 0)
def reconcile(self, op):

View file

@ -308,6 +308,21 @@ class MigrateJobTestCase(TransactionTestCase):
get_kind(op.kind).cancel(op)
proxy.return_value.instance.abortJob.assert_not_called()
def test_a_waiting_cancel_leaves_the_migration_of_a_later_job_alone(self):
# the job ends before libvirt began; another job's migration of the VM starts meanwhile
op = self.queue(live_migrate="true")
self.start(op)
def ended(_seconds):
Operation.objects.filter(pk=op.pk).update(state=Operation.FAILED)
with patch("instances.jobs.wvmInstance") as proxy, patch("instances.jobs.time.sleep", side_effect=ended):
dom = proxy.return_value.instance
dom.isActive.return_value = True
dom.jobInfo.side_effect = [[0] * 12] + [[2] + [0] * 11] * 3 # no job yet, then the later one's
get_kind(op.kind).cancel(op)
dom.abortJob.assert_not_called()
def test_each_end_of_a_migration_leaves_one_logs_row(self):
op = self.queue(live_migrate="true")
ctx = self.start(op)