From 599700f1fb71c12132864fceeb93b2a22108a84e Mon Sep 17 00:00:00 2001 From: exllamav2 <224149905+exllamav2@users.noreply.github.com> Date: Fri, 4 Dec 2026 11:55:18 +0000 Subject: [PATCH] gh-92001: Fix wait() and as_completed() hanging on futures cancelled while pending --- Lib/concurrent/futures/_base.py | 6 ++--- .../test_as_completed.py | 10 ++++++++ Lib/test/test_concurrent_futures/test_wait.py | 25 ++++++++++++++++--- ...6-12-04-12-00-00.gh-issue-92001.fX93kL.rst | 5 ++++ 4 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-12-04-12-00-00.gh-issue-92001.fX93kL.rst diff --git a/Lib/concurrent/futures/_base.py b/Lib/concurrent/futures/_base.py index e4388bfa2e86626..aa3a29ac41ca437 100644 --- a/Lib/concurrent/futures/_base.py +++ b/Lib/concurrent/futures/_base.py @@ -153,7 +153,7 @@ def _create_and_install_waiters(fs, return_when): waiter = _FirstCompletedWaiter() else: pending_count = sum( - f._state not in [CANCELLED_AND_NOTIFIED, FINISHED] for f in fs) + f._state not in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED] for f in fs) if return_when == FIRST_EXCEPTION: waiter = _AllCompletedWaiter(pending_count, stop_on_exception=True) @@ -216,7 +216,7 @@ def as_completed(fs, timeout=None): with _AcquireFutures(fs): finished = set( f for f in fs - if f._state in [CANCELLED_AND_NOTIFIED, FINISHED]) + if f._state in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED]) pending = fs - finished waiter = _create_and_install_waiters(fs, _AS_COMPLETED) finished = list(finished) @@ -283,7 +283,7 @@ def wait(fs, timeout=None, return_when=ALL_COMPLETED): fs = set(fs) with _AcquireFutures(fs): done = {f for f in fs - if f._state in [CANCELLED_AND_NOTIFIED, FINISHED]} + if f._state in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED]} not_done = fs - done if (return_when == FIRST_COMPLETED) and done: return DoneAndNotDoneFutures(done, not_done) diff --git a/Lib/test/test_concurrent_futures/test_as_completed.py b/Lib/test/test_concurrent_futures/test_as_completed.py index 31c7bb3ebd872cb..5b78e157a5b6a7e 100644 --- a/Lib/test/test_concurrent_futures/test_as_completed.py +++ b/Lib/test/test_concurrent_futures/test_as_completed.py @@ -20,6 +20,16 @@ def mul(x, y): class AsCompletedTests: + def test_cancelled_pending_future_yields(self): + # gh-92001: a future in state CANCELLED is complete for + # as_completed(), even if no executor has processed (and notified) + # the cancellation yet. + future = futures.Future() + self.assertTrue(future.cancel()) + + yielded = list(futures.as_completed([future], timeout=10)) + self.assertEqual([future], yielded) + @warnings_helper.ignore_fork_in_thread_deprecation_warnings() def test_no_timeout(self): future1 = self.executor.submit(mul, 2, 21) diff --git a/Lib/test/test_concurrent_futures/test_wait.py b/Lib/test/test_concurrent_futures/test_wait.py index b8250cec7ab3e7c..26c50b175375f32 100644 --- a/Lib/test/test_concurrent_futures/test_wait.py +++ b/Lib/test/test_concurrent_futures/test_wait.py @@ -42,8 +42,11 @@ def test_first_completed(self): [CANCELLED_FUTURE, future1, future2], return_when=futures.FIRST_COMPLETED) - self.assertEqual(set([future1]), done) - self.assertEqual(set([CANCELLED_FUTURE, future2]), not_done) + # CANCELLED_FUTURE is already done when wait() is entered, so + # wait() may return before future1 finishes; future2 is the only + # future that cannot be done yet. + self.assertIn(CANCELLED_FUTURE, done) + self.assertIn(future2, not_done) finally: event.set() future2.result() # wait for job to finish @@ -111,9 +114,10 @@ def test_first_exception_some_already_complete(self): return_when=futures.FIRST_EXCEPTION) self.assertEqual(set([SUCCESSFUL_FUTURE, + CANCELLED_FUTURE, CANCELLED_AND_NOTIFIED_FUTURE, future1]), finished) - self.assertEqual(set([CANCELLED_FUTURE, future2]), pending) + self.assertEqual(set([future2]), pending) finally: event.set() future2.result() # wait for job to finish @@ -154,6 +158,21 @@ def test_all_completed(self): future2]), finished) self.assertEqual(set(), pending) + def test_cancelled_pending_future_is_done(self): + # gh-92001: a future in state CANCELLED is done, even if no executor + # has processed (and notified) the cancellation yet. + future = futures.Future() + self.assertTrue(future.cancel()) + + done, not_done = futures.wait([future], timeout=10) + self.assertEqual(set([future]), done) + self.assertEqual(set(), not_done) + + done, not_done = futures.wait( + [future], return_when=futures.FIRST_COMPLETED, timeout=10) + self.assertEqual(set([future]), done) + self.assertEqual(set(), not_done) + @warnings_helper.ignore_fork_in_thread_deprecation_warnings() def test_timeout(self): short_timeout = 0.050 diff --git a/Misc/NEWS.d/next/Library/2026-12-04-12-00-00.gh-issue-92001.fX93kL.rst b/Misc/NEWS.d/next/Library/2026-12-04-12-00-00.gh-issue-92001.fX93kL.rst new file mode 100644 index 000000000000000..6dfbc5fcc52bcd1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-12-04-12-00-00.gh-issue-92001.fX93kL.rst @@ -0,0 +1,5 @@ +Fix :func:`concurrent.futures.wait` and :func:`concurrent.futures.as_completed` +indefinitely waiting on futures that were cancelled while still pending: a +future in state ``CANCELLED`` now counts as done, consistently with +:meth:`concurrent.futures.Future.done`, instead of only once the executor has +processed the cancellation.