summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/base_subprocess.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-01-14 01:10:33 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-01-14 01:10:33 (GMT)
commitf651a604075c2dc9a2d7f3d3bd74da374ff8a696 (patch)
tree1fd5b58dcc596b6d4b26b9904076f391040f4f3e /Lib/asyncio/base_subprocess.py
parentc2c12e433aa47149c692eef5e5debd7c475b04c7 (diff)
downloadcpython-f651a604075c2dc9a2d7f3d3bd74da374ff8a696.zip
cpython-f651a604075c2dc9a2d7f3d3bd74da374ff8a696.tar.gz
cpython-f651a604075c2dc9a2d7f3d3bd74da374ff8a696.tar.bz2
Python issue #23173: sync with Tulip
* If an exception is raised during the creation of a subprocess, kill the subprocess (close pipes, kill and read the return status). Log an error in such case. * Fix SubprocessStreamProtocol.connection_made() to handle cancelled waiter. Add unit test cancelling subprocess methods.
Diffstat (limited to 'Lib/asyncio/base_subprocess.py')
-rw-r--r--Lib/asyncio/base_subprocess.py77
1 files changed, 53 insertions, 24 deletions
diff --git a/Lib/asyncio/base_subprocess.py b/Lib/asyncio/base_subprocess.py
index afc434d..0787ad7 100644
--- a/Lib/asyncio/base_subprocess.py
+++ b/Lib/asyncio/base_subprocess.py
@@ -96,32 +96,61 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
def kill(self):
self._proc.kill()
+ def _kill_wait(self):
+ """Close pipes, kill the subprocess and read its return status.
+
+ Function called when an exception is raised during the creation
+ of a subprocess.
+ """
+ if self._loop.get_debug():
+ logger.warning('Exception during subprocess creation, '
+ 'kill the subprocess %r',
+ self,
+ exc_info=True)
+
+ proc = self._proc
+ if proc.stdout:
+ proc.stdout.close()
+ if proc.stderr:
+ proc.stderr.close()
+ if proc.stdin:
+ proc.stdin.close()
+ try:
+ proc.kill()
+ except ProcessLookupError:
+ pass
+ proc.wait()
+
@coroutine
def _post_init(self):
- proc = self._proc
- loop = self._loop
- if proc.stdin is not None:
- _, pipe = yield from loop.connect_write_pipe(
- lambda: WriteSubprocessPipeProto(self, 0),
- proc.stdin)
- self._pipes[0] = pipe
- if proc.stdout is not None:
- _, pipe = yield from loop.connect_read_pipe(
- lambda: ReadSubprocessPipeProto(self, 1),
- proc.stdout)
- self._pipes[1] = pipe
- if proc.stderr is not None:
- _, pipe = yield from loop.connect_read_pipe(
- lambda: ReadSubprocessPipeProto(self, 2),
- proc.stderr)
- self._pipes[2] = pipe
-
- assert self._pending_calls is not None
-
- self._loop.call_soon(self._protocol.connection_made, self)
- for callback, data in self._pending_calls:
- self._loop.call_soon(callback, *data)
- self._pending_calls = None
+ try:
+ proc = self._proc
+ loop = self._loop
+ if proc.stdin is not None:
+ _, pipe = yield from loop.connect_write_pipe(
+ lambda: WriteSubprocessPipeProto(self, 0),
+ proc.stdin)
+ self._pipes[0] = pipe
+ if proc.stdout is not None:
+ _, pipe = yield from loop.connect_read_pipe(
+ lambda: ReadSubprocessPipeProto(self, 1),
+ proc.stdout)
+ self._pipes[1] = pipe
+ if proc.stderr is not None:
+ _, pipe = yield from loop.connect_read_pipe(
+ lambda: ReadSubprocessPipeProto(self, 2),
+ proc.stderr)
+ self._pipes[2] = pipe
+
+ assert self._pending_calls is not None
+
+ self._loop.call_soon(self._protocol.connection_made, self)
+ for callback, data in self._pending_calls:
+ self._loop.call_soon(callback, *data)
+ self._pending_calls = None
+ except:
+ self._kill_wait()
+ raise
def _call(self, cb, *data):
if self._pending_calls is not None: