Skip to content

Commit 2c3b765

Browse files
fix asyncio subprocess
1 parent c32aef4 commit 2c3b765

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

Lib/asyncio/base_subprocess.py

+2
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def _process_exited(self, returncode):
222222
if not waiter.cancelled():
223223
waiter.set_result(returncode)
224224
self._exit_waiters = None
225+
self.close()
225226

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

238+
237239
def _try_finish(self):
238240
assert not self._finished
239241
if self._returncode is None:

Lib/test/test_asyncio/test_subprocess.py

+14
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,20 @@ def test_kill(self):
181181
else:
182182
self.assertEqual(-signal.SIGKILL, returncode)
183183

184+
def test_kill_issue43884(self):
185+
blocking_shell_command = f'{sys.executable} -c "import time; time.sleep(10000)"'
186+
proc = self.loop.run_until_complete(
187+
asyncio.create_subprocess_shell(blocking_shell_command, stdout=asyncio.subprocess.PIPE)
188+
)
189+
self.loop.run_until_complete(asyncio.sleep(1))
190+
proc.kill()
191+
returncode = self.loop.run_until_complete(proc.wait())
192+
if sys.platform == 'win32':
193+
self.assertIsInstance(returncode, int)
194+
# expect 1 but sometimes get 0
195+
else:
196+
self.assertEqual(-signal.SIGKILL, returncode)
197+
184198
def test_terminate(self):
185199
args = PROGRAM_BLOCKED
186200
proc = self.loop.run_until_complete(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :mod:`asyncio` subprocess to close it's transport when the process exits.

0 commit comments

Comments
 (0)