Server going away combined with a cancel causes a RuntimeError when closing httpx.AsyncClient #2437
-
|
It looks like that if the server goes away in the middle of a request, and a request is cancelled, then when subsequently closing the async client a RuntimeError is raised:
The below script shows this for me, on Python 3.10 and 3.11 on macOS, with httpx 0.23.0 and aiohttp 3.8.3 (for the server part) import asyncio
import httpx
from aiohttp import web
async def main():
async def slow(_):
await asyncio.sleep(4)
return web.Response(status=200)
print('Starting server')
app = web.Application()
app.add_routes([
web.get(f'/', slow),
])
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, '0.0.0.0', 8080)
await site.start()
print("Started server")
print('Opening client')
client = httpx.AsyncClient()
print('Making request')
task = asyncio.create_task(client.request('GET', 'http://127.0.0.1:8080/'))
await asyncio.sleep(1)
print('Stopping server')
await runner.cleanup()
print('Stopped server')
print('Cancelling request')
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
print('Cancelled request')
print('Closing client')
await client.aclose()
print('Closed client') # Never gets here
asyncio.run(main())It seems to need both the stopping of the server and the cancelling of the task that's running the request to see the issue. If only one of them is done, then no exception is raised. Note on Python 3.9 the behaviour is different for me. When closing the client it doesn't raise an exception, but it seems to hang forever. (I discovered this in tests where I fire up and close down servers running locally a lot) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
So I think this is since I had an older version of httpcore installed: 0.15.0. Testing with 0.16.0 and 0.16.1, then no exception (and no hanging) - it all behaves as expected. Having a nose around, maybe it was encode/httpcore#580 that fixed it |
Beta Was this translation helpful? Give feedback.
So I think this is since I had an older version of httpcore installed: 0.15.0. Testing with 0.16.0 and 0.16.1, then no exception (and no hanging) - it all behaves as expected.
Having a nose around, maybe it was encode/httpcore#580 that fixed it