CancelledError raised unexpectedly when using asyncio.TaskGroup and httpx #3150
-
I'm experiencing some unexpected behaviour when using The following code demonstrates the problem. The expected behaviour is:
The actual behaviour is:
Swapping the call to Am I using the TaskGroup in the wrong way somehow? Or is this a bug? import asyncio
import httpx
async def task1_raises_runtime_error():
await asyncio.sleep(0.001)
raise RuntimeError("foo")
async def task2_httpx_request(client, name: str):
print(f"task2_httpx_request {name}")
# swap the following lines to see the difference
result = await client.get("https://example.com")
# await asyncio.sleep(1)
print("task2_httpx_request done")
async def main():
async with httpx.AsyncClient() as client:
try:
async with asyncio.TaskGroup() as tg:
tg.create_task(task1_raises_runtime_error())
tg.create_task(task2_httpx_request(client, "A"))
except Exception as err:
print("an exception occurred", err)
await asyncio.sleep(0)
await asyncio.sleep(0)
await task2_httpx_request(client, "B")
asyncio.run(main()) Running on macOS, Python 3.11.6, httpx 0.27.0. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I don't think I'm seeing problematic behaviour here... Here's what I've got: import asyncio
import httpx
async def task1_raises_runtime_error():
print("task1_raises_runtime_error started")
await asyncio.sleep(0.001)
raise RuntimeError("foo")
async def task2_httpx_request(client, name: str):
print(f"task2_httpx_request {name} started")
try:
# swap the following lines to see the difference
await asyncio.sleep(1)
result = await client.get("https://example.com")
except asyncio.CancelledError:
print(f"task2_httpx_request {name} cancelled")
raise
print(f"task2_httpx_request {name} done")
async def main():
async with httpx.AsyncClient() as client:
try:
async with asyncio.TaskGroup() as tg:
tg.create_task(task1_raises_runtime_error())
tg.create_task(task2_httpx_request(client, "A"))
except Exception as err:
print("an exception occurred", err)
await asyncio.sleep(0)
await asyncio.sleep(0)
await task2_httpx_request(client, "B")
asyncio.run(main()) And the output...
|
Beta Was this translation helpful? Give feedback.
OK. I've found the problematic dependency. Another package in my environment was depending on
anyio==3.7.1
which is not the latestanyio==4.3.0
. If I upgrade that package the exception goes away. 😕