From 305c6bfdf489f566fcb4f9fbb8576df79fe26fb6 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sat, 20 Nov 2021 19:36:29 -0800 Subject: [PATCH] Add lock around running_count increment, decrement. I thought these assignments were atomic for int but they're not. --- voussoirkit/threadpool.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/voussoirkit/threadpool.py b/voussoirkit/threadpool.py index 7b1c803..cf76b70 100644 --- a/voussoirkit/threadpool.py +++ b/voussoirkit/threadpool.py @@ -92,12 +92,17 @@ class PooledThread: return NO_MORE_JOBS log.debug('%s is running job %s.', self, job) - self.pool._running_count += 1 + + with self.pool._running_count_lock: + self.pool._running_count += 1 + try: job.run() except BaseException: traceback.print_traceback() - self.pool._running_count -= 1 + + with self.pool._running_count_lock: + self.pool._running_count -= 1 def join(self) -> None: log.debug('%s is joining.', self) @@ -163,8 +168,11 @@ class ThreadPool: self._running_count = 0 self._result_queue = None self._pending_jobs = lazychain.LazyChain() + # Should be held while manipulating self._pending_jobs. self._job_manager_lock = threading.Lock() + # Should be held while manipulating self._running_count. + self._running_count_lock = threading.Lock() self._size = size self._threads = [PooledThread(pool=self) for x in range(size)]