From 4b4b186ec204c67e1d16dd14758943c90e590152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Sun, 22 Nov 2026 10:49:49 +0100 Subject: [PATCH 1/5] up --- Lib/asyncio/unix_events.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py index e9e60d8874f7d40..bbd1ae4ddbf3c0c 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -769,12 +769,20 @@ def write(self, data): self._buffer += data self._maybe_pause_protocol() + def _resume_write_polling(self): + if self._buffer: # abort() may have discarded the pending write. + self._loop._add_writer(self._fileno, self._write_ready) + def _write_ready(self): assert self._buffer, 'Data should not be empty' try: n = os.write(self._fileno, self._buffer) - except (BlockingIOError, InterruptedError): + except BlockingIOError: + # A full pipe can keep polling as writable on macOS (gh-162016). + self._loop._remove_writer(self._fileno) + self._loop.call_later(0.01, self._resume_write_polling) + except InterruptedError: pass except (SystemExit, KeyboardInterrupt): raise From 232ae7bfceba4f2374f2c94664e523b66d849ccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Sun, 22 Nov 2026 10:49:53 +0100 Subject: [PATCH 2/5] test --- Lib/test/test_asyncio/test_unix_events.py | 30 +++++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index c383a3bff962d74..6c4e75ed62dc256 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -1037,13 +1037,33 @@ def test__write_ready_partial(self, m_write): @mock.patch('os.write') def test__write_ready_again(self, m_write): tr = self.write_pipe_transport() - self.loop.add_writer(5, tr._write_ready) - tr._buffer = bytearray(b'data') - m_write.side_effect = BlockingIOError() + m_write.side_effect = (BlockingIOError, BlockingIOError, 4) + tr.write(b'data') tr._write_ready() - m_write.assert_called_with(5, bytearray(b'data')) + m_write.assert_called_with(5, b'data') + self.assertFalse(self.loop.writers) + retry, = self.loop._scheduled + self.assertGreater(retry.when(), self.loop.time()) + + tr.write_eof() + retry._run() self.loop.assert_writer(5, tr._write_ready) - self.assertEqual(bytearray(b'data'), tr._buffer) + self.assertEqual(tr._buffer, b'data') + + self.loop.writers[5]._run() + self.assertEqual(m_write.call_count, 3) + self.assertFalse(tr._buffer) + self.protocol.connection_lost.assert_called_once_with(None) + + @mock.patch('os.write', side_effect=BlockingIOError) + def test_abort_pending_write_retry(self, m_write): + tr = self.write_pipe_transport() + tr.write(b'data') + tr._write_ready() + retry, = self.loop._scheduled + tr.abort() + retry._run() + self.assertFalse(self.loop.writers) @mock.patch('os.write') def test__write_ready_empty(self, m_write): From 3ef834f328e2083cbc3b007a3c07377763f04b71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Sun, 22 Nov 2026 10:49:58 +0100 Subject: [PATCH 3/5] news --- .../next/Library/2026-11-22-01-26-50.gh-issue-162016.Xa3eQ9.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-22-01-26-50.gh-issue-162016.Xa3eQ9.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-22-01-26-50.gh-issue-162016.Xa3eQ9.rst b/Misc/NEWS.d/next/Library/2026-11-22-01-26-50.gh-issue-162016.Xa3eQ9.rst new file mode 100644 index 000000000000000..a16388d4491f14f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-22-01-26-50.gh-issue-162016.Xa3eQ9.rst @@ -0,0 +1,2 @@ +Prevent :mod:`asyncio` from busy-looping when a full pipe keeps being reported +as writable on Unix. From 2023471ddbdbb8e09e3a72d0ff68bfa7f80a7af6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Sun, 22 Nov 2026 11:08:45 +0100 Subject: [PATCH 4/5] const --- Lib/asyncio/constants.py | 3 +++ Lib/asyncio/unix_events.py | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Lib/asyncio/constants.py b/Lib/asyncio/constants.py index e0788851e187e7b..bb9bf41c6e84ff4 100644 --- a/Lib/asyncio/constants.py +++ b/Lib/asyncio/constants.py @@ -11,6 +11,9 @@ # Seconds to wait before retrying accept(). ACCEPT_RETRY_DELAY = 1 +# Seconds to wait before retrying a write to a full pipe. +PIPE_WRITE_RETRY_DELAY = 0.01 + # Number of stack entries to capture in debug mode. # The larger the number, the slower the operation in debug mode # (see extract_stack() in format_helpers.py). diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py index bbd1ae4ddbf3c0c..80741e73afb06e1 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -781,7 +781,8 @@ def _write_ready(self): except BlockingIOError: # A full pipe can keep polling as writable on macOS (gh-162016). self._loop._remove_writer(self._fileno) - self._loop.call_later(0.01, self._resume_write_polling) + self._loop.call_later(constants.PIPE_WRITE_RETRY_DELAY, + self._resume_write_polling) except InterruptedError: pass except (SystemExit, KeyboardInterrupt): From 0a96426b30068024145e18327376ba3ae8f23844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Sun, 22 Nov 2026 11:26:07 +0100 Subject: [PATCH 5/5] smaller delay --- Lib/asyncio/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/asyncio/constants.py b/Lib/asyncio/constants.py index bb9bf41c6e84ff4..0e1eaa9a79bb5d3 100644 --- a/Lib/asyncio/constants.py +++ b/Lib/asyncio/constants.py @@ -12,7 +12,7 @@ ACCEPT_RETRY_DELAY = 1 # Seconds to wait before retrying a write to a full pipe. -PIPE_WRITE_RETRY_DELAY = 0.01 +PIPE_WRITE_RETRY_DELAY = 0.001 # Number of stack entries to capture in debug mode. # The larger the number, the slower the operation in debug mode