Skip to content

Commit 242c26f

Browse files
authored
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.
1 parent 29f609e commit 242c26f

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

Lib/concurrent/futures/thread.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929

3030
_threads_queues = weakref.WeakKeyDictionary()
3131
_shutdown = False
32+
# Lock that ensures that new workers are not created while the interpreter is
33+
# shutting down. Must be held while mutating _threads_queues and _shutdown.
34+
_global_shutdown_lock = threading.Lock()
3235

3336
def _python_exit():
3437
global _shutdown
35-
_shutdown = True
38+
with _global_shutdown_lock:
39+
_shutdown = True
3640
items = list(_threads_queues.items())
3741
for t, q in items:
3842
q.put(None)
@@ -156,7 +160,7 @@ def __init__(self, max_workers=None, thread_name_prefix='',
156160
self._initargs = initargs
157161

158162
def submit(self, fn, /, *args, **kwargs):
159-
with self._shutdown_lock:
163+
with self._shutdown_lock, _global_shutdown_lock:
160164
if self._broken:
161165
raise BrokenThreadPool(self._broken)
162166

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix race condition in ThreadPoolExecutor when worker threads are created during interpreter shutdown.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy