diff --git a/Lib/asyncio/constants.py b/Lib/asyncio/constants.py index e0788851e187e7..0e1eaa9a79bb5d 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.001 + # 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 e9e60d8874f7d4..80741e73afb06e 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -769,12 +769,21 @@ 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(constants.PIPE_WRITE_RETRY_DELAY, + self._resume_write_polling) + except InterruptedError: pass except (SystemExit, KeyboardInterrupt): raise diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index c383a3bff962d7..6c4e75ed62dc25 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): 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 00000000000000..a16388d4491f14 --- /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.