From 17a346b023554f48ff72f4e52decea002e311e81 Mon Sep 17 00:00:00 2001 From: Thomas Herve Date: Wed, 8 Feb 2017 17:20:04 +0100 Subject: [PATCH] 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 e371821a0183db3eb325ea4f3c54ba01664285ab) --- oslo_service/threadgroup.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/oslo_service/threadgroup.py b/oslo_service/threadgroup.py index 2f947ef8..9c8ebdad 100644 --- a/oslo_service/threadgroup.py +++ b/oslo_service/threadgroup.py @@ -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):