pgjones / hypercorn

Hypercorn is an ASGI and WSGI Server based on Hyper libraries and inspired by Gunicorn.
MIT License
1.12k stars 102 forks source link

`DispatcherMiddleware` causes `CancelledError` on shudown #258

Open npt opened 3 days ago

npt commented 3 days ago

Copied from this comment on a different issue with DispatcherMiddleware:

When I use Hypercorn's serve to serve Hypercorn's DispatcherMiddleware, when the server exits, it raises a CancelledError with a traceback looking like:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File ".../__main__.py", line 69, in <module>
    asyncio.run(serve(dispatcher, config))
  File "/usr/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/base_events.py", line 664, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File ".../.venv/lib/python3.12/site-packages/hypercorn/asyncio/__init__.py", line 44, in serve
    await worker_serve(
  File ".../.venv/lib/python3.12/site-packages/hypercorn/asyncio/run.py", line 181, in worker_serve
    await lifespan_task
  File ".../.venv/lib/python3.12/site-packages/hypercorn/asyncio/lifespan.py", line 55, in handle_lifespan
    await self.app(
  File ".../.venv/lib/python3.12/site-packages/hypercorn/app_wrappers.py", line 34, in __call__
    await self.app(scope, receive, send)
  File ".../src/connector/__main__.py", line 57, in __call__
    await self.app(scope, receive, send)
  File ".../.venv/lib/python3.12/site-packages/hypercorn/middleware/dispatcher.py", line 19, in __call__
    await self._handle_lifespan(scope, receive, send)
  File ".../.venv/lib/python3.12/site-packages/hypercorn/middleware/dispatcher.py", line 46, in _handle_lifespan
    async with TaskGroup(asyncio.get_event_loop()) as task_group:
  File ".../.venv/lib/python3.12/site-packages/hypercorn/asyncio/task_group.py", line 74, in __aexit__
    await self._task_group.__aexit__(exc_type, exc_value, tb)
  File "/usr/lib/python3.12/asyncio/taskgroups.py", line 136, in __aexit__
    raise propagate_cancellation_error
  File "/usr/lib/python3.12/asyncio/taskgroups.py", line 112, in __aexit__
    await self._on_completed_fut
asyncio.exceptions.CancelledError