Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix improperly closed WebSocket connections generating a backtrace #9883

Merged
merged 13 commits into from
Nov 14, 2024
1 change: 1 addition & 0 deletions CHANGES/9883.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed improperly closed WebSocket connections generating a backtrace -- by :user:`bdraco`.
bdraco marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions aiohttp/_websocket/reader_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def __init__(
self._get_buffer = self._buffer.popleft
self._put_buffer = self._buffer.append

def is_eof(self) -> bool:
return self._eof

def exception(self) -> Optional[Union[Type[BaseException], BaseException]]:
return self._exception

Expand Down
24 changes: 24 additions & 0 deletions tests/test_client_ws_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1232,3 +1232,27 @@ async def test_ws_connect_with_wrong_ssl_type(aiohttp_client: AiohttpClient) ->

with pytest.raises(TypeError, match="ssl should be SSLContext, .*"):
await session.ws_connect("/", ssl=42)


async def test_websocket_connection_not_closed_property(
aiohttp_client: AiohttpClient,
) -> None:
"""Test that closing the connection via __del__ does not raise an exception."""

async def handler(request: web.Request) -> web.WebSocketResponse:
ws = web.WebSocketResponse()
await ws.prepare(request)
await ws.close()
return ws

app = web.Application()
app.router.add_route("GET", "/", handler)
client = await aiohttp_client(app)
resp = await client.ws_connect("/")
assert resp._conn is not None
# Simulate the connection not being closed properly
# https://github.com/aio-libs/aiohttp/issues/9880
resp._conn.release()
bdraco marked this conversation as resolved.
Show resolved Hide resolved

# Clean up so the test does not leak
await resp.close()
Loading