From e0905c3e5660c69dce2f116fe28e9f7f764a1e54 Mon Sep 17 00:00:00 2001 From: lipengyu Date: Mon, 16 Nov 2026 16:33:17 +0800 Subject: [PATCH] gh-162091: Stop logging config listener on premature EOF --- Lib/logging/config.py | 5 ++++- Lib/test/test_logging.py | 10 ++++++++++ .../2026-11-16-16-28-11.gh-issue-162091.J42qmv.rst | 2 ++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-16-16-28-11.gh-issue-162091.J42qmv.rst diff --git a/Lib/logging/config.py b/Lib/logging/config.py index f566de5750dbf55..a49d3d17a6549be 100644 --- a/Lib/logging/config.py +++ b/Lib/logging/config.py @@ -972,7 +972,10 @@ def handle(self): slen = struct.unpack(">L", chunk)[0] chunk = self.connection.recv(slen) while len(chunk) < slen: - chunk = chunk + conn.recv(slen - len(chunk)) + data = conn.recv(slen - len(chunk)) + if not data: + return + chunk = chunk + data if self.server.verify is not None: chunk = self.server.verify(chunk) if chunk is not None: # verified, can process diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 4c61019fc4f8f07..6b934dd1ffb2d93 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -3839,6 +3839,16 @@ def verify_reverse(stuff): ('ERROR', '2'), ], pat=r"^[\w.]+ -> (\w+): (\d+)$") + def test_listen_disconnect(self): + listener = logging.config.listen(0) + handler = listener.hdlr.__new__(listener.hdlr) + handler.connection = Mock(recv=Mock(side_effect=( + struct.pack('>L', 100), b'abc', b''))) + + handler.handle() + + self.assertEqual(handler.connection.recv.call_count, 3) + @support.requires_working_socket() def test_listen_server_error(self): # The "ready" event should be set even if the server fails to start. diff --git a/Misc/NEWS.d/next/Library/2026-11-16-16-28-11.gh-issue-162091.J42qmv.rst b/Misc/NEWS.d/next/Library/2026-11-16-16-28-11.gh-issue-162091.J42qmv.rst new file mode 100644 index 000000000000000..b68eef802cbd617 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-16-16-28-11.gh-issue-162091.J42qmv.rst @@ -0,0 +1,2 @@ +Fix an infinite loop in :func:`logging.config.listen` when a client +disconnects before sending the complete configuration payload.