From 7f4962ba47ad00eeabf84cd2665ebb8d0d1f8940 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sun, 23 Oct 2022 23:51:18 +0530 Subject: [PATCH 1/4] fix hang --- Lib/asyncio/proactor_events.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/asyncio/proactor_events.py b/Lib/asyncio/proactor_events.py index ddb9daca026936..2685a3376cfd95 100644 --- a/Lib/asyncio/proactor_events.py +++ b/Lib/asyncio/proactor_events.py @@ -60,6 +60,7 @@ def __init__(self, loop, sock, protocol, waiter=None, self._pending_write = 0 self._conn_lost = 0 self._closing = False # Set when close() called. + self._called_connection_lost = False self._eof_written = False if self._server is not None: self._server._attach() @@ -136,7 +137,7 @@ def _force_close(self, exc): self._empty_waiter.set_result(None) else: self._empty_waiter.set_exception(exc) - if self._closing: + if self._closing and self._called_connection_lost: return self._closing = True self._conn_lost += 1 @@ -166,6 +167,7 @@ def _call_connection_lost(self, exc): if server is not None: server._detach() self._server = None + self._called_connection_lost = True def get_write_buffer_size(self): size = self._pending_write From fd9cf928b138ce48eed11ec68e35a7afd0283750 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 23 Oct 2022 18:30:41 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-10-23-18-30-39.gh-issue-89237.kBui30.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-10-23-18-30-39.gh-issue-89237.kBui30.rst diff --git a/Misc/NEWS.d/next/Library/2022-10-23-18-30-39.gh-issue-89237.kBui30.rst b/Misc/NEWS.d/next/Library/2022-10-23-18-30-39.gh-issue-89237.kBui30.rst new file mode 100644 index 00000000000000..c14abb0c5b7f3a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-10-23-18-30-39.gh-issue-89237.kBui30.rst @@ -0,0 +1 @@ +Fix hang in ``subprocess.wait_closed()`` in :mod:`asyncio` with :class:`~asyncio.ProactorEventLoop`. Patch by Kumar Aditya. From 008be5681ed05ae20c8e9e0d7662bc4ea6e84da9 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Mon, 24 Oct 2022 06:36:26 +0000 Subject: [PATCH 3/4] fix test and add comment --- Lib/test/test_asyncio/test_proactor_events.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_asyncio/test_proactor_events.py b/Lib/test/test_asyncio/test_proactor_events.py index 7fca0541ee75a2..7fd8b261cd5ee1 100644 --- a/Lib/test/test_asyncio/test_proactor_events.py +++ b/Lib/test/test_asyncio/test_proactor_events.py @@ -290,7 +290,12 @@ def test_force_close_idempotent(self): tr._closing = True tr._force_close(None) test_utils.run_briefly(self.loop) - self.assertFalse(self.protocol.connection_lost.called) + # See https://github.com/python/cpython/issues/89237 + # `protocol.connection_lost` should be called even if + # the transport was closed forcefully otherwise + # the resources held by protocol will never be freed + # and waiters will never be notified leading to hang. + self.assertTrue(self.protocol.connection_lost.called) def test_fatal_error_2(self): tr = self.socket_transport() From b1316d04e49652c18398742c23a7d9ad9287a90f Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 24 Oct 2022 10:56:50 -0700 Subject: [PATCH 4/4] Mention Windows in news --- .../next/Library/2022-10-23-18-30-39.gh-issue-89237.kBui30.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2022-10-23-18-30-39.gh-issue-89237.kBui30.rst b/Misc/NEWS.d/next/Library/2022-10-23-18-30-39.gh-issue-89237.kBui30.rst index c14abb0c5b7f3a..668ea4c7a4eadb 100644 --- a/Misc/NEWS.d/next/Library/2022-10-23-18-30-39.gh-issue-89237.kBui30.rst +++ b/Misc/NEWS.d/next/Library/2022-10-23-18-30-39.gh-issue-89237.kBui30.rst @@ -1 +1 @@ -Fix hang in ``subprocess.wait_closed()`` in :mod:`asyncio` with :class:`~asyncio.ProactorEventLoop`. Patch by Kumar Aditya. +Fix hang on Windows in ``subprocess.wait_closed()`` in :mod:`asyncio` with :class:`~asyncio.ProactorEventLoop`. Patch by Kumar Aditya.