mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00
gh-95166: cancel map waited on future on timeout (GH-95169) (GH-95364)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
(cherry picked from commit e16d4ed590
)
Co-authored-by: Thomas Grainger <tagrain@gmail.com>
This commit is contained in:
parent
123079241d
commit
763801aae2
3 changed files with 42 additions and 2 deletions
|
@ -931,6 +931,33 @@ class ThreadPoolExecutorTest(ThreadPoolMixin, ExecutorTest, BaseTestCase):
|
|||
with futures.ProcessPoolExecutor(1, mp_context=mp.get_context('fork')) as workers:
|
||||
workers.submit(tuple)
|
||||
|
||||
def test_executor_map_current_future_cancel(self):
|
||||
stop_event = threading.Event()
|
||||
log = []
|
||||
|
||||
def log_n_wait(ident):
|
||||
log.append(f"{ident=} started")
|
||||
try:
|
||||
stop_event.wait()
|
||||
finally:
|
||||
log.append(f"{ident=} stopped")
|
||||
|
||||
with self.executor_type(max_workers=1) as pool:
|
||||
# submit work to saturate the pool
|
||||
fut = pool.submit(log_n_wait, ident="first")
|
||||
try:
|
||||
with contextlib.closing(
|
||||
pool.map(log_n_wait, ["second", "third"], timeout=0)
|
||||
) as gen:
|
||||
with self.assertRaises(TimeoutError):
|
||||
next(gen)
|
||||
finally:
|
||||
stop_event.set()
|
||||
fut.result()
|
||||
# ident='second' is cancelled as a result of raising a TimeoutError
|
||||
# ident='third' is cancelled because it remained in the collection of futures
|
||||
self.assertListEqual(log, ["ident='first' started", "ident='first' stopped"])
|
||||
|
||||
|
||||
class ProcessPoolExecutorTest(ExecutorTest):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue