@@ -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
0 commit comments