Unexpected suppressing of asyncio.CancelledError exception #2370
-
Environment:
Listing: import asyncio
import httpx
async def request(url):
async with httpx.AsyncClient() as client:
try:
await client.get(url)
except BaseException as exc:
print('Request exception', type(exc), exc)
raise
async def main():
url = 'http://1.2.3.4:1234/' # Some unreachable resource with delay
task = asyncio.create_task(request(url))
await asyncio.sleep(1)
task.cancel()
try:
await task
except BaseException as exc:
print('Task exception', type(exc), exc)
asyncio.run(main()) Expected behavior:
Actual behavior:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
I'm not able to replicate that... import asyncio
import httpx
async def request(url):
async with httpx.AsyncClient() as client:
try:
await client.get(url)
except BaseException as exc:
print('Request exception', type(exc), exc)
raise
async def main():
url = 'https://httpbin.org/delay/3' # Some unreachable resource with delay
task = asyncio.create_task(request(url))
await asyncio.sleep(1)
task.cancel()
try:
await task
except BaseException as exc:
print('Task exception', type(exc), exc)
asyncio.run(main()) Results in... $ python ./example.py
Request exception <class 'asyncio.exceptions.CancelledError'>
Task exception <class 'asyncio.exceptions.CancelledError'> If I use an actually unreachable resource... Eg. |
Beta Was this translation helpful? Give feedback.
-
@tomchristie I have the following task: there is a coroutine where request should be done with guarantee, something like that: async def request(url):
while True:
async with httpx.AsyncClient() as client:
try:
return await client.get(url)
except httpx.RequestError as exc:
await asyncio.sleep(5) If I cancel a task with this coroutine on demand then it can suppress |
Beta Was this translation helpful? Give feedback.
-
I guess agronholm/anyio#374 is the reason for. |
Beta Was this translation helpful? Give feedback.
-
Fixed in agronholm/anyio#374. |
Beta Was this translation helpful? Give feedback.
I'm not able to replicate that...
Results in...