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.
This commit is contained in:
Brian Quinlan 2019-06-28 11:54:52 -07:00 committed by GitHub
parent 29f609ed07
commit 242c26f53e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View File

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

View File

@ -0,0 +1 @@
Fix race condition in ThreadPoolExecutor when worker threads are created during interpreter shutdown.