diff options
author | Brian Quinlan <brian@sweetapp.com> | 2019-06-28 18:54:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-28 18:54:52 (GMT) |
commit | 242c26f53edb965e9808dd918089e664c0223407 (patch) | |
tree | 6fec44dfe5a6af889cbb54844e86e1d10ea33a2c /Lib/concurrent | |
parent | 29f609ed07aa7856741ff8b8b8b050369d0faf81 (diff) | |
download | cpython-242c26f53edb965e9808dd918089e664c0223407.zip cpython-242c26f53edb965e9808dd918089e664c0223407.tar.gz cpython-242c26f53edb965e9808dd918089e664c0223407.tar.bz2 |
bpo-31783: Fix a race condition creating workers during shutdown (#13171)
* bpo-31783: Fix a race condition while creating workers during interpreter shutdown
* 📜🤖 Added by blurb_it.
Diffstat (limited to 'Lib/concurrent')
-rw-r--r-- | Lib/concurrent/futures/thread.py | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/Lib/concurrent/futures/thread.py b/Lib/concurrent/futures/thread.py index d84b3aa..b89f8f2 100644 --- a/Lib/concurrent/futures/thread.py +++ b/Lib/concurrent/futures/thread.py @@ -29,10 +29,14 @@ import os _threads_queues = weakref.WeakKeyDictionary() _shutdown = False +# Lock that ensures that new workers are not created while the interpreter is +# shutting down. Must be held while mutating _threads_queues and _shutdown. +_global_shutdown_lock = threading.Lock() def _python_exit(): global _shutdown - _shutdown = True + with _global_shutdown_lock: + _shutdown = True items = list(_threads_queues.items()) for t, q in items: q.put(None) @@ -156,7 +160,7 @@ class ThreadPoolExecutor(_base.Executor): self._initargs = initargs def submit(self, fn, /, *args, **kwargs): - with self._shutdown_lock: + with self._shutdown_lock, _global_shutdown_lock: if self._broken: raise BrokenThreadPool(self._broken) |