1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 09:26:03 +00:00

fix(operations): a job's unexpected error is not shown raw; the log has its traceback

A job that failed on an exception that is not libvirt's or a refusal
showed its raw text to users (a SQL error such as "UNIQUE constraint
failed: instances_instance.uuid", a KeyError's key), and the worker
logged only that text. Users now read "The panel failed unexpectedly;
its log has the details." and the worker logs the exception with its
traceback.

A live clone whose recovery found the clone made but could not thaw the
source's file systems kept that in the error of a succeeded job, which
the job panel does not show. It is a warning of the clone's result, as
when the job itself made the clone.
This commit is contained in:
catborise 2026-11-06 23:10:59 +03:00
parent eaf1d9c36d
commit 3965abd7ec
10 changed files with 375 additions and 277 deletions

View file

@ -21,7 +21,7 @@ from operations.utils import words
from operations import jobs
from operations.models import Claim, Operation, WorkerLease
from operations.tests import fakes
from operations.worker import Worker, data_dir, host_identity, housekeeping
from operations.worker import Worker, data_dir, host_identity, housekeeping, traceback_of
from vrtManager.util import ConnectionFailed
@ -250,12 +250,25 @@ class WorkerTestCase(TransactionTestCase):
def test_a_job_thread_that_raised_before_its_effects_fails(self):
op = self.submit("test.noop", {}, "key-00000001")
with patch("operations.jobs.Context", side_effect=RuntimeError("no context")):
with patch("operations.jobs.Context", side_effect=RuntimeError("no context")), \
self.assertLogs("operations.worker", "WARNING") as logs:
self.start()
self.assertTrue(wait_for(lambda: state(op) == Operation.FAILED))
self.assertIn("no context", words(Operation.objects.get(pk=op.pk).error))
# a fault of the panel: users read that, the log has the exception with its traceback
self.assertEqual(words(Operation.objects.get(pk=op.pk).error), "The panel failed unexpectedly; its log has the details.")
self.assertTrue(any("no context" in line and "Traceback" in line for line in logs.output))
self.assertFalse(Claim.objects.filter(operation=op).exists())
def test_a_traceback_in_the_log_has_no_secret(self):
# as the message: the exception's text is written again at its end
try:
raise RuntimeError("password=review-dummy-secret")
except RuntimeError as err:
trace = traceback_of(err)
self.assertIn("Traceback", trace)
self.assertIn("password=***", trace)
self.assertNotIn("review-dummy-secret", trace)
def test_a_database_error_skips_a_tick(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)

View file

@ -7,6 +7,7 @@ the Settings page. The worker reads them itself (the settings middleware only
runs in gunicorn)."""
import re
import traceback
import libvirt
from django.core.exceptions import PermissionDenied
@ -121,6 +122,27 @@ def public_error_text(exception):
return words(public_error(exception))
# not libvirt's error or a refusal (whose text is for users) but a fault of
# the panel: its text (a SQL error, a KeyError's key) tells users nothing
UNEXPECTED = gettext_noop("The panel failed unexpectedly; its log has the details.")
REPORTED = (libvirtError, PermissionDenied, Http404, ValueError)
def traceback_of(err):
"""A fault of the panel, also one a reported error wraps, goes to the log
with its traceback, without secrets as the message (exc_info would write
them); a reported error says enough."""
fault = err.__cause__ if isinstance(err, REPORTED) else err
if fault is None or isinstance(fault, REPORTED):
return ""
return "\n" + safe_text("".join(traceback.format_exception(err)))
def job_error(err):
"""What users read of a job's exception."""
return public_error(err) if isinstance(err, REPORTED) else say(UNEXPECTED)
def check_params(params):
if isinstance(params, dict):
for key, value in params.items():

View file

@ -28,6 +28,7 @@ from logs.models import Logs
from . import claims, jobs, utils
from .models import Claim, LeaseLost, Operation, acquire, fenced, renew
from .utils import job_error, traceback_of
logger = logging.getLogger(__name__)
@ -220,8 +221,8 @@ class Worker:
self.progress_asked.pop(op_id, None)
if (err := future.exception()) is not None:
# execute raised before it could finish the job itself
logger.error("job %s ended unfinished: %s", op_id, utils.safe_text(err))
self.recovery.inspect_now(op_id, utils.public_error(err))
logger.error("job %s ended unfinished: %s%s", op_id, utils.safe_text(err), traceback_of(err))
self.recovery.inspect_now(op_id, job_error(err))
def _concurrency(self):
return self.fixed_concurrency or utils.concurrency()
@ -357,13 +358,13 @@ def execute(worker, op_id):
except jobs.Cancelled:
jobs.finish(op, jobs.Outcome(Operation.CANCELLED), worker=worker)
except Exception as err:
logger.warning("job %s %s raised: %s", op.pk, op.kind, utils.safe_text(err))
logger.warning("job %s %s raised: %s%s", op.pk, op.kind, utils.safe_text(err), traceback_of(err))
try:
# jobs have no request: the error users see never names a host
if ctx.effects_started:
worker.recovery.inspect_now(op.pk, error=utils.public_error(err))
worker.recovery.inspect_now(op.pk, error=job_error(err))
else:
jobs.finish(op, jobs.Outcome(Operation.FAILED, error=utils.public_error(err)), worker=worker)
jobs.finish(op, jobs.Outcome(Operation.FAILED, error=job_error(err)), worker=worker)
except LeaseLost:
pass
finally:
@ -478,13 +479,13 @@ class Scheduler:
now = time.monotonic()
if now - self.failing_since.setdefault(op_id, now) >= RECOVERY_LIMIT:
self._finish(op, jobs.Outcome(Operation.UNCERTAIN, error=self._error(
op_id, [*utils.public_error(err), *utils.say(UNREACHABLE)])))
op_id, [*job_error(err), *utils.say(UNREACHABLE)])))
return
n = self.attempts.get(op_id, 0)
self.attempts[op_id] = n + 1
self.next_try[op_id] = now + BACKOFF[min(n, len(BACKOFF) - 1)]
self._phase(op_id, WAITING)
logger.warning("recovery of job %s waits: %s", op_id, utils.safe_text(err))
logger.warning("recovery of job %s waits: %s%s", op_id, utils.safe_text(err), traceback_of(err))
return
self.failing_since.pop(op_id, None)
if outcome.state == Operation.RUNNING:
@ -507,7 +508,7 @@ class Scheduler:
except LeaseLost:
self._done(op_id)
except Exception as err:
logger.error("recovery of job %s failed: %s", op_id, utils.safe_text(err))
logger.error("recovery of job %s failed: %s%s", op_id, utils.safe_text(err), traceback_of(err))
self.next_try[op_id] = time.monotonic() + BACKOFF[-1]
finally:
self.inflight.discard(op_id)