@@ -114,18 +114,20 @@ def log_n_wait(ident):
114114
115115 def test_shutdown_cancels_pending_futures (self ):
116116 # gh-109934: ensure shutdown cancels and notifies pending futures
117- def waiter (barrier ):
118- barrier .wait (3 )
117+ def waiter (b1 , b2 ):
118+ b1 .wait (3 )
119+ b2 .wait (3 )
119120 def noop ():
120121 pass
121- barrier = threading .Barrier (2 )
122+ b1 = threading .Barrier (2 )
123+ b2 = threading .Barrier (2 )
122124 called_back_1 = threading .Event ()
123125 called_back_2 = threading .Event ()
124126 with self .executor_type (max_workers = 1 ) as pool :
125127
126128 # Submit two futures, the first of which will block and prevent the
127129 # second from running
128- f1 = pool .submit (waiter , barrier )
130+ f1 = pool .submit (waiter , b1 , b2 )
129131 f2 = pool .submit (noop )
130132 f1 .add_done_callback (lambda f : called_back_1 .set ())
131133 f2 .add_done_callback (lambda f : called_back_2 .set ())
@@ -134,7 +136,9 @@ def noop():
134136 completed_iter = futures .as_completed (fs , timeout = 0 )
135137 self .assertRaises (TimeoutError , next , completed_iter )
136138
137- # Shutdown the pool, cancelling unstarted task
139+ # Ensure the first task has started running then shutdown the
140+ # pool, cancelling the unstarted task
141+ b1 .wait (3 )
138142 pool .shutdown (wait = False , cancel_futures = True )
139143 self .assertTrue (f1 .running ())
140144 self .assertTrue (f2 .cancelled ())
@@ -151,7 +155,7 @@ def noop():
151155 self .assertEqual (result .done , {f2 })
152156
153157 # Unblock and wait for the first future to complete
154- barrier .wait (3 )
158+ b2 .wait (3 )
155159 called_back_1 .wait (3 )
156160 self .assertTrue (f1 .done ())
157161 self .assertTrue (called_back_1 .is_set ())
0 commit comments