]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-94440: Fix issue of ProcessPoolExecutor shutdown hanging (GH-94468)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 16 Mar 2023 05:09:20 +0000 (22:09 -0700)
committerGitHub <noreply@github.com>
Thu, 16 Mar 2023 05:09:20 +0000 (22:09 -0700)
Fix an issue of concurrent.futures ProcessPoolExecutor shutdown hanging.

(cherry picked from commit 2dc94634b50f0e5e207787e5ac1d56c68b22c3ae)

Co-authored-by: yonatanp <yonatan.perry@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Lib/concurrent/futures/process.py
Lib/test/test_concurrent_futures.py
Misc/ACKS
Misc/NEWS.d/next/Library/2022-06-30-21-28-41.gh-issue-94440.LtgX0d.rst [new file with mode: 0644]

index 7e2f5fa30e8264132d97c5a72a04d594b83cd6f8..392d1960e0595022102590152716648c33e1f89d 100644 (file)
@@ -364,6 +364,11 @@ class _ExecutorManagerThread(threading.Thread):
             if self.is_shutting_down():
                 self.flag_executor_shutting_down()
 
+                # When only canceled futures remain in pending_work_items, our
+                # next call to wait_result_broken_or_wakeup would hang forever.
+                # This makes sure we have some running futures or none at all.
+                self.add_call_item_to_queue()
+
                 # Since no new work items can be added, it is safe to shutdown
                 # this thread if there are no pending work items.
                 if not self.pending_work_items:
index 02c2fa06fad4eb22f94a4cf8b0ded47693f62368..1bb6156a18ddbfb452f3a005f316e9984e9efaa6 100644 (file)
@@ -14,6 +14,7 @@ import logging
 from logging.handlers import QueueHandler
 import os
 import queue
+import signal
 import sys
 import threading
 import time
@@ -396,6 +397,33 @@ class ExecutorShutdownTest:
         self.assertFalse(err)
         self.assertEqual(out.strip(), b"apple")
 
+    def test_hang_gh94440(self):
+        """shutdown(wait=True) doesn't hang when a future was submitted and
+        quickly canceled right before shutdown.
+
+        See https://github.com/python/cpython/issues/94440.
+        """
+        if not hasattr(signal, 'alarm'):
+            raise unittest.SkipTest(
+                "Tested platform does not support the alarm signal")
+
+        def timeout(_signum, _frame):
+            raise RuntimeError("timed out waiting for shutdown")
+
+        kwargs = {}
+        if getattr(self, 'ctx', None):
+            kwargs['mp_context'] = self.get_context()
+        executor = self.executor_type(max_workers=1, **kwargs)
+        executor.submit(int).result()
+        old_handler = signal.signal(signal.SIGALRM, timeout)
+        try:
+            signal.alarm(5)
+            executor.submit(int).cancel()
+            executor.shutdown(wait=True)
+        finally:
+            signal.alarm(0)
+            signal.signal(signal.SIGALRM, old_handler)
+
 
 class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest, BaseTestCase):
     def test_threads_terminate(self):
index afe86d37b0a707ca3fc0215f74cbe40ea862ebcd..dbcff1e15435e35faf32c08ed20eeb5462356fe6 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1372,6 +1372,7 @@ Thomas Perl
 Mathieu Perreault
 Mark Perrego
 Trevor Perrin
+Yonatan Perry
 Gabriel de Perthuis
 Tim Peters
 Benjamin Peterson
diff --git a/Misc/NEWS.d/next/Library/2022-06-30-21-28-41.gh-issue-94440.LtgX0d.rst b/Misc/NEWS.d/next/Library/2022-06-30-21-28-41.gh-issue-94440.LtgX0d.rst
new file mode 100644 (file)
index 0000000..3eee82e
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a :mod:`concurrent.futures.process` bug where ``ProcessPoolExecutor`` shutdown
+could hang after a future has been quickly submitted and canceled.