Skip to content

Commit 6bd49e8

Browse files
committed
Fix killing greenlets gevent exception
1 parent ddbd5c7 commit 6bd49e8

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

src/Debug/Debug.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66

77
# Non fatal exception
88
class Notify(Exception):
9-
def __init__(self, message):
10-
self.message = message
9+
def __init__(self, message=None):
10+
if message:
11+
self.message = message
1112

1213
def __str__(self):
1314
return self.message
1415

1516

17+
# Gevent greenlet.kill accept Exception type
18+
def createNotifyType(message):
19+
return type("Notify", (Notify, ), {"message": message})
20+
21+
1622
def formatExceptionMessage(err):
1723
err_type = err.__class__.__name__
1824
if err.args:
@@ -101,6 +107,8 @@ def formatStack(limit=None):
101107

102108

103109
num_block = 0
110+
111+
104112
def testBlock():
105113
global num_block
106114
logging.debug("Gevent block checker started")
@@ -111,6 +119,8 @@ def testBlock():
111119
logging.debug("Gevent block detected: %.3fs" % (time.time() - last_time - 1))
112120
num_block += 1
113121
last_time = time.time()
122+
123+
114124
gevent.spawn(testBlock)
115125

116126

src/Worker/Worker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,14 @@ def start(self):
226226
def skip(self, reason="Unknown"):
227227
self.manager.log.debug("%s: Force skipping (reason: %s)" % (self.key, reason))
228228
if self.thread:
229-
self.thread.kill(exception=Debug.Notify("Worker skipping (reason: %s)" % reason))
229+
self.thread.kill(exception=Debug.createNotifyType("Worker skipping (reason: %s)" % reason))
230230
self.start()
231231

232232
# Force stop the worker
233233
def stop(self, reason="Unknown"):
234234
self.manager.log.debug("%s: Force stopping (reason: %s)" % (self.key, reason))
235235
self.running = False
236236
if self.thread:
237-
self.thread.kill(exception=Debug.Notify("Worker stopped (reason: %s)" % reason))
237+
self.thread.kill(exception=Debug.createNotifyType("Worker stopped (reason: %s)" % reason))
238238
del self.thread
239239
self.manager.removeWorker(self)

src/util/GreenletManager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ def spawn(self, *args, **kwargs):
2020

2121
def stopGreenlets(self, reason="Stopping all greenlets"):
2222
num = len(self.greenlets)
23-
gevent.killall(list(self.greenlets), Debug.Notify(reason), block=False)
23+
gevent.killall(list(self.greenlets), Debug.createNotifyType(reason), block=False)
2424
return num

0 commit comments

Comments
 (0)