Skip to content

gh-105288: wake up exit waiters after sub-process exits #105289

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

Closed
wants to merge 3 commits into from
Closed
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
14 changes: 9 additions & 5 deletions Lib/asyncio/base_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ def _process_exited(self, returncode):
self._call(self._protocol.process_exited)

self._try_finish()
self._wakeup_exit_waiters()

def _wakeup_exit_waiters(self):
# wake up futures waiting for wait()
for waiter in self._exit_waiters:
if not waiter.cancelled():
waiter.set_result(self._returncode)
self._exit_waiters = []

async def _wait(self):
"""Wait until the process exit and return the process return code.
Expand All @@ -260,11 +268,7 @@ def _call_connection_lost(self, exc):
try:
self._protocol.connection_lost(exc)
finally:
# wake up futures waiting for wait()
for waiter in self._exit_waiters:
if not waiter.cancelled():
waiter.set_result(self._returncode)
self._exit_waiters = None
self._wakeup_exit_waiters()
self._loop = None
self._proc = None
self._protocol = None
Expand Down
31 changes: 31 additions & 0 deletions Lib/test/test_asyncio/test_subprocess.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import signal
import sys
import threading
import time
import textwrap
import unittest
import warnings
Expand Down Expand Up @@ -847,6 +849,35 @@ async def main() -> None:
for fd in fds:
self.assertEqual(per_fd_events[fd], per_fd_expected, (fd, events))

def test_subprocess_wait_not_hang_gh105288(self):
# See https://github.com/python/cpython/issues/105288
TIMEOUT = 5
finished = False

async def whoami():
proc = await subprocess.create_subprocess_exec('whoami')
await proc.communicate()

async def main():
t = asyncio.create_task(whoami())
await asyncio.wait([0, t])

def _main():
nonlocal finished
try:
# We have to run the event loop in a separate thread,
# to make sure we can fail in the main thread
# and successfully notify the test driver.
self.loop.run_until_complete(main())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem is in asyncio.run - so this doesn't actually test the issue

finally:
finished = True


threading.Thread(target=_main).start()
time.sleep(TIMEOUT)
if not finished:
self.fail('watchdog timeout, event loop is probably sleeping infinitely')

def test_subprocess_communicate_stdout(self):
# See https://github.com/python/cpython/issues/100133
async def get_command_stdout(cmd, *args):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Exit waiters are not resolved after subprocess handled by :class:`asyncio.Task`
exits. This will causes a recent :func:`asyncio.wait` call hang forever if
there are objects that are not :class:`typing.Awaitable` yield from its first
parameter.
Loading