Skip to content

GH-89237: fix hang in proactor subprocess.wait_closed() #98572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/asyncio/proactor_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_asyncio/test_proactor_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix hang on Windows in ``subprocess.wait_closed()`` in :mod:`asyncio` with :class:`~asyncio.ProactorEventLoop`. Patch by Kumar Aditya.