blob: 7e5a901845d270c9ae710cbcc7c73c6ae6bd8a8b [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:
60 info.append('pid=%s' % self._pid)
Victor Stinneracdb7822014-07-14 18:33:40 +020061 if self._returncode is not None:
62 info.append('returncode=%s' % 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:
70 info.append('stdin=%s' % stdin.pipe)
71
72 stdout = self._pipes.get(1)
73 stderr = self._pipes.get(2)
74 if stdout is not None and stderr is stdout:
75 info.append('stdout=stderr=%s' % stdout.pipe)
76 else:
77 if stdout is not None:
78 info.append('stdout=%s' % stdout.pipe)
79 if stderr is not None:
80 info.append('stderr=%s' % stderr.pipe)
81
82 return '<%s>' % ' '.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
Victor Stinner8e368122015-02-10 14:49:32 +0100106 if (self._proc is not None
107 # the child process finished?
108 and self._returncode is None
109 # the child process finished but the transport was not notified yet?
110 and self._proc.poll() is None
111 ):
Victor Stinner47cd10d2015-01-30 00:05:19 +0100112 if self._loop.get_debug():
113 logger.warning('Close running child process: kill %r', self)
114
115 try:
116 self._proc.kill()
117 except ProcessLookupError:
118 pass
119
Victor Stinnerf2e43cb2015-01-30 01:20:44 +0100120 # Don't clear the _proc reference yet: _post_init() may still run
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700121
INADA Naoki3e2ad8e2017-04-25 10:57:18 +0900122 def __del__(self):
123 if not self._closed:
124 warnings.warn("unclosed transport %r" % self, ResourceWarning,
125 source=self)
126 self.close()
Victor Stinner978a9af2015-01-29 17:50:58 +0100127
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700128 def get_pid(self):
Victor Stinneracdb7822014-07-14 18:33:40 +0200129 return self._pid
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700130
131 def get_returncode(self):
132 return self._returncode
133
134 def get_pipe_transport(self, fd):
135 if fd in self._pipes:
136 return self._pipes[fd].pipe
137 else:
138 return None
139
Victor Stinner47cd10d2015-01-30 00:05:19 +0100140 def _check_proc(self):
Victor Stinner47cd10d2015-01-30 00:05:19 +0100141 if self._proc is None:
142 raise ProcessLookupError()
143
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700144 def send_signal(self, signal):
Victor Stinner47cd10d2015-01-30 00:05:19 +0100145 self._check_proc()
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700146 self._proc.send_signal(signal)
147
148 def terminate(self):
Victor Stinner47cd10d2015-01-30 00:05:19 +0100149 self._check_proc()
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700150 self._proc.terminate()
151
152 def kill(self):
Victor Stinner47cd10d2015-01-30 00:05:19 +0100153 self._check_proc()
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700154 self._proc.kill()
155
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200156 async def _connect_pipes(self, waiter):
Victor Stinnerf651a602015-01-14 02:10:33 +0100157 try:
158 proc = self._proc
159 loop = self._loop
Victor Stinner47cd10d2015-01-30 00:05:19 +0100160
Victor Stinnerf651a602015-01-14 02:10:33 +0100161 if proc.stdin is not None:
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200162 _, pipe = await loop.connect_write_pipe(
Victor Stinnerf651a602015-01-14 02:10:33 +0100163 lambda: WriteSubprocessPipeProto(self, 0),
164 proc.stdin)
165 self._pipes[0] = pipe
Victor Stinner47cd10d2015-01-30 00:05:19 +0100166
Victor Stinnerf651a602015-01-14 02:10:33 +0100167 if proc.stdout is not None:
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200168 _, pipe = await loop.connect_read_pipe(
Victor Stinnerf651a602015-01-14 02:10:33 +0100169 lambda: ReadSubprocessPipeProto(self, 1),
170 proc.stdout)
171 self._pipes[1] = pipe
Victor Stinner47cd10d2015-01-30 00:05:19 +0100172
Victor Stinnerf651a602015-01-14 02:10:33 +0100173 if proc.stderr is not None:
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200174 _, pipe = await loop.connect_read_pipe(
Victor Stinnerf651a602015-01-14 02:10:33 +0100175 lambda: ReadSubprocessPipeProto(self, 2),
176 proc.stderr)
177 self._pipes[2] = pipe
Victor Stinneraaabc4f2014-01-29 14:22:56 -0800178
Victor Stinnerf651a602015-01-14 02:10:33 +0100179 assert self._pending_calls is not None
Victor Stinneraaabc4f2014-01-29 14:22:56 -0800180
Victor Stinner47cd10d2015-01-30 00:05:19 +0100181 loop.call_soon(self._protocol.connection_made, self)
Victor Stinnerf651a602015-01-14 02:10:33 +0100182 for callback, data in self._pending_calls:
Victor Stinner47cd10d2015-01-30 00:05:19 +0100183 loop.call_soon(callback, *data)
Victor Stinnerf651a602015-01-14 02:10:33 +0100184 self._pending_calls = None
Victor Stinner47cd10d2015-01-30 00:05:19 +0100185 except Exception as exc:
186 if waiter is not None and not waiter.cancelled():
187 waiter.set_exception(exc)
188 else:
189 if waiter is not None and not waiter.cancelled():
190 waiter.set_result(None)
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700191
192 def _call(self, cb, *data):
193 if self._pending_calls is not None:
194 self._pending_calls.append((cb, data))
195 else:
196 self._loop.call_soon(cb, *data)
197
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700198 def _pipe_connection_lost(self, fd, exc):
199 self._call(self._protocol.pipe_connection_lost, fd, exc)
200 self._try_finish()
201
202 def _pipe_data_received(self, fd, data):
203 self._call(self._protocol.pipe_data_received, fd, data)
204
205 def _process_exited(self, returncode):
206 assert returncode is not None, returncode
207 assert self._returncode is None, self._returncode
Victor Stinneracdb7822014-07-14 18:33:40 +0200208 if self._loop.get_debug():
209 logger.info('%r exited with return code %r',
210 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):
266 return ('<%s fd=%s pipe=%r>'
267 % (self.__class__.__name__, self.fd, self.pipe))
268
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700269 def connection_lost(self, exc):
270 self.disconnected = True
271 self.proc._pipe_connection_lost(self.fd, exc)
Victor Stinner587feb12015-01-09 21:34:27 +0100272 self.proc = None
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700273
Guido van Rossum1e9a4462014-01-29 14:28:15 -0800274 def pause_writing(self):
275 self.proc._protocol.pause_writing()
276
277 def resume_writing(self):
278 self.proc._protocol.resume_writing()
Guido van Rossum0016e1d2013-10-30 14:56:49 -0700279
280
281class ReadSubprocessPipeProto(WriteSubprocessPipeProto,
282 protocols.Protocol):
283
284 def data_received(self, data):
285 self.proc._pipe_data_received(self.fd, data)