Skip to content

Commit e8c67ce

Browse files
committed
Don't raise TimeoutError if the CancelledError was swallowed by inner code
1 parent ac6f8c8 commit e8c67ce

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

Lib/asyncio/timeouts.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ async def __aexit__(
9292
if self._state is _State.EXPIRING:
9393
self._state = _State.EXPIRED
9494

95-
if (self._task.uncancel() == 0
96-
and exc_type in (None, exceptions.CancelledError)
97-
):
95+
if self._task.uncancel() == 0 and exc_type is exceptions.CancelledError:
9896
# Since there are no outstanding cancel requests, we're
9997
# handling this.
10098
raise TimeoutError

Lib/test/test_asyncio/test_timeouts.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,33 @@ async def test_timeout_at_basic(self):
4545
self.assertEqual(deadline, cm.when())
4646

4747
async def test_nested_timeouts(self):
48-
cancel = False
48+
loop = asyncio.get_running_loop()
49+
cancelled = False
4950
with self.assertRaises(TimeoutError):
50-
async with asyncio.timeout(0.01) as cm1:
51+
deadline = loop.time() + 0.01
52+
async with asyncio.timeout_at(deadline) as cm1:
5153
# Only the topmost context manager should raise TimeoutError
52-
with self.assertRaises(asyncio.CancelledError):
53-
async with asyncio.timeout(0.01) as cm2:
54+
try:
55+
async with asyncio.timeout_at(deadline) as cm2:
5456
await asyncio.sleep(10)
57+
except asyncio.CancelledError:
58+
cancelled = True
59+
raise
60+
self.assertTrue(cancelled)
5561
self.assertTrue(cm1.expired())
5662
self.assertTrue(cm2.expired())
5763

5864
async def test_waiter_cancelled(self):
65+
loop = asyncio.get_running_loop()
66+
cancelled = False
5967
with self.assertRaises(TimeoutError):
6068
async with asyncio.timeout(0.01):
61-
with self.assertRaises(asyncio.CancelledError):
69+
try:
6270
await asyncio.sleep(10)
71+
except asyncio.CancelledError:
72+
cancelled = True
73+
raise
74+
self.assertTrue(cancelled)
6375

6476
async def test_timeout_not_called(self):
6577
loop = asyncio.get_running_loop()

0 commit comments

Comments
 (0)