Debian Codegraph @

Lib/asyncio/base_subprocess.py

raw
1 import collections
2 import subprocess
3 import warnings
4 import os
5 import signal
6 import sys
7
8 from . import protocols
9 from . import transports
10 from .log import logger
11
12
13 class BaseSubprocessTransport(transports.SubprocessTransport):
14
15 def __init__(self, loop, protocol, args, shell,
16 stdin, stdout, stderr, bufsize,
17 waiter=None, extra=None, **kwargs):
18 super().__init__(extra)
19 self._closed = False
20 self._protocol = protocol
21 self._loop = loop
22 self._proc = None
23 self._pid = None
24 self._returncode = None
25 self._exit_waiters = []
26 self._pending_calls = collections.deque()
27 self._pipes = {}
28 self._finished = False
29
30 if stdin == subprocess.PIPE:
31 self._pipes[0] = None
32 if stdout == subprocess.PIPE:
33 self._pipes[1] = None
34 if stderr == subprocess.PIPE:
35 self._pipes[2] = None
36
37 # Create the child process: set the _proc attribute
38 try:
39 self._start(args=args, shell=shell, stdin=stdin, stdout=stdout,
40 stderr=stderr, bufsize=bufsize, **kwargs)
41 except:
42 self.close()
43 raise
44
45 self._pid = self._proc.pid
46 self._extra['subprocess'] = self._proc
47
48 if self._loop.get_debug():
49 if isinstance(args, (bytes, str)):
50 program = args
51 else:
52 program = args[0]
53 logger.debug('process %r created: pid %s',
54 program, self._pid)
55
56 self._loop.create_task(self._connect_pipes(waiter))
57
58 def __repr__(self):
59 info = [self.__class__.__name__]
60 if self._closed:
61 info.append('closed')
62 if self._pid is not None:
63 info.append(f'pid={self._pid}')
64 if self._returncode is not None:
65 info.append(f'returncode={self._returncode}')
66 elif self._pid is not None:
67 info.append('running')
68 else:
69 info.append('not started')
70
71 stdin = self._pipes.get(0)
72 if stdin is not None:
73 info.append(f'stdin={stdin.pipe}')
74
75 stdout = self._pipes.get(1)
76 stderr = self._pipes.get(2)
77 if stdout is not None and stderr is stdout:
78 info.append(f'stdout=stderr={stdout.pipe}')
79 else:
80 if stdout is not None:
81 info.append(f'stdout={stdout.pipe}')
82 if stderr is not None:
83 info.append(f'stderr={stderr.pipe}')
84
85 return '<{}>'.format(' '.join(info))
86
87 def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs):
88 raise NotImplementedError
89
90 def set_protocol(self, protocol):
91 self._protocol = protocol
92
93 def get_protocol(self):
94 return self._protocol
95
96 def is_closing(self):
97 return self._closed
98
99 def close(self):
100 if self._closed:
101 return
102 self._closed = True
103
104 for proto in self._pipes.values():
105 if proto is None:
106 continue
107 # See gh-114177
108 # skip closing the pipe if loop is already closed
109 # this can happen e.g. when loop is closed immediately after
110 # process is killed
111 if self._loop and not self._loop.is_closed():
112 proto.pipe.close()
113
114 if (self._proc is not None and
115 # has the child process finished?
116 self._returncode is None and
117 # the child process has finished, but the
118 # transport hasn't been notified yet?
119 self._proc.poll() is None):
120
121 if self._loop.get_debug():
122 logger.warning('Close running child process: kill %r', self)
123
124 try:
125 self._proc.kill()
126 except (ProcessLookupError, PermissionError):
127 # the process may have already exited or may be running setuid
128 pass
129
130 # Don't clear the _proc reference yet: _post_init() may still run
131
132 def __del__(self, _warn=warnings.warn):
133 if not self._closed:
134 _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
135 self.close()
136
137 def get_pid(self):
138 return self._pid
139
140 def get_returncode(self):
141 return self._returncode
142
143 def get_pipe_transport(self, fd):
144 if fd in self._pipes:
145 return self._pipes[fd].pipe
146 else:
147 return None
148
149 def _check_proc(self):
150 if self._proc is None:
151 raise ProcessLookupError()
152
153 if sys.platform == 'win32':
154 def send_signal(self, signal):
155 self._check_proc()
156 self._proc.send_signal(signal)
157
158 def terminate(self):
159 self._check_proc()
160 self._proc.terminate()
161
162 def kill(self):
163 self._check_proc()
164 self._proc.kill()
165 else:
166 def send_signal(self, signal):
167 self._check_proc()
168 try:
169 os.kill(self._proc.pid, signal)
170 except ProcessLookupError:
171 pass
172
173 def terminate(self):
174 self.send_signal(signal.SIGTERM)
175
176 def kill(self):
177 self.send_signal(signal.SIGKILL)
178
179 async def _connect_pipes(self, waiter):
180 try:
181 proc = self._proc
182 loop = self._loop
183
184 if proc.stdin is not None:
185 _, pipe = await loop.connect_write_pipe(
186 lambda: WriteSubprocessPipeProto(self, 0),
187 proc.stdin)
188 self._pipes[0] = pipe
189
190 if proc.stdout is not None:
191 _, pipe = await loop.connect_read_pipe(
192 lambda: ReadSubprocessPipeProto(self, 1),
193 proc.stdout)
194 self._pipes[1] = pipe
195
196 if proc.stderr is not None:
197 _, pipe = await loop.connect_read_pipe(
198 lambda: ReadSubprocessPipeProto(self, 2),
199 proc.stderr)
200 self._pipes[2] = pipe
201
202 assert self._pending_calls is not None
203
204 loop.call_soon(self._protocol.connection_made, self)
205 for callback, data in self._pending_calls:
206 loop.call_soon(callback, *data)
207 self._pending_calls = None
208 except (SystemExit, KeyboardInterrupt):
209 raise
210 except BaseException as exc:
211 if waiter is not None and not waiter.cancelled():
212 waiter.set_exception(exc)
213 else:
214 if waiter is not None and not waiter.cancelled():
215 waiter.set_result(None)
216
217 def _call(self, cb, *data):
218 if self._pending_calls is not None:
219 self._pending_calls.append((cb, data))
220 else:
221 self._loop.call_soon(cb, *data)
222
223 def _pipe_connection_lost(self, fd, exc):
224 self._call(self._protocol.pipe_connection_lost, fd, exc)
225 self._try_finish()
226
227 def _pipe_data_received(self, fd, data):
228 self._call(self._protocol.pipe_data_received, fd, data)
229
230 def _process_exited(self, returncode):
231 assert returncode is not None, returncode
232 assert self._returncode is None, self._returncode
233 if self._loop.get_debug():
234 logger.info('%r exited with return code %r', self, returncode)
235 self._returncode = returncode
236
237 if self._proc.returncode is None:
238 # asyncio uses a child watcher: copy the status into the Popen
239 # object. On Python 3.6, it is required to avoid a ResourceWarning.
240 self._proc.returncode = returncode
241 self._call(self._protocol.process_exited)
242
243 self._try_finish()
244
245 # gh-119710: Wake up futures waiting for wait() as soon as the process
246 # exits.
247 for waiter in self._exit_waiters:
248 if not waiter.done():
249 waiter.set_result(returncode)
250 self._exit_waiters = None
251
252 async def _wait(self):
253 """Wait until the process exit and return the process return code.
254
255 This method is a coroutine."""
256 if self._returncode is not None:
257 return self._returncode
258
259 waiter = self._loop.create_future()
260 self._exit_waiters.append(waiter)
261 return await waiter
262
263 def _try_finish(self):
264 assert not self._finished
265 if self._returncode is None:
266 return
267
268 if all(p is not None and p.disconnected
269 for p in self._pipes.values()):
270 self._finished = True
271 self._call(self._call_connection_lost, None)
272
273 def _call_connection_lost(self, exc):
274 try:
275 self._protocol.connection_lost(exc)
276 finally:
277 self._loop = None
278 self._proc = None
279 self._protocol = None
280
281
282 class WriteSubprocessPipeProto(protocols.BaseProtocol):
283
284 def __init__(self, proc, fd):
285 self.proc = proc
286 self.fd = fd
287 self.pipe = None
288 self.disconnected = False
289
290 def connection_made(self, transport):
291 self.pipe = transport
292
293 def __repr__(self):
294 return f'<{self.__class__.__name__} fd={self.fd} pipe={self.pipe!r}>'
295
296 def connection_lost(self, exc):
297 self.disconnected = True
298 self.proc._pipe_connection_lost(self.fd, exc)
299 self.proc = None
300
301 def pause_writing(self):
302 self.proc._protocol.pause_writing()
303
304 def resume_writing(self):
305 self.proc._protocol.resume_writing()
306
307
308 class ReadSubprocessPipeProto(WriteSubprocessPipeProto,
309 protocols.Protocol):
310
311 def data_received(self, data):
312 self.proc._pipe_data_received(self.fd, data)