Skip to content

Commit

Permalink
Fix killing greenlets gevent exception
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcutme committed Jun 30, 2020
1 parent ddbd5c7 commit 6bd49e8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/Debug/Debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -101,6 +107,8 @@ def formatStack(limit=None):


num_block = 0


def testBlock():
global num_block
logging.debug("Gevent block checker started")
Expand All @@ -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)


Expand Down
4 changes: 2 additions & 2 deletions src/Worker/Worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,14 @@ 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
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)
2 changes: 1 addition & 1 deletion src/util/GreenletManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6bd49e8

Please sign in to comment.