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

fix(operations): a cancel during recovery cannot hold up its job for good

After a worker restart, recovery watches a migration or disk move that
libvirt still runs and sends a cancel asked meanwhile to the job's hook.
It ran inline in the inspection. The hook waits until libvirt's job runs
or the panel's job ends; when libvirt finished just after the inspection
saw it run, only the next inspection could record the end, so the job
stayed cancelling for good and held its host's control slot. The hook
now runs in a thread of its own, and the next inspection ends the job.

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:13:58 +03:00
parent 8197e645f9
commit c49ee24aae
2 changed files with 36 additions and 4 deletions

View file

@ -1,3 +1,5 @@
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from unittest.mock import patch
@ -187,13 +189,34 @@ class RecoveryTestCase(TransactionTestCase):
def test_a_cancel_of_a_job_libvirt_still_runs_goes_to_libvirt_once(self):
op = self.orphan({})
Operation.objects.filter(pk=op.pk).update(state=Operation.CANCELLING, cancel_requested=True)
with patch.object(fakes.ScriptedKind, "cancel") as cancel:
sent = threading.Event()
with patch.object(fakes.ScriptedKind, "cancel", side_effect=lambda op: sent.set()) as cancel:
self.run_at(1000, Outcome(Operation.RUNNING))
self.assertTrue(sent.wait(5)) # in a thread of its own
self.scheduler.next_try[op.pk] = 0
self.run_at(1015, Outcome(Operation.RUNNING))
cancel.assert_called_once()
self.assertEqual(cancel.call_args.args[0].pk, op.pk)
def test_a_cancel_hook_waiting_for_the_job_to_end_does_not_hold_up_its_inspection(self):
# libvirt finished the job just after the inspection saw it run: the hook waits for an end that
# only the next inspection can record
op = self.orphan({})
Operation.objects.filter(pk=op.pk).update(state=Operation.CANCELLING, cancel_requested=True)
ended = threading.Event()
def cancel(op):
while Operation.objects.filter(pk=op.pk, state__in=Operation.ACTIVE).exists():
time.sleep(0.05)
ended.set()
with patch.object(fakes.ScriptedKind, "cancel", side_effect=cancel):
self.run_at(1000, Outcome(Operation.RUNNING))
self.scheduler.next_try[op.pk] = 0
self.run_at(1015, Outcome(Operation.SUCCEEDED))
self.assertTrue(ended.wait(5), "the hook ends with the job")
self.assertEqual(self.reload(op).state, Operation.SUCCEEDED)
def test_a_job_without_a_cancel_is_watched_without_one(self):
self.orphan({})
with patch.object(fakes.ScriptedKind, "cancel") as cancel:

View file

@ -19,7 +19,7 @@ from importlib import import_module
from pathlib import Path
from django.conf import settings
from django.db import DatabaseError, close_old_connections
from django.db import DatabaseError, close_old_connections, connection
from django.utils import timezone
from django.utils.translation import gettext_noop
@ -346,6 +346,13 @@ def _guarded(fn, op=None):
close_old_connections()
def _cancel_apart(fn, op):
try:
_guarded(fn, op)
finally:
connection.close() # the thread's own
def execute(worker, op_id):
"""A job thread: precheck and run. Errors before effects fail the job;
after effects they go to inspection; a lost lease leaves it alone."""
@ -494,10 +501,12 @@ class Scheduler:
return
self.failing_since.pop(op_id, None)
if outcome.state == Operation.RUNNING:
# libvirt still runs it: a cancel asked meanwhile goes to libvirt once, as for a job thread's
# libvirt still runs it: a cancel asked meanwhile goes to libvirt once, as for a job thread's.
# In a thread of its own: a hook may wait for the job to end, which only this inspection
# can find (libvirt finished it meanwhile); inline it would wait for good.
if op.cancel_requested and op_id not in self.cancel_sent:
self.cancel_sent.add(op_id)
_guarded(jobs.get_kind(op.kind).cancel, op)
threading.Thread(target=_cancel_apart, args=(jobs.get_kind(op.kind).cancel, op), daemon=True).start()
now = time.monotonic()
if not outcome.stalled:
self.stalled_since.pop(op_id, None)