diff --git a/src/acp/connection.py b/src/acp/connection.py index aca1c19..8f4c401 100644 --- a/src/acp/connection.py +++ b/src/acp/connection.py @@ -151,6 +151,9 @@ async def _receive_loop(self) -> None: line = await self._reader.readline() if not line: break + line = line.strip() + if not line: + continue try: message: dict[str, Any] = json.loads(line) except Exception: diff --git a/tests/test_rpc.py b/tests/test_rpc.py index 0d3bb75..7c44441 100644 --- a/tests/test_rpc.py +++ b/tests/test_rpc.py @@ -316,6 +316,20 @@ async def test_ignore_invalid_messages(connect, server): await asyncio.wait_for(server.client_reader.readline(), timeout=0.1) +@pytest.mark.asyncio +async def test_blank_lines_skipped(connect, server): + connect(connect_agent=True, connect_client=False) + + for noise in [b"\n", b" \n", b"\r\n"]: + server.client_writer.write(noise) + req = {"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": 1}} + server.client_writer.write((json.dumps(req) + "\n").encode()) + await server.client_writer.drain() + + resp = json.loads(await asyncio.wait_for(server.client_reader.readline(), timeout=1)) + assert resp["id"] == 1 and "result" in resp + + class _ExampleAgent(Agent): __test__ = False