From 99832e2f42245672a12deb978879f88e7ea6328c Mon Sep 17 00:00:00 2001 From: Omer Lachish Date: Thu, 20 Feb 2020 13:08:03 +0000 Subject: [PATCH 1/2] forward timeout SIGALRMs to MySQL threads in order to kill any running proccesses --- redash/query_runner/mysql.py | 8 ++++++++ redash/tasks/worker.py | 9 ++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/redash/query_runner/mysql.py b/redash/query_runner/mysql.py index 05552eef7f..3d2c3993a4 100644 --- a/redash/query_runner/mysql.py +++ b/redash/query_runner/mysql.py @@ -14,6 +14,7 @@ ) from redash.settings import parse_boolean from redash.utils import json_dumps, json_loads +from redash.tasks.worker import attach_to_timeout_signal, JobTimeoutException try: import MySQLdb @@ -150,11 +151,14 @@ def _get_tables(self, schema): return list(schema.values()) + def run_query(self, query, user): ev = threading.Event() thread_id = "" r = Result() t = None + attach_to_timeout_signal() + try: connection = self._connection() thread_id = connection.thread_id() @@ -164,6 +168,10 @@ def run_query(self, query, user): t.start() while not ev.wait(1): pass + except JobTimeoutException as e: + self._cancel(thread_id) + t.join() + raise e except (KeyboardInterrupt, InterruptException): error = self._cancel(thread_id) t.join() diff --git a/redash/tasks/worker.py b/redash/tasks/worker.py index 3f68351228..1e15565f98 100644 --- a/redash/tasks/worker.py +++ b/redash/tasks/worker.py @@ -4,7 +4,7 @@ import time from rq import Worker as BaseWorker, Queue as BaseQueue, get_current_job from rq.utils import utcnow -from rq.timeouts import UnixSignalDeathPenalty, HorseMonitorTimeoutException +from rq.timeouts import UnixSignalDeathPenalty, HorseMonitorTimeoutException, JobTimeoutException from rq.job import Job as BaseJob, JobStatus @@ -130,6 +130,13 @@ def monitor_work_horse(self, job): ) +def attach_to_timeout_signal(): + def cancel_job(signum, frame): + raise JobTimeoutException() + + signal.signal(signal.SIGALRM, cancel_job) + + Job = CancellableJob Queue = CancellableQueue Worker = HardLimitingWorker From 383efabfa74561ed8614ddcb5f0d874dc41fd619 Mon Sep 17 00:00:00 2001 From: Omer Lachish Date: Mon, 24 Feb 2020 20:02:30 +0000 Subject: [PATCH 2/2] no need to attach to SIGALRM as RQ already does that --- redash/query_runner/mysql.py | 3 +-- redash/tasks/worker.py | 7 ------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/redash/query_runner/mysql.py b/redash/query_runner/mysql.py index 3d2c3993a4..82f19394cf 100644 --- a/redash/query_runner/mysql.py +++ b/redash/query_runner/mysql.py @@ -14,7 +14,7 @@ ) from redash.settings import parse_boolean from redash.utils import json_dumps, json_loads -from redash.tasks.worker import attach_to_timeout_signal, JobTimeoutException +from redash.tasks.worker import JobTimeoutException try: import MySQLdb @@ -157,7 +157,6 @@ def run_query(self, query, user): thread_id = "" r = Result() t = None - attach_to_timeout_signal() try: connection = self._connection() diff --git a/redash/tasks/worker.py b/redash/tasks/worker.py index 1e15565f98..c74983afce 100644 --- a/redash/tasks/worker.py +++ b/redash/tasks/worker.py @@ -130,13 +130,6 @@ def monitor_work_horse(self, job): ) -def attach_to_timeout_signal(): - def cancel_job(signum, frame): - raise JobTimeoutException() - - signal.signal(signal.SIGALRM, cancel_job) - - Job = CancellableJob Queue = CancellableQueue Worker = HardLimitingWorker