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