From 799d25c3be206b5313b4d7f92cabd53f2ddbe447 Mon Sep 17 00:00:00 2001 From: Yonatan Perry Date: Thu, 30 Jun 2022 17:15:56 -0400 Subject: [PATCH 1/6] gh-94440: Fix issue of ProcessPoolExecutor shutdown hanging --- Lib/concurrent/futures/process.py | 5 +++++ Lib/test/test_concurrent_futures.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/Lib/concurrent/futures/process.py b/Lib/concurrent/futures/process.py index 7e2f5fa30e8264..d4f65cb66116c0 100644 --- a/Lib/concurrent/futures/process.py +++ b/Lib/concurrent/futures/process.py @@ -364,6 +364,11 @@ def run(self): if self.is_shutting_down(): self.flag_executor_shutting_down() + # If only canceled futures remain in pending_work_items, we + # should purge them now to avoid waiting forever in our + # subsequent call to wait_result_broken_or_wakeup. + 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: diff --git a/Lib/test/test_concurrent_futures.py b/Lib/test/test_concurrent_futures.py index f50255bd575601..cae37d449ce448 100644 --- a/Lib/test/test_concurrent_futures.py +++ b/Lib/test/test_concurrent_futures.py @@ -14,6 +14,7 @@ from logging.handlers import QueueHandler import os import queue +import signal import sys import threading import time @@ -397,6 +398,33 @@ def test_hang_gh83386(self): 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): From 1f444b2a967041677f75fbfd71c9187c31863337 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 30 Jun 2022 21:28:42 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-06-30-21-28-41.gh-issue-94440.LtgX0d.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2022-06-30-21-28-41.gh-issue-94440.LtgX0d.rst 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 index 00000000000000..62a89e9f51d881 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-06-30-21-28-41.gh-issue-94440.LtgX0d.rst @@ -0,0 +1,3 @@ +Fixed ``concurrent.futures.process._ExecutorManagerThread`` to flush canceled +futures before attempting to shutdown, so that it doesn't hang if all that +remained in its pending work items are canceled futures. From 5e88846c32da51a33f11ad53e5b82ca21f6a0a87 Mon Sep 17 00:00:00 2001 From: Yonatan Perry Date: Thu, 30 Jun 2022 18:57:35 -0400 Subject: [PATCH 3/6] gh-94440: Fix issue of ProcessPoolExecutor shutdown hanging - documentation enhancement --- Lib/concurrent/futures/process.py | 6 +++--- Misc/ACKS | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/concurrent/futures/process.py b/Lib/concurrent/futures/process.py index d4f65cb66116c0..392d1960e05950 100644 --- a/Lib/concurrent/futures/process.py +++ b/Lib/concurrent/futures/process.py @@ -364,9 +364,9 @@ def run(self): if self.is_shutting_down(): self.flag_executor_shutting_down() - # If only canceled futures remain in pending_work_items, we - # should purge them now to avoid waiting forever in our - # subsequent call to wait_result_broken_or_wakeup. + # 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 diff --git a/Misc/ACKS b/Misc/ACKS index b6340414cf7012..18830ee63db055 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1367,6 +1367,7 @@ Thomas Perl Mathieu Perreault Mark Perrego Trevor Perrin +Yonatan Perry Gabriel de Perthuis Tim Peters Benjamin Peterson From 061ccfb8b3677cc743654f9f79587d244215e4c3 Mon Sep 17 00:00:00 2001 From: Yonatan Perry Date: Wed, 15 Mar 2023 12:23:54 -0400 Subject: [PATCH 4/6] rephrase news to describe user impact --- .../Library/2022-06-30-21-28-41.gh-issue-94440.LtgX0d.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 index 62a89e9f51d881..3eee82e59dfafb 100644 --- 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 @@ -1,3 +1,2 @@ -Fixed ``concurrent.futures.process._ExecutorManagerThread`` to flush canceled -futures before attempting to shutdown, so that it doesn't hang if all that -remained in its pending work items are canceled futures. +Fix a :mod:`concurrent.futures.process` bug where ``ProcessPoolExecutor`` shutdown +could hang after a future has been quickly submitted and canceled. From b69bc4cbaeab9b14dd38f848cee8228db3076807 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 15 Mar 2023 16:34:07 +0000 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-03-15-16-34-05.gh-issue-94440.RnJ6Lq.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-03-15-16-34-05.gh-issue-94440.RnJ6Lq.rst diff --git a/Misc/NEWS.d/next/Library/2023-03-15-16-34-05.gh-issue-94440.RnJ6Lq.rst b/Misc/NEWS.d/next/Library/2023-03-15-16-34-05.gh-issue-94440.RnJ6Lq.rst new file mode 100644 index 00000000000000..e8e88ca0cfcb0f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-03-15-16-34-05.gh-issue-94440.RnJ6Lq.rst @@ -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. From 46d1d483487c63bfc7189d26bd213d4d001c4e0f Mon Sep 17 00:00:00 2001 From: Yonatan Perry Date: Wed, 15 Mar 2023 12:39:01 -0400 Subject: [PATCH 6/6] remove accidental extra news item created with blurb-it bot --- .../next/Library/2023-03-15-16-34-05.gh-issue-94440.RnJ6Lq.rst | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 Misc/NEWS.d/next/Library/2023-03-15-16-34-05.gh-issue-94440.RnJ6Lq.rst diff --git a/Misc/NEWS.d/next/Library/2023-03-15-16-34-05.gh-issue-94440.RnJ6Lq.rst b/Misc/NEWS.d/next/Library/2023-03-15-16-34-05.gh-issue-94440.RnJ6Lq.rst deleted file mode 100644 index e8e88ca0cfcb0f..00000000000000 --- a/Misc/NEWS.d/next/Library/2023-03-15-16-34-05.gh-issue-94440.RnJ6Lq.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix a :mod:`concurrent.futures.process` bug where ``ProcessPoolExecutor`` shutdown -could hang after a future has been quickly submitted and canceled.