Skip to content

Commit

Permalink
fix asyncio subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaraditya303 authored Feb 28, 2022
1 parent c32aef4 commit 2c3b765
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
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.

0 comments on commit 2c3b765

Please sign in to comment.