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
(cherry picked from commit e371821a01)
This commit is contained in:
Thomas Herve 2017-02-08 17:20:04 +01:00
parent e11a288077
commit 17a346b023
1 changed files with 5 additions and 3 deletions

View File

@ -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):