From bf24b5677fab207e327063f0ad1f1b38868f85fa Mon Sep 17 00:00:00 2001 From: Henrik Jess Nielsen Date: Sun, 10 May 2026 15:16:55 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20backend=20unavailable=20test=20=E2=80=94?= =?UTF-8?q?=20use=20free=20port=20+=20ws.close(timeout=3D2s)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Use _free_port() instead of hardcoded 19999 (avoids CI port conflicts) - Add timeout=2.0 to ws.close() so close handshake never blocks >2s --- ilsp/server.py | 2 +- tests/test_ws_proxy.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ilsp/server.py b/ilsp/server.py index 8e724d2..cc00043 100644 --- a/ilsp/server.py +++ b/ilsp/server.py @@ -72,7 +72,7 @@ async def _ws_proxy(request: web.Request, host: str, port: int) -> web.WebSocket ) except (OSError, asyncio.TimeoutError) as exc: logger.error("Cannot connect to LSP on %s:%d — %s", host, port, exc) - await ws.close(code=1011, message=b"LSP backend unavailable") + await ws.close(code=1011, message=b"LSP backend unavailable", timeout=2.0) return ws async def ws_to_tcp(): diff --git a/tests/test_ws_proxy.py b/tests/test_ws_proxy.py index 73b45d3..7745648 100644 --- a/tests/test_ws_proxy.py +++ b/tests/test_ws_proxy.py @@ -192,7 +192,9 @@ async def test_fake_client_receives_completion_capabilities(proxy_base_url): @pytest.mark.asyncio async def test_ws_backend_unavailable_closes_cleanly(proxy_base_url, monkeypatch): """If pylsp is unreachable, WebSocket should close gracefully (not crash).""" - monkeypatch.setattr("ilsp.server.PYTHON_LSP_PORT", 19999) + # Use _free_port() to guarantee nothing is listening (avoids CI port 19999 collisions) + dead_port = _free_port() + monkeypatch.setattr("ilsp.server.PYTHON_LSP_PORT", dead_port) ws_url = proxy_base_url.replace("http://", "ws://") + "/python" async with aiohttp.ClientSession() as session: