diff --git a/operations/tests/test_recovery.py b/operations/tests/test_recovery.py index 3cccc227..d116f506 100644 --- a/operations/tests/test_recovery.py +++ b/operations/tests/test_recovery.py @@ -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: diff --git a/operations/worker.py b/operations/worker.py index e4757946..1838e2ac 100644 --- a/operations/worker.py +++ b/operations/worker.py @@ -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)