From 6bd49e8aff9c438e59a6fffa1bd1bc66541eb4c8 Mon Sep 17 00:00:00 2001 From: shortcutme Date: Tue, 30 Jun 2020 17:04:47 +0200 Subject: [PATCH] Fix killing greenlets gevent exception --- src/Debug/Debug.py | 14 ++++++++++++-- src/Worker/Worker.py | 4 ++-- src/util/GreenletManager.py | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Debug/Debug.py b/src/Debug/Debug.py index 10a4d6277..12f084bc3 100644 --- a/src/Debug/Debug.py +++ b/src/Debug/Debug.py @@ -6,13 +6,19 @@ # Non fatal exception class Notify(Exception): - def __init__(self, message): - self.message = message + def __init__(self, message=None): + if message: + self.message = message def __str__(self): return self.message +# Gevent greenlet.kill accept Exception type +def createNotifyType(message): + return type("Notify", (Notify, ), {"message": message}) + + def formatExceptionMessage(err): err_type = err.__class__.__name__ if err.args: @@ -101,6 +107,8 @@ def formatStack(limit=None): num_block = 0 + + def testBlock(): global num_block logging.debug("Gevent block checker started") @@ -111,6 +119,8 @@ def testBlock(): logging.debug("Gevent block detected: %.3fs" % (time.time() - last_time - 1)) num_block += 1 last_time = time.time() + + gevent.spawn(testBlock) diff --git a/src/Worker/Worker.py b/src/Worker/Worker.py index 4cf04d975..b7111ba18 100644 --- a/src/Worker/Worker.py +++ b/src/Worker/Worker.py @@ -226,7 +226,7 @@ def start(self): def skip(self, reason="Unknown"): self.manager.log.debug("%s: Force skipping (reason: %s)" % (self.key, reason)) if self.thread: - self.thread.kill(exception=Debug.Notify("Worker skipping (reason: %s)" % reason)) + self.thread.kill(exception=Debug.createNotifyType("Worker skipping (reason: %s)" % reason)) self.start() # Force stop the worker @@ -234,6 +234,6 @@ def stop(self, reason="Unknown"): self.manager.log.debug("%s: Force stopping (reason: %s)" % (self.key, reason)) self.running = False if self.thread: - self.thread.kill(exception=Debug.Notify("Worker stopped (reason: %s)" % reason)) + self.thread.kill(exception=Debug.createNotifyType("Worker stopped (reason: %s)" % reason)) del self.thread self.manager.removeWorker(self) diff --git a/src/util/GreenletManager.py b/src/util/GreenletManager.py index 7245d05c7..e024233da 100644 --- a/src/util/GreenletManager.py +++ b/src/util/GreenletManager.py @@ -20,5 +20,5 @@ def spawn(self, *args, **kwargs): def stopGreenlets(self, reason="Stopping all greenlets"): num = len(self.greenlets) - gevent.killall(list(self.greenlets), Debug.Notify(reason), block=False) + gevent.killall(list(self.greenlets), Debug.createNotifyType(reason), block=False) return num