Fix race condition with fast threads
When a green thread dies quickly, it will call the linked callbacks on the link call synchronously. In this case, an error will happen when ThreadGroup is used, as the thread is not accounted for until the link is done. This manages this race condition by reverting the operations to link after. Change-Id: Iccf6edb0dfddda54552bd8787f64da84486061b7 Closes-Bug: #1662939
This commit is contained in:
parent
e801066c62
commit
e371821a01
|
@ -41,9 +41,10 @@ class Thread(object):
|
|||
the :class:`ThreadGroup` when it has done so it can be removed from
|
||||
the threads list.
|
||||
"""
|
||||
def __init__(self, thread, group):
|
||||
def __init__(self, thread, group, link=True):
|
||||
self.thread = thread
|
||||
self.thread.link(_on_thread_done, group, self)
|
||||
if link:
|
||||
self.thread.link(_on_thread_done, group, self)
|
||||
self._ident = id(thread)
|
||||
|
||||
@property
|
||||
|
@ -93,8 +94,9 @@ class ThreadGroup(object):
|
|||
|
||||
def add_thread(self, callback, *args, **kwargs):
|
||||
gt = self.pool.spawn(callback, *args, **kwargs)
|
||||
th = Thread(gt, self)
|
||||
th = Thread(gt, self, link=False)
|
||||
self.threads.append(th)
|
||||
gt.link(_on_thread_done, self, th)
|
||||
return th
|
||||
|
||||
def thread_done(self, thread):
|
||||
|
|
Loading…
Reference in New Issue