]> granicus.if.org Git - python/commitdiff
bpo-31783: Fix a race condition creating workers during shutdown (#13171)
authorBrian Quinlan <brian@sweetapp.com>
Fri, 28 Jun 2019 18:54:52 +0000 (11:54 -0700)
committerGitHub <noreply@github.com>
Fri, 28 Jun 2019 18:54:52 +0000 (11:54 -0700)
* bpo-31783: Fix a race condition while creating workers during interpreter shutdown

* ðŸ“œðŸ¤– Added by blurb_it.

Lib/concurrent/futures/thread.py
Misc/NEWS.d/next/Library/2019-05-07-19-25-55.bpo-31783.lgLo69.rst [new file with mode: 0644]

index d84b3aa7da0c2ea95682f348b6dc8a66d133fc5b..b89f8f24d4d6738fb0e7b81229ba3de6a914ab34 100644 (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)
 
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 (file)
index 0000000..a261e59
--- /dev/null
@@ -0,0 +1 @@
+Fix race condition in ThreadPoolExecutor when worker threads are created during interpreter shutdown.
\ No newline at end of file