uvicorn icon indicating copy to clipboard operation
uvicorn copied to clipboard

websocket.disconnect events are not sent to applications when clients don’t close connections properly (wsproto only)

Open sephioh opened this issue 4 years ago • 0 comments

Checklist

  • [x] The bug is reproducible against the latest release and/or master.
  • [x] There are no similar issues or pull requests to fix it yet.

Describe the bug

When WebSocket connections are not properly closed by clients, uvicorn + wsproto leaves one zombie asyncio tasks running per connection and do not emit disconnect events to applications.

To reproduce

  1. Start any application with uvicorn using wsproto;
  2. Create a websocket connection;
  3. Do not close the websocket properly;
  4. Observe that no websocket.disconnect events have been sent to your application;
  5. Check for all asyncio tasks (asyncio.all_tasks()): there should be a zombie run_asgi task for each non properly closed connection.

I’ve setup this repo with instructions to make it easier to reproduce this issue: https://github.com/sephioh/uvicorn-wsproto-issue

Expected behavior

A disconnect event should be sent to applications.

Actual behavior

Disconnect event are not being sent to apps when client socket loss happens.

Environment

Running uvicorn 0.13.4 with CPython 3.9.0 on Linux Must be executed with wsproto (—ws wsproto).

Additional context

https://docs.python.org/3/library/asyncio-protocol.html#asyncio.BaseProtocol.connection_lost

No problems are found when running with websockets impl (—ws websockets).

I’ve opened a PR with a possible fix: https://github.com/encode/uvicorn/pull/996

sephioh avatar Mar 10 '21 13:03 sephioh