[Python-checkins] bpo-31783: Fix a race condition creating workers during shutdown (#13171)

Brian Quinlan webhook-mailer at python.org
Fri Jun 28 14:54:59 EDT 2019


https://github.com/python/cpython/commit/242c26f53edb965e9808dd918089e664c0223407
commit: 242c26f53edb965e9808dd918089e664c0223407
branch: master
author: Brian Quinlan <brian at sweetapp.com>
committer: GitHub <noreply at github.com>
date: 2019-06-28T11:54:52-07:00
summary:

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.

files:
A Misc/NEWS.d/next/Library/2019-05-07-19-25-55.bpo-31783.lgLo69.rst
M Lib/concurrent/futures/thread.py

diff --git a/Lib/concurrent/futures/thread.py b/Lib/concurrent/futures/thread.py
index d84b3aa7da0c..b89f8f24d4d6 100644
--- a/Lib/concurrent/futures/thread.py
+++ b/Lib/concurrent/futures/thread.py
@@ -29,10 +29,14 @@
 
 _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 @@ def __init__(self, max_workers=None, thread_name_prefix='',
         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)
 
diff --git a/Misc/NEWS.d/next/Library/2019-05-07-19-25-55.bpo-31783.lgLo69.rst b/Misc/NEWS.d/next/Library/2019-05-07-19-25-55.bpo-31783.lgLo69.rst
new file mode 100644
index 000000000000..a261e59ccfec
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-05-07-19-25-55.bpo-31783.lgLo69.rst
@@ -0,0 +1 @@
+Fix race condition in ThreadPoolExecutor when worker threads are created during interpreter shutdown.
\ No newline at end of file



More information about the Python-checkins mailing list