blob: 7c17066f8bb26c9af260bd623331f7ed3ffe48b8 [file] [log] [blame]
Guido van Rossum0016e1d2013-10-30 14:56:49 -07001import collections
2import subprocess
Victor Stinner978a9af2015-01-29 17:50:58 +01003import warnings
Guido van Rossum0016e1d2013-10-30 14:56:49 -07004
5from . import protocols
Guido van Rossum0016e1d2013-10-30 14:56:49 -07006from . import transports
Victor Stinneracdb7822014-07-14 18:33:40 +02007from .log import logger
Guido van Rossum0016e1d2013-10-30 14:56:49 -07008
9
Guido van Rossum0016e1d2013-10-30 14:56:49 -070010class BaseSubprocessTransport(transports.SubprocessTransport):
11
12 def __init__(self, loop, protocol, args, shell,
13 stdin, stdout, stderr, bufsize,
Victor Stinner47cd10d2015-01-30 00:05:19 +010014 waiter=None, extra=None, **kwargs):
Guido van Rossum0016e1d2013-10-30 14:56:49 -070015 super().__init__(extra)
Victor Stinner978a9af2015-01-29 17:50:58 +010016 self._closed = False
Guido van Rossum0016e1d2013-10-30 14:56:49 -070017 self._protocol = protocol
18 self._loop = loop
Victor Stinner47cd10d2015-01-30 00:05:19 +010019 self._proc = None
Victor Stinneracdb7822014-07-14 18:33:40 +020020 self._pid = None
Victor Stinner47cd10d2015-01-30 00:05:19 +010021 self._returncode = None
22 self._exit_waiters = []
23 self._pending_calls = collections.deque()
Guido van Rossum0016e1d2013-10-30 14:56:49 -070024 self._pipes = {}
Victor Stinner47cd10d2015-01-30 00:05:19 +010025 self._finished = False
26
Guido van Rossum0016e1d2013-10-30 14:56:49 -070027 if stdin == subprocess.PIPE:
Victor Stinner915bcb02014-02-01 22:49:59 +010028 self._pipes[0] = None
Guido van Rossum0016e1d2013-10-30 14:56:49 -070029 if stdout == subprocess.PIPE:
Victor Stinner915bcb02014-02-01 22:49:59 +010030 self._pipes[1] = None
Guido van Rossum0016e1d2013-10-30 14:56:49 -070031 if stderr == subprocess.PIPE:
Victor Stinner915bcb02014-02-01 22:49:59 +010032 self._pipes[2] = None
Victor Stinner47cd10d2015-01-30 00:05:19 +010033
34 # Create the child process: set the _proc attribute
Victor Stinner6fb1e742015-07-31 17:49:43 +020035 try:
36 self._start(args=args, shell=shell, stdin=stdin, stdout=stdout,
37 stderr=stderr, bufsize=bufsize, **kwargs)
38 except:
39 self.close()
40 raise
41
Victor Stinneracdb7822014-07-14 18:33:40 +020042 self._pid = self._proc.pid
Guido van Rossum0016e1d2013-10-30 14:56:49 -070043 self._extra['subprocess'] = self._proc
Victor Stinner47cd10d2015-01-30 00:05:19 +010044
Victor Stinneracdb7822014-07-14 18:33:40 +020045 if self._loop.get_debug():
46 if isinstance(args, (bytes, str)):
47 program = args
48 else:
49 program = args[0]
50 logger.debug('process %r created: pid %s',
51 program, self._pid)
52
Victor Stinner47cd10d2015-01-30 00:05:19 +010053 self._loop.create_task(self._connect_pipes(waiter))
54
Victor Stinneracdb7822014-07-14 18:33:40 +020055 def __repr__(self):
Victor Stinner978a9af2015-01-29 17:50:58 +010056 info = [self.__class__.__name__]
57 if self._closed:
58 info.append('closed')
Victor Stinner7a82afe2015-03-10 16:32:29 +010059 if self._pid is not None:
Yury Selivanov6370f342017-12-10 18:36:12 -050060 info.append(f'pid={self.pid}')
Victor Stinneracdb7822014-07-14 18:33:40 +020061 if self._returncode is not None:
Yury Selivanov6370f342017-12-10 18:36:12 -050062 info.append(f'returncode={self._returncode}')
Victor Stinner7a82afe2015-03-10 16:32:29 +010063 elif self._pid is not None:
Victor Stinner4e82fb92015-02-17 22:50:33 +010064 info.append('running')
Victor Stinner7a82afe2015-03-10 16:32:29 +010065 else:
66 info.append('not started')
Victor Stinneracdb7822014-07-14 18:33:40 +020067
68 stdin = self._pipes.get(0)
69 if stdin is not None:
Yury Selivanov6370f342017-12-10 18:36:12 -050070 info.append(f'stdin={stdin.pipe}')
Victor Stinneracdb7822014-07-14 18:33:40 +020071
72 stdout = self._pipes.get(1)
73 stderr = self._pipes.get(2)
74 if stdout is not None and stderr is stdout:
Yury Selivanov6370f342017-12-10 18:36:12 -050075 info.append(f'stdout=stderr={stdout.pipe}')
Victor Stinneracdb7822014-07-14 18:33:40 +020076 else:
77 if stdout is not None:
Yury Selivanov6370f342017-12-10 18:36:12 -050078 info.append(f'stdout={stdout.pipe}')
Victor Stinneracdb7822014-07-14 18:33:40 +020079 if stderr is not None:
Yury Selivanov6370f342017-12-10 18:36:12 -050080 info.append(f'stderr={stderr.pipe}')
Victor Stinneracdb7822014-07-14 18:33:40 +020081
Yury Selivanov6370f342017-12-10 18:36:12 -050082 return '<{}>'.format(' '.join(info))
Guido van Rossum0016e1d2013-10-30 14:56:49 -070083
84 def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs):
85 raise NotImplementedError
86
Yury Selivanova05a6ef2016-09-11 21:11:02 -040087 def set_protocol(self, protocol):
88 self._protocol = protocol
89
90 def get_protocol(self):
91 return self._protocol
92
Yury Selivanov5bb1afb2015-11-16 12:43:21 -050093 def is_closing(self):
94 return self._closed
95
Guido van Rossum0016e1d2013-10-30 14:56:49 -070096 def close(self):
Victor Stinnerf2e43cb2015-01-30 01:20:44 +010097 if self._closed:
98 return
Victor Stinner978a9af2015-01-29 17:50:58 +010099 self._closed = True
Victor Stinner47cd10d2015-01-30 00:05:19 +0100100
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700101 for proto in self._pipes.values():
Victor Stinner29ad0112015-01-15 00:04:21 +0100102 if proto is None:
103 continue
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700104 proto.pipe.close()
Victor Stinner47cd10d2015-01-30 00:05:19 +0100105
Yury Selivanov6370f342017-12-10 18:36:12 -0500106 if (self._proc is not None and
107 # has the child process finished?
108 self._returncode is None and
109 # the child process has finished, but the
110 # transport hasn't been notified yet?
111 self._proc.poll() is None):
112
Victor Stinner47cd10d2015-01-30 00:05:19 +0100113 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 Stinnerf2e43cb2015-01-30 01:20:44 +0100121 # Don't clear the _proc reference yet: _post_init() may still run
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700122
INADA Naoki3e2ad8e2017-04-25 10:57:18 +0900123 def __del__(self):
124 if not self._closed:
Yury Selivanov6370f342017-12-10 18:36:12 -0500125 warnings.warn(f"unclosed transport {self!r}", ResourceWarning,
INADA Naoki3e2ad8e2017-04-25 10:57:18 +0900126 source=self)
127 self.close()
Victor Stinner978a9af2015-01-29 17:50:58 +0100128
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700129 def get_pid(self):
Victor Stinneracdb7822014-07-14 18:33:40 +0200130 return self._pid
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700131
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 Stinner47cd10d2015-01-30 00:05:19 +0100141 def _check_proc(self):
Victor Stinner47cd10d2015-01-30 00:05:19 +0100142 if self._proc is None:
143 raise ProcessLookupError()
144
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700145 def send_signal(self, signal):
Victor Stinner47cd10d2015-01-30 00:05:19 +0100146 self._check_proc()
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700147 self._proc.send_signal(signal)
148
149 def terminate(self):
Victor Stinner47cd10d2015-01-30 00:05:19 +0100150 self._check_proc()
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700151 self._proc.terminate()
152
153 def kill(self):
Victor Stinner47cd10d2015-01-30 00:05:19 +0100154 self._check_proc()
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700155 self._proc.kill()
156
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200157 async def _connect_pipes(self, waiter):
Victor Stinnerf651a602015-01-14 02:10:33 +0100158 try:
159 proc = self._proc
160 loop = self._loop
Victor Stinner47cd10d2015-01-30 00:05:19 +0100161
Victor Stinnerf651a602015-01-14 02:10:33 +0100162 if proc.stdin is not None:
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200163 _, pipe = await loop.connect_write_pipe(
Victor Stinnerf651a602015-01-14 02:10:33 +0100164 lambda: WriteSubprocessPipeProto(self, 0),
165 proc.stdin)
166 self._pipes[0] = pipe
Victor Stinner47cd10d2015-01-30 00:05:19 +0100167
Victor Stinnerf651a602015-01-14 02:10:33 +0100168 if proc.stdout is not None:
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200169 _, pipe = await loop.connect_read_pipe(
Victor Stinnerf651a602015-01-14 02:10:33 +0100170 lambda: ReadSubprocessPipeProto(self, 1),
171 proc.stdout)
172 self._pipes[1] = pipe
Victor Stinner47cd10d2015-01-30 00:05:19 +0100173
Victor Stinnerf651a602015-01-14 02:10:33 +0100174 if proc.stderr is not None:
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200175 _, pipe = await loop.connect_read_pipe(
Victor Stinnerf651a602015-01-14 02:10:33 +0100176 lambda: ReadSubprocessPipeProto(self, 2),
177 proc.stderr)
178 self._pipes[2] = pipe
Victor Stinneraaabc4f2014-01-29 14:22:56 -0800179
Victor Stinnerf651a602015-01-14 02:10:33 +0100180 assert self._pending_calls is not None
Victor Stinneraaabc4f2014-01-29 14:22:56 -0800181
Victor Stinner47cd10d2015-01-30 00:05:19 +0100182 loop.call_soon(self._protocol.connection_made, self)
Victor Stinnerf651a602015-01-14 02:10:33 +0100183 for callback, data in self._pending_calls:
Victor Stinner47cd10d2015-01-30 00:05:19 +0100184 loop.call_soon(callback, *data)
Victor Stinnerf651a602015-01-14 02:10:33 +0100185 self._pending_calls = None
Victor Stinner47cd10d2015-01-30 00:05:19 +0100186 except Exception as exc:
187 if waiter is not None and not waiter.cancelled():
188 waiter.set_exception(exc)
189 else:
190 if waiter is not None and not waiter.cancelled():
191 waiter.set_result(None)
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700192
193 def _call(self, cb, *data):
194 if self._pending_calls is not None:
195 self._pending_calls.append((cb, data))
196 else:
197 self._loop.call_soon(cb, *data)
198
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700199 def _pipe_connection_lost(self, fd, exc):
200 self._call(self._protocol.pipe_connection_lost, fd, exc)
201 self._try_finish()
202
203 def _pipe_data_received(self, fd, data):
204 self._call(self._protocol.pipe_data_received, fd, data)
205
206 def _process_exited(self, returncode):
207 assert returncode is not None, returncode
208 assert self._returncode is None, self._returncode
Victor Stinneracdb7822014-07-14 18:33:40 +0200209 if self._loop.get_debug():
Yury Selivanov6370f342017-12-10 18:36:12 -0500210 logger.info('%r exited with return code %r', self, returncode)
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700211 self._returncode = returncode
Victor Stinnerb0d43ce2016-05-20 13:05:48 +0200212 if self._proc.returncode is None:
213 # asyncio uses a child watcher: copy the status into the Popen
214 # object. On Python 3.6, it is required to avoid a ResourceWarning.
215 self._proc.returncode = returncode
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700216 self._call(self._protocol.process_exited)
217 self._try_finish()
218
Victor Stinner47cd10d2015-01-30 00:05:19 +0100219 # wake up futures waiting for wait()
220 for waiter in self._exit_waiters:
221 if not waiter.cancelled():
222 waiter.set_result(returncode)
223 self._exit_waiters = None
224
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200225 async def _wait(self):
Victor Stinner47cd10d2015-01-30 00:05:19 +0100226 """Wait until the process exit and return the process return code.
227
228 This method is a coroutine."""
229 if self._returncode is not None:
230 return self._returncode
231
Yury Selivanov7661db62016-05-16 15:38:39 -0400232 waiter = self._loop.create_future()
Victor Stinner47cd10d2015-01-30 00:05:19 +0100233 self._exit_waiters.append(waiter)
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200234 return await waiter
Victor Stinner47cd10d2015-01-30 00:05:19 +0100235
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700236 def _try_finish(self):
237 assert not self._finished
238 if self._returncode is None:
239 return
240 if all(p is not None and p.disconnected
241 for p in self._pipes.values()):
242 self._finished = True
Victor Stinner1b9763d2014-12-18 23:47:27 +0100243 self._call(self._call_connection_lost, None)
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700244
245 def _call_connection_lost(self, exc):
246 try:
247 self._protocol.connection_lost(exc)
248 finally:
Victor Stinner47cd10d2015-01-30 00:05:19 +0100249 self._loop = None
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700250 self._proc = None
251 self._protocol = None
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700252
253
254class WriteSubprocessPipeProto(protocols.BaseProtocol):
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700255
256 def __init__(self, proc, fd):
257 self.proc = proc
258 self.fd = fd
Victor Stinneraaabc4f2014-01-29 14:22:56 -0800259 self.pipe = None
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700260 self.disconnected = False
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700261
262 def connection_made(self, transport):
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700263 self.pipe = transport
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700264
Victor Stinneracdb7822014-07-14 18:33:40 +0200265 def __repr__(self):
Yury Selivanov6370f342017-12-10 18:36:12 -0500266 return f'<{self.__class__.__name__} fd={self.fd} pipe={self.pipe!r}>'
Victor Stinneracdb7822014-07-14 18:33:40 +0200267
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700268 def connection_lost(self, exc):
269 self.disconnected = True
270 self.proc._pipe_connection_lost(self.fd, exc)
Victor Stinner587feb12015-01-09 21:34:27 +0100271 self.proc = None
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700272
Guido van Rossum1e9a4462014-01-29 14:28:15 -0800273 def pause_writing(self):
274 self.proc._protocol.pause_writing()
275
276 def resume_writing(self):
277 self.proc._protocol.resume_writing()
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700278
279
280class ReadSubprocessPipeProto(WriteSubprocessPipeProto,
281 protocols.Protocol):
282
283 def data_received(self, data):
284 self.proc._pipe_data_received(self.fd, data)