Skip to content

Commit 599700f

Browse files
committed
gh-92001: Fix wait() and as_completed() hanging on futures cancelled while pending
1 parent 5b6dd1e commit 599700f

4 files changed

Lines changed: 40 additions & 6 deletions

File tree

‎Lib/concurrent/futures/_base.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def _create_and_install_waiters(fs, return_when):
153153
waiter = _FirstCompletedWaiter()
154154
else:
155155
pending_count = sum(
156-
f._state not in [CANCELLED_AND_NOTIFIED, FINISHED] for f in fs)
156+
f._state not in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED] for f in fs)
157157

158158
if return_when == FIRST_EXCEPTION:
159159
waiter = _AllCompletedWaiter(pending_count, stop_on_exception=True)
@@ -216,7 +216,7 @@ def as_completed(fs, timeout=None):
216216
with _AcquireFutures(fs):
217217
finished = set(
218218
f for f in fs
219-
if f._state in [CANCELLED_AND_NOTIFIED, FINISHED])
219+
if f._state in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED])
220220
pending = fs - finished
221221
waiter = _create_and_install_waiters(fs, _AS_COMPLETED)
222222
finished = list(finished)
@@ -283,7 +283,7 @@ def wait(fs, timeout=None, return_when=ALL_COMPLETED):
283283
fs = set(fs)
284284
with _AcquireFutures(fs):
285285
done = {f for f in fs
286-
if f._state in [CANCELLED_AND_NOTIFIED, FINISHED]}
286+
if f._state in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED]}
287287
not_done = fs - done
288288
if (return_when == FIRST_COMPLETED) and done:
289289
return DoneAndNotDoneFutures(done, not_done)

‎Lib/test/test_concurrent_futures/test_as_completed.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ def mul(x, y):
2020

2121

2222
class AsCompletedTests:
23+
def test_cancelled_pending_future_yields(self):
24+
# gh-92001: a future in state CANCELLED is complete for
25+
# as_completed(), even if no executor has processed (and notified)
26+
# the cancellation yet.
27+
future = futures.Future()
28+
self.assertTrue(future.cancel())
29+
30+
yielded = list(futures.as_completed([future], timeout=10))
31+
self.assertEqual([future], yielded)
32+
2333
@warnings_helper.ignore_fork_in_thread_deprecation_warnings()
2434
def test_no_timeout(self):
2535
future1 = self.executor.submit(mul, 2, 21)

‎Lib/test/test_concurrent_futures/test_wait.py‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ def test_first_completed(self):
4242
[CANCELLED_FUTURE, future1, future2],
4343
return_when=futures.FIRST_COMPLETED)
4444

45-
self.assertEqual(set([future1]), done)
46-
self.assertEqual(set([CANCELLED_FUTURE, future2]), not_done)
45+
# CANCELLED_FUTURE is already done when wait() is entered, so
46+
# wait() may return before future1 finishes; future2 is the only
47+
# future that cannot be done yet.
48+
self.assertIn(CANCELLED_FUTURE, done)
49+
self.assertIn(future2, not_done)
4750
finally:
4851
event.set()
4952
future2.result() # wait for job to finish
@@ -111,9 +114,10 @@ def test_first_exception_some_already_complete(self):
111114
return_when=futures.FIRST_EXCEPTION)
112115

113116
self.assertEqual(set([SUCCESSFUL_FUTURE,
117+
CANCELLED_FUTURE,
114118
CANCELLED_AND_NOTIFIED_FUTURE,
115119
future1]), finished)
116-
self.assertEqual(set([CANCELLED_FUTURE, future2]), pending)
120+
self.assertEqual(set([future2]), pending)
117121
finally:
118122
event.set()
119123
future2.result() # wait for job to finish
@@ -154,6 +158,21 @@ def test_all_completed(self):
154158
future2]), finished)
155159
self.assertEqual(set(), pending)
156160

161+
def test_cancelled_pending_future_is_done(self):
162+
# gh-92001: a future in state CANCELLED is done, even if no executor
163+
# has processed (and notified) the cancellation yet.
164+
future = futures.Future()
165+
self.assertTrue(future.cancel())
166+
167+
done, not_done = futures.wait([future], timeout=10)
168+
self.assertEqual(set([future]), done)
169+
self.assertEqual(set(), not_done)
170+
171+
done, not_done = futures.wait(
172+
[future], return_when=futures.FIRST_COMPLETED, timeout=10)
173+
self.assertEqual(set([future]), done)
174+
self.assertEqual(set(), not_done)
175+
157176
@warnings_helper.ignore_fork_in_thread_deprecation_warnings()
158177
def test_timeout(self):
159178
short_timeout = 0.050
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix :func:`concurrent.futures.wait` and :func:`concurrent.futures.as_completed`
2+
indefinitely waiting on futures that were cancelled while still pending: a
3+
future in state ``CANCELLED`` now counts as done, consistently with
4+
:meth:`concurrent.futures.Future.done`, instead of only once the executor has
5+
processed the cancellation.

0 commit comments

Comments
 (0)