From cf4f24752af71788d0393452b2f5bb1ddc5bc7d0 Mon Sep 17 00:00:00 2001 From: Trenton Holmes Date: Wed, 31 Aug 2022 16:03:33 -0700 Subject: [PATCH] Inherit the timeout from a lower leverl exception, so user code is less likely to catch it --- django_q/cluster.py | 13 +++---------- django_q/timeouts.py | 5 +++-- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/django_q/cluster.py b/django_q/cluster.py index f3ee2adf..b85726ec 100644 --- a/django_q/cluster.py +++ b/django_q/cluster.py @@ -443,17 +443,10 @@ def worker( with UnixSignalDeathPenalty(timeout=timeout): res = f(*task["args"], **task["kwargs"]) result = (res, True) - except Exception as e: - # Minor QoL, parse the exception chain as far as - # possible to check if this was a timeout or just an error - exception_chain = [e] - next_exception = e.__cause__ - while next_exception is not None: - exception_chain.append(next_exception) - next_exception = next_exception.__cause__ - if any(isinstance(x, JobTimeoutException) for x in exception_chain): - timed_out = True + except (JobTimeoutException, Exception) as e: result = (f"{e} : {traceback.format_exc()}", False) + if isinstance(e, JobTimeoutException): + timed_out = True if error_reporter: error_reporter.report() if task.get("sync", False): diff --git a/django_q/timeouts.py b/django_q/timeouts.py index 7d4ce378..77251183 100644 --- a/django_q/timeouts.py +++ b/django_q/timeouts.py @@ -5,9 +5,10 @@ import signal -class JobTimeoutException(Exception): +class JobTimeoutException(SystemExit): """Raised when a job takes longer to complete than the allowed maximum - timeout value. + timeout value. Inherits from SystemExit to prevent user code which catches + Exception from not timing out correctly. """ pass