Skip to content

bpo-43884: Fix asyncio subprocess kill process cleanly when process is blocked #31611

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 1 commit 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
2 changes: 2 additions & 0 deletions Lib/asyncio/base_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def _process_exited(self, returncode):
if not waiter.cancelled():
waiter.set_result(returncode)
self._exit_waiters = None
self.close()

async def _wait(self):
"""Wait until the process exit and return the process return code.
Expand All @@ -234,6 +235,7 @@ async def _wait(self):
self._exit_waiters.append(waiter)
return await waiter


def _try_finish(self):
assert not self._finished
if self._returncode is None:
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_asyncio/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,20 @@ def test_kill(self):
else:
self.assertEqual(-signal.SIGKILL, returncode)

def test_kill_issue43884(self):
blocking_shell_command = f'{sys.executable} -c "import time; time.sleep(10000)"'
proc = self.loop.run_until_complete(
asyncio.create_subprocess_shell(blocking_shell_command, stdout=asyncio.subprocess.PIPE)
)
self.loop.run_until_complete(asyncio.sleep(1))
proc.kill()
returncode = self.loop.run_until_complete(proc.wait())
if sys.platform == 'win32':
self.assertIsInstance(returncode, int)
# expect 1 but sometimes get 0
else:
self.assertEqual(-signal.SIGKILL, returncode)

def test_terminate(self):
args = PROGRAM_BLOCKED
proc = self.loop.run_until_complete(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :mod:`asyncio` subprocess to close it's transport when the process exits.