diff --git a/Lib/logging/config.py b/Lib/logging/config.py index f566de5750dbf5..a49d3d17a6549b 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 4c61019fc4f8f0..6b934dd1ffb2d9 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 00000000000000..b68eef802cbd61 --- /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.