From ccfc6006c42b2ade226a75f70f692b1ce1b86055 Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Wed, 25 Nov 2026 20:24:24 +0800 Subject: [PATCH] Stop the stream listener after a framing read error --- pylsp_jsonrpc/streams.py | 1 + test/test_streams.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/pylsp_jsonrpc/streams.py b/pylsp_jsonrpc/streams.py index 40048a9..91362d7 100644 --- a/pylsp_jsonrpc/streams.py +++ b/pylsp_jsonrpc/streams.py @@ -32,6 +32,7 @@ def listen(self, message_consumer): if self._rfile.closed: return log.exception("Failed to read from rfile") + return if request_str is None: break diff --git a/test/test_streams.py b/test/test_streams.py index 14fe1bb..ebec247 100644 --- a/test/test_streams.py +++ b/test/test_streams.py @@ -76,6 +76,21 @@ def test_reader_bad_json(rfile, reader): consumer.assert_not_called() +@pytest.mark.parametrize('preceding_message', [False, True]) +def test_reader_invalid_length_stops_without_replaying(preceding_message, caplog): + stream = BytesIO() + writer = JsonRpcStreamWriter(stream) + message = {'id': 1, 'method': 'perform_action', 'params': {}} + if preceding_message: + writer.write(message) + stream.write(b'Content-Length: invalid\r\n\r\n{}') + stream.seek(0) + received = [] + JsonRpcStreamReader(stream).listen(received.append) + assert received == ([message] if preceding_message else []) + assert 'Failed to read from rfile' in caplog.text + + def test_writer(wfile, writer): writer.write({ 'id': 'hello',