Skip to content

Commit

Permalink
bpo-31783: Fix a race condition creating workers during shutdown (pyt…
Browse files Browse the repository at this point in the history
…hon#13171)

* bpo-31783: Fix a race condition while creating workers during interpreter shutdown

* 📜🤖 Added by blurb_it.
  • Loading branch information
brianquinlan authored and DinoV committed Jan 14, 2020
1 parent acebc7e commit adbfdfc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Lib/concurrent/futures/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix race condition in ThreadPoolExecutor when worker threads are created during interpreter shutdown.

0 comments on commit adbfdfc

Please sign in to comment.