Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Job timeout doesn't kill the mysql query #4629

Merged
merged 2 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions redash/query_runner/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
9 changes: 8 additions & 1 deletion redash/tasks/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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