Skip to content

Commit

Permalink
Bugfix ensure websocket disconnect cancels the connection's tasks
Browse files Browse the repository at this point in the history
The return would skip paste the cancel call, resulting in the nursery
not being cancelled and hence the app potentially running after
disconnection.
  • Loading branch information
pgjones committed Mar 26, 2020
1 parent 2277aeb commit 7b43f79
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/quart_trio/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async def handle_messages( # type: ignore
if event["type"] == "websocket.receive":
await self.send_channel.send(event.get("bytes") or event["text"])
elif event["type"] == "websocket.disconnect":
return
break
nursery.cancel_scope.cancel()

def _create_websocket_from_scope(self, send: Callable) -> Websocket:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest
import trio

from quart_trio.app import QuartTrio
from quart_trio.asgi import TrioASGIWebsocketConnection


@pytest.mark.trio
async def test_websocket_complete_on_disconnect() -> None:
scope = {
"asgi": {"version": "3"},
"headers": [(b"host", b"quart")],
"http_version": "1.1",
"method": "GET",
"scheme": "wss",
"path": "ws://quart/path",
"query_string": b"",
"subprotocols": [],
"extensions": {"websocket.http.response": {}},
}
connection = TrioASGIWebsocketConnection(QuartTrio(__name__), scope)
send_channel, receive_channel = trio.open_memory_channel(0)
async with trio.open_nursery() as nursery:
nursery.start_soon(connection.handle_messages, nursery, receive_channel.receive)
await send_channel.send({"type": "websocket.disconnect"})
assert nursery.cancel_scope.cancelled_caught

0 comments on commit 7b43f79

Please sign in to comment.