Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Lib/concurrent/futures/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_concurrent_futures/test_as_completed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 22 additions & 3 deletions Lib/test/test_concurrent_futures/test_wait.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading