Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 1 | import collections |
| 2 | import subprocess |
Victor Stinner | 978a9af | 2015-01-29 17:50:58 +0100 | [diff] [blame] | 3 | import warnings |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 4 | |
Yury Selivanov | 2a8911c | 2015-08-04 15:56:33 -0400 | [diff] [blame] | 5 | from . import compat |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 6 | from . import protocols |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 7 | from . import transports |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 8 | from .coroutines import coroutine |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 9 | from .log import logger |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 10 | |
| 11 | |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 12 | class BaseSubprocessTransport(transports.SubprocessTransport): |
| 13 | |
| 14 | def __init__(self, loop, protocol, args, shell, |
| 15 | stdin, stdout, stderr, bufsize, |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 16 | waiter=None, extra=None, **kwargs): |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 17 | super().__init__(extra) |
Victor Stinner | 978a9af | 2015-01-29 17:50:58 +0100 | [diff] [blame] | 18 | self._closed = False |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 19 | self._protocol = protocol |
| 20 | self._loop = loop |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 21 | self._proc = None |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 22 | self._pid = None |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 23 | self._returncode = None |
| 24 | self._exit_waiters = [] |
| 25 | self._pending_calls = collections.deque() |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 26 | self._pipes = {} |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 27 | self._finished = False |
| 28 | |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 29 | if stdin == subprocess.PIPE: |
Victor Stinner | 915bcb0 | 2014-02-01 22:49:59 +0100 | [diff] [blame] | 30 | self._pipes[0] = None |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 31 | if stdout == subprocess.PIPE: |
Victor Stinner | 915bcb0 | 2014-02-01 22:49:59 +0100 | [diff] [blame] | 32 | self._pipes[1] = None |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 33 | if stderr == subprocess.PIPE: |
Victor Stinner | 915bcb0 | 2014-02-01 22:49:59 +0100 | [diff] [blame] | 34 | self._pipes[2] = None |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 35 | |
| 36 | # Create the child process: set the _proc attribute |
Victor Stinner | 6fb1e74 | 2015-07-31 17:49:43 +0200 | [diff] [blame] | 37 | try: |
| 38 | self._start(args=args, shell=shell, stdin=stdin, stdout=stdout, |
| 39 | stderr=stderr, bufsize=bufsize, **kwargs) |
| 40 | except: |
| 41 | self.close() |
| 42 | raise |
| 43 | |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 44 | self._pid = self._proc.pid |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 45 | self._extra['subprocess'] = self._proc |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 46 | |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 47 | if self._loop.get_debug(): |
| 48 | if isinstance(args, (bytes, str)): |
| 49 | program = args |
| 50 | else: |
| 51 | program = args[0] |
| 52 | logger.debug('process %r created: pid %s', |
| 53 | program, self._pid) |
| 54 | |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 55 | self._loop.create_task(self._connect_pipes(waiter)) |
| 56 | |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 57 | def __repr__(self): |
Victor Stinner | 978a9af | 2015-01-29 17:50:58 +0100 | [diff] [blame] | 58 | info = [self.__class__.__name__] |
| 59 | if self._closed: |
| 60 | info.append('closed') |
Victor Stinner | 7a82afe | 2015-03-10 16:32:29 +0100 | [diff] [blame] | 61 | if self._pid is not None: |
| 62 | info.append('pid=%s' % self._pid) |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 63 | if self._returncode is not None: |
| 64 | info.append('returncode=%s' % self._returncode) |
Victor Stinner | 7a82afe | 2015-03-10 16:32:29 +0100 | [diff] [blame] | 65 | elif self._pid is not None: |
Victor Stinner | 4e82fb9 | 2015-02-17 22:50:33 +0100 | [diff] [blame] | 66 | info.append('running') |
Victor Stinner | 7a82afe | 2015-03-10 16:32:29 +0100 | [diff] [blame] | 67 | else: |
| 68 | info.append('not started') |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 69 | |
| 70 | stdin = self._pipes.get(0) |
| 71 | if stdin is not None: |
| 72 | info.append('stdin=%s' % stdin.pipe) |
| 73 | |
| 74 | stdout = self._pipes.get(1) |
| 75 | stderr = self._pipes.get(2) |
| 76 | if stdout is not None and stderr is stdout: |
| 77 | info.append('stdout=stderr=%s' % stdout.pipe) |
| 78 | else: |
| 79 | if stdout is not None: |
| 80 | info.append('stdout=%s' % stdout.pipe) |
| 81 | if stderr is not None: |
| 82 | info.append('stderr=%s' % stderr.pipe) |
| 83 | |
| 84 | return '<%s>' % ' '.join(info) |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 85 | |
| 86 | def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs): |
| 87 | raise NotImplementedError |
| 88 | |
Yury Selivanov | a05a6ef | 2016-09-11 21:11:02 -0400 | [diff] [blame] | 89 | def set_protocol(self, protocol): |
| 90 | self._protocol = protocol |
| 91 | |
| 92 | def get_protocol(self): |
| 93 | return self._protocol |
| 94 | |
Yury Selivanov | 5bb1afb | 2015-11-16 12:43:21 -0500 | [diff] [blame] | 95 | def is_closing(self): |
| 96 | return self._closed |
| 97 | |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 98 | def close(self): |
Victor Stinner | f2e43cb | 2015-01-30 01:20:44 +0100 | [diff] [blame] | 99 | if self._closed: |
| 100 | return |
Victor Stinner | 978a9af | 2015-01-29 17:50:58 +0100 | [diff] [blame] | 101 | self._closed = True |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 102 | |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 103 | for proto in self._pipes.values(): |
Victor Stinner | 29ad011 | 2015-01-15 00:04:21 +0100 | [diff] [blame] | 104 | if proto is None: |
| 105 | continue |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 106 | proto.pipe.close() |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 107 | |
Victor Stinner | 8e36812 | 2015-02-10 14:49:32 +0100 | [diff] [blame] | 108 | if (self._proc is not None |
| 109 | # the child process finished? |
| 110 | and self._returncode is None |
| 111 | # the child process finished but the transport was not notified yet? |
| 112 | and self._proc.poll() is None |
| 113 | ): |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 114 | if self._loop.get_debug(): |
| 115 | logger.warning('Close running child process: kill %r', self) |
| 116 | |
| 117 | try: |
| 118 | self._proc.kill() |
| 119 | except ProcessLookupError: |
| 120 | pass |
| 121 | |
Victor Stinner | f2e43cb | 2015-01-30 01:20:44 +0100 | [diff] [blame] | 122 | # Don't clear the _proc reference yet: _post_init() may still run |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 123 | |
Victor Stinner | 978a9af | 2015-01-29 17:50:58 +0100 | [diff] [blame] | 124 | # On Python 3.3 and older, objects with a destructor part of a reference |
| 125 | # cycle are never destroyed. It's not more the case on Python 3.4 thanks |
| 126 | # to the PEP 442. |
Yury Selivanov | 2a8911c | 2015-08-04 15:56:33 -0400 | [diff] [blame] | 127 | if compat.PY34: |
Victor Stinner | 978a9af | 2015-01-29 17:50:58 +0100 | [diff] [blame] | 128 | def __del__(self): |
| 129 | if not self._closed: |
Victor Stinner | e19558a | 2016-03-23 00:28:08 +0100 | [diff] [blame] | 130 | warnings.warn("unclosed transport %r" % self, ResourceWarning, |
| 131 | source=self) |
Victor Stinner | 978a9af | 2015-01-29 17:50:58 +0100 | [diff] [blame] | 132 | self.close() |
| 133 | |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 134 | def get_pid(self): |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 135 | return self._pid |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 136 | |
| 137 | def get_returncode(self): |
| 138 | return self._returncode |
| 139 | |
| 140 | def get_pipe_transport(self, fd): |
| 141 | if fd in self._pipes: |
| 142 | return self._pipes[fd].pipe |
| 143 | else: |
| 144 | return None |
| 145 | |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 146 | def _check_proc(self): |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 147 | if self._proc is None: |
| 148 | raise ProcessLookupError() |
| 149 | |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 150 | def send_signal(self, signal): |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 151 | self._check_proc() |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 152 | self._proc.send_signal(signal) |
| 153 | |
| 154 | def terminate(self): |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 155 | self._check_proc() |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 156 | self._proc.terminate() |
| 157 | |
| 158 | def kill(self): |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 159 | self._check_proc() |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 160 | self._proc.kill() |
| 161 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 162 | @coroutine |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 163 | def _connect_pipes(self, waiter): |
Victor Stinner | f651a60 | 2015-01-14 02:10:33 +0100 | [diff] [blame] | 164 | try: |
| 165 | proc = self._proc |
| 166 | loop = self._loop |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 167 | |
Victor Stinner | f651a60 | 2015-01-14 02:10:33 +0100 | [diff] [blame] | 168 | if proc.stdin is not None: |
| 169 | _, pipe = yield from loop.connect_write_pipe( |
| 170 | lambda: WriteSubprocessPipeProto(self, 0), |
| 171 | proc.stdin) |
| 172 | self._pipes[0] = pipe |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 173 | |
Victor Stinner | f651a60 | 2015-01-14 02:10:33 +0100 | [diff] [blame] | 174 | if proc.stdout is not None: |
| 175 | _, pipe = yield from loop.connect_read_pipe( |
| 176 | lambda: ReadSubprocessPipeProto(self, 1), |
| 177 | proc.stdout) |
| 178 | self._pipes[1] = pipe |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 179 | |
Victor Stinner | f651a60 | 2015-01-14 02:10:33 +0100 | [diff] [blame] | 180 | if proc.stderr is not None: |
| 181 | _, pipe = yield from loop.connect_read_pipe( |
| 182 | lambda: ReadSubprocessPipeProto(self, 2), |
| 183 | proc.stderr) |
| 184 | self._pipes[2] = pipe |
Victor Stinner | aaabc4f | 2014-01-29 14:22:56 -0800 | [diff] [blame] | 185 | |
Victor Stinner | f651a60 | 2015-01-14 02:10:33 +0100 | [diff] [blame] | 186 | assert self._pending_calls is not None |
Victor Stinner | aaabc4f | 2014-01-29 14:22:56 -0800 | [diff] [blame] | 187 | |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 188 | loop.call_soon(self._protocol.connection_made, self) |
Victor Stinner | f651a60 | 2015-01-14 02:10:33 +0100 | [diff] [blame] | 189 | for callback, data in self._pending_calls: |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 190 | loop.call_soon(callback, *data) |
Victor Stinner | f651a60 | 2015-01-14 02:10:33 +0100 | [diff] [blame] | 191 | self._pending_calls = None |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 192 | except Exception as exc: |
| 193 | if waiter is not None and not waiter.cancelled(): |
| 194 | waiter.set_exception(exc) |
| 195 | else: |
| 196 | if waiter is not None and not waiter.cancelled(): |
| 197 | waiter.set_result(None) |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 198 | |
| 199 | def _call(self, cb, *data): |
| 200 | if self._pending_calls is not None: |
| 201 | self._pending_calls.append((cb, data)) |
| 202 | else: |
| 203 | self._loop.call_soon(cb, *data) |
| 204 | |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 205 | def _pipe_connection_lost(self, fd, exc): |
| 206 | self._call(self._protocol.pipe_connection_lost, fd, exc) |
| 207 | self._try_finish() |
| 208 | |
| 209 | def _pipe_data_received(self, fd, data): |
| 210 | self._call(self._protocol.pipe_data_received, fd, data) |
| 211 | |
| 212 | def _process_exited(self, returncode): |
| 213 | assert returncode is not None, returncode |
| 214 | assert self._returncode is None, self._returncode |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 215 | if self._loop.get_debug(): |
| 216 | logger.info('%r exited with return code %r', |
| 217 | self, returncode) |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 218 | self._returncode = returncode |
Victor Stinner | b0d43ce | 2016-05-20 13:05:48 +0200 | [diff] [blame] | 219 | if self._proc.returncode is None: |
| 220 | # asyncio uses a child watcher: copy the status into the Popen |
| 221 | # object. On Python 3.6, it is required to avoid a ResourceWarning. |
| 222 | self._proc.returncode = returncode |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 223 | self._call(self._protocol.process_exited) |
| 224 | self._try_finish() |
| 225 | |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 226 | # wake up futures waiting for wait() |
| 227 | for waiter in self._exit_waiters: |
| 228 | if not waiter.cancelled(): |
| 229 | waiter.set_result(returncode) |
| 230 | self._exit_waiters = None |
| 231 | |
Victor Stinner | d6dc7bd | 2015-03-18 11:37:42 +0100 | [diff] [blame] | 232 | @coroutine |
Victor Stinner | 1241ecc | 2015-01-30 00:16:14 +0100 | [diff] [blame] | 233 | def _wait(self): |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 234 | """Wait until the process exit and return the process return code. |
| 235 | |
| 236 | This method is a coroutine.""" |
| 237 | if self._returncode is not None: |
| 238 | return self._returncode |
| 239 | |
Yury Selivanov | 7661db6 | 2016-05-16 15:38:39 -0400 | [diff] [blame] | 240 | waiter = self._loop.create_future() |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 241 | self._exit_waiters.append(waiter) |
| 242 | return (yield from waiter) |
| 243 | |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 244 | def _try_finish(self): |
| 245 | assert not self._finished |
| 246 | if self._returncode is None: |
| 247 | return |
| 248 | if all(p is not None and p.disconnected |
| 249 | for p in self._pipes.values()): |
| 250 | self._finished = True |
Victor Stinner | 1b9763d | 2014-12-18 23:47:27 +0100 | [diff] [blame] | 251 | self._call(self._call_connection_lost, None) |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 252 | |
| 253 | def _call_connection_lost(self, exc): |
| 254 | try: |
| 255 | self._protocol.connection_lost(exc) |
| 256 | finally: |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 257 | self._loop = None |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 258 | self._proc = None |
| 259 | self._protocol = None |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 260 | |
| 261 | |
| 262 | class WriteSubprocessPipeProto(protocols.BaseProtocol): |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 263 | |
| 264 | def __init__(self, proc, fd): |
| 265 | self.proc = proc |
| 266 | self.fd = fd |
Victor Stinner | aaabc4f | 2014-01-29 14:22:56 -0800 | [diff] [blame] | 267 | self.pipe = None |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 268 | self.disconnected = False |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 269 | |
| 270 | def connection_made(self, transport): |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 271 | self.pipe = transport |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 272 | |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 273 | def __repr__(self): |
| 274 | return ('<%s fd=%s pipe=%r>' |
| 275 | % (self.__class__.__name__, self.fd, self.pipe)) |
| 276 | |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 277 | def connection_lost(self, exc): |
| 278 | self.disconnected = True |
| 279 | self.proc._pipe_connection_lost(self.fd, exc) |
Victor Stinner | 587feb1 | 2015-01-09 21:34:27 +0100 | [diff] [blame] | 280 | self.proc = None |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 281 | |
Guido van Rossum | 1e9a446 | 2014-01-29 14:28:15 -0800 | [diff] [blame] | 282 | def pause_writing(self): |
| 283 | self.proc._protocol.pause_writing() |
| 284 | |
| 285 | def resume_writing(self): |
| 286 | self.proc._protocol.resume_writing() |
Guido van Rossum | 0016e1d | 2013-10-30 14:56:49 -0700 | [diff] [blame] | 287 | |
| 288 | |
| 289 | class ReadSubprocessPipeProto(WriteSubprocessPipeProto, |
| 290 | protocols.Protocol): |
| 291 | |
| 292 | def data_received(self, data): |
| 293 | self.proc._pipe_data_received(self.fd, data) |