Skip to content

Commit 0560fe3

Browse files
miss-islingtonyonatanpAlexWaygood
authored
gh-94440: Fix issue of ProcessPoolExecutor shutdown hanging (GH-94468)
Fix an issue of concurrent.futures ProcessPoolExecutor shutdown hanging. (cherry picked from commit 2dc9463) Co-authored-by: yonatanp <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
1 parent 3ddf397 commit 0560fe3

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

Lib/concurrent/futures/process.py

+5
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ def run(self):
364364
if self.is_shutting_down():
365365
self.flag_executor_shutting_down()
366366

367+
# When only canceled futures remain in pending_work_items, our
368+
# next call to wait_result_broken_or_wakeup would hang forever.
369+
# This makes sure we have some running futures or none at all.
370+
self.add_call_item_to_queue()
371+
367372
# Since no new work items can be added, it is safe to shutdown
368373
# this thread if there are no pending work items.
369374
if not self.pending_work_items:

Lib/test/test_concurrent_futures.py

+28
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from logging.handlers import QueueHandler
1515
import os
1616
import queue
17+
import signal
1718
import sys
1819
import threading
1920
import time
@@ -396,6 +397,33 @@ def test_hang_gh83386(self):
396397
self.assertFalse(err)
397398
self.assertEqual(out.strip(), b"apple")
398399

400+
def test_hang_gh94440(self):
401+
"""shutdown(wait=True) doesn't hang when a future was submitted and
402+
quickly canceled right before shutdown.
403+
404+
See https://github.com/python/cpython/issues/94440.
405+
"""
406+
if not hasattr(signal, 'alarm'):
407+
raise unittest.SkipTest(
408+
"Tested platform does not support the alarm signal")
409+
410+
def timeout(_signum, _frame):
411+
raise RuntimeError("timed out waiting for shutdown")
412+
413+
kwargs = {}
414+
if getattr(self, 'ctx', None):
415+
kwargs['mp_context'] = self.get_context()
416+
executor = self.executor_type(max_workers=1, **kwargs)
417+
executor.submit(int).result()
418+
old_handler = signal.signal(signal.SIGALRM, timeout)
419+
try:
420+
signal.alarm(5)
421+
executor.submit(int).cancel()
422+
executor.shutdown(wait=True)
423+
finally:
424+
signal.alarm(0)
425+
signal.signal(signal.SIGALRM, old_handler)
426+
399427

400428
class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest, BaseTestCase):
401429
def test_threads_terminate(self):

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,7 @@ Thomas Perl
13721372
Mathieu Perreault
13731373
Mark Perrego
13741374
Trevor Perrin
1375+
Yonatan Perry
13751376
Gabriel de Perthuis
13761377
Tim Peters
13771378
Benjamin Peterson
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a :mod:`concurrent.futures.process` bug where ``ProcessPoolExecutor`` shutdown
2+
could hang after a future has been quickly submitted and canceled.

0 commit comments

Comments
 (0)