Skip to content

Commit 7660179

Browse files
Spec change: when cancelled, always raise CancelledError.
This means swallowing the inner future's result / exception, if they are set simultaneously with the external cancellation.
1 parent c834122 commit 7660179

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

Lib/asyncio/tasks.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,6 @@ async def wait_for(fut, timeout):
474474
# See https://bugs.python.org/issue32751
475475
fut.remove_done_callback(cb)
476476
await _cancel_and_wait(fut, loop=loop)
477-
exc = fut.exception()
478-
if exc: # If fut.cancelled(), this will raise CancelledError
479-
raise exc
480477
raise
481478

482479
if fut.done():

Lib/test/test_asyncio/test_waitfor.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,12 @@ async def test_cancel_wait_for(self):
291291

292292
async def simultaneous_self_cancel_and_inner_result(
293293
self,
294-
do_cancel,
295294
waitfor_timeout,
296295
inner_action,
297296
):
298297
"""Construct scenario where external cancellation and
299298
awaitable becoming done happen simultaneously.
300299
inner_acion is one of 'cancel', 'exception' or 'result'.
301-
if do_cancel, the wait_for() call is cancelled at the same time when
302-
inner_action is executed.
303300
Make sure waitfor_timeout > 0.1. Trying to make it == 0.1 is not
304301
a reliable way to make timeout happen at the same time as the above.
305302
"""
@@ -320,30 +317,31 @@ async def simultaneous_self_cancel_and_inner_result(
320317
else:
321318
assert inner_action == 'result'
322319
inner.set_result('inner result')
323-
if do_cancel:
324-
waitfor_task.cancel()
325-
return await waitfor_task
320+
waitfor_task.cancel()
321+
with self.assertRaises(asyncio.CancelledError):
322+
return await waitfor_task
323+
# Consume inner's exception, to avoid "Future exception was never
324+
# retrieved" messages
325+
if inner_action == 'exception':
326+
self.assertIsInstance(inner.exception(), RuntimeError)
326327

327328
async def test_simultaneous_self_cancel_and_inner_result(self):
328329
for timeout in (10, None):
329330
with self.subTest(waitfor_timeout=timeout):
330-
with self.assertRaises(asyncio.CancelledError):
331-
await self.simultaneous_self_cancel_and_inner_result(
332-
True, timeout, 'result')
331+
await self.simultaneous_self_cancel_and_inner_result(
332+
timeout, 'result')
333333

334334
async def test_simultaneous_self_cancel_and_inner_exc(self):
335335
for timeout in (10, None):
336336
with self.subTest(waitfor_timeout=timeout):
337-
with self.assertRaises(RuntimeError):
338-
await self.simultaneous_self_cancel_and_inner_result(
339-
True, timeout, 'exception')
337+
await self.simultaneous_self_cancel_and_inner_result(
338+
timeout, 'exception')
340339

341340
async def test_simultaneous_self_cancel_and_inner_cancel(self):
342341
for timeout in (10, None):
343342
with self.subTest(waitfor_timeout=timeout):
344-
with self.assertRaises(asyncio.CancelledError):
345-
await self.simultaneous_self_cancel_and_inner_result(
346-
True, timeout, 'cancel')
343+
await self.simultaneous_self_cancel_and_inner_result(
344+
timeout, 'cancel')
347345

348346

349347
if __name__ == '__main__':

0 commit comments

Comments
 (0)