Yury Selivanov | dec1a45 | 2014-02-18 22:27:48 -0500 | [diff] [blame] | 1 | """Selector event loop for Unix with signal handling.""" |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 2 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 3 | import errno |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 4 | import os |
| 5 | import signal |
| 6 | import socket |
| 7 | import stat |
| 8 | import subprocess |
| 9 | import sys |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 10 | import threading |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 11 | |
| 12 | |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 13 | from . import base_events |
Guido van Rossum | 5969128 | 2013-10-30 14:52:03 -0700 | [diff] [blame] | 14 | from . import base_subprocess |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 15 | from . import constants |
| 16 | from . import events |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 17 | from . import selector_events |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 18 | from . import selectors |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 19 | from . import transports |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 20 | from .coroutines import coroutine |
Guido van Rossum | fc29e0f | 2013-10-17 15:39:45 -0700 | [diff] [blame] | 21 | from .log import logger |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 22 | |
| 23 | |
Victor Stinner | 915bcb0 | 2014-02-01 22:49:59 +0100 | [diff] [blame] | 24 | __all__ = ['SelectorEventLoop', |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 25 | 'AbstractChildWatcher', 'SafeChildWatcher', |
| 26 | 'FastChildWatcher', 'DefaultEventLoopPolicy', |
| 27 | ] |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 28 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 29 | if sys.platform == 'win32': # pragma: no cover |
| 30 | raise ImportError('Signals are not really supported on Windows') |
| 31 | |
| 32 | |
Victor Stinner | fe5649c | 2014-07-17 22:43:40 +0200 | [diff] [blame] | 33 | def _sighandler_noop(signum, frame): |
| 34 | """Dummy signal handler.""" |
| 35 | pass |
| 36 | |
| 37 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 38 | class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop): |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 39 | """Unix event loop. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 40 | |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 41 | Adds signal handling and UNIX Domain Socket support to SelectorEventLoop. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 42 | """ |
| 43 | |
| 44 | def __init__(self, selector=None): |
| 45 | super().__init__(selector) |
| 46 | self._signal_handlers = {} |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 47 | |
| 48 | def _socketpair(self): |
| 49 | return socket.socketpair() |
| 50 | |
Guido van Rossum | 0b69fbc | 2013-11-06 20:25:50 -0800 | [diff] [blame] | 51 | def close(self): |
Victor Stinner | f328c7d | 2014-06-23 01:02:37 +0200 | [diff] [blame] | 52 | super().close() |
Guido van Rossum | 0b69fbc | 2013-11-06 20:25:50 -0800 | [diff] [blame] | 53 | for sig in list(self._signal_handlers): |
| 54 | self.remove_signal_handler(sig) |
Guido van Rossum | 0b69fbc | 2013-11-06 20:25:50 -0800 | [diff] [blame] | 55 | |
Victor Stinner | fe5649c | 2014-07-17 22:43:40 +0200 | [diff] [blame] | 56 | def _process_self_data(self, data): |
| 57 | for signum in data: |
| 58 | if not signum: |
| 59 | # ignore null bytes written by _write_to_self() |
| 60 | continue |
| 61 | self._handle_signal(signum) |
| 62 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 63 | def add_signal_handler(self, sig, callback, *args): |
| 64 | """Add a handler for a signal. UNIX only. |
| 65 | |
| 66 | Raise ValueError if the signal number is invalid or uncatchable. |
| 67 | Raise RuntimeError if there is a problem setting up the handler. |
| 68 | """ |
| 69 | self._check_signal(sig) |
| 70 | try: |
| 71 | # set_wakeup_fd() raises ValueError if this is not the |
| 72 | # main thread. By calling it early we ensure that an |
| 73 | # event loop running in another thread cannot add a signal |
| 74 | # handler. |
| 75 | signal.set_wakeup_fd(self._csock.fileno()) |
Victor Stinner | c4c4649 | 2014-07-23 18:21:45 +0200 | [diff] [blame] | 76 | except (ValueError, OSError) as exc: |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 77 | raise RuntimeError(str(exc)) |
| 78 | |
Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 79 | handle = events.Handle(callback, args, self) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 80 | self._signal_handlers[sig] = handle |
| 81 | |
| 82 | try: |
Victor Stinner | fe5649c | 2014-07-17 22:43:40 +0200 | [diff] [blame] | 83 | # Register a dummy signal handler to ask Python to write the signal |
| 84 | # number in the wakup file descriptor. _process_self_data() will |
| 85 | # read signal numbers from this file descriptor to handle signals. |
| 86 | signal.signal(sig, _sighandler_noop) |
| 87 | |
Charles-François Natali | 74e7cf3 | 2013-12-05 22:47:19 +0100 | [diff] [blame] | 88 | # Set SA_RESTART to limit EINTR occurrences. |
| 89 | signal.siginterrupt(sig, False) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 90 | except OSError as exc: |
| 91 | del self._signal_handlers[sig] |
| 92 | if not self._signal_handlers: |
| 93 | try: |
| 94 | signal.set_wakeup_fd(-1) |
Victor Stinner | c4c4649 | 2014-07-23 18:21:45 +0200 | [diff] [blame] | 95 | except (ValueError, OSError) as nexc: |
Guido van Rossum | fc29e0f | 2013-10-17 15:39:45 -0700 | [diff] [blame] | 96 | logger.info('set_wakeup_fd(-1) failed: %s', nexc) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 97 | |
| 98 | if exc.errno == errno.EINVAL: |
| 99 | raise RuntimeError('sig {} cannot be caught'.format(sig)) |
| 100 | else: |
| 101 | raise |
| 102 | |
Victor Stinner | fe5649c | 2014-07-17 22:43:40 +0200 | [diff] [blame] | 103 | def _handle_signal(self, sig): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 104 | """Internal helper that is the actual signal handler.""" |
| 105 | handle = self._signal_handlers.get(sig) |
| 106 | if handle is None: |
| 107 | return # Assume it's some race condition. |
| 108 | if handle._cancelled: |
| 109 | self.remove_signal_handler(sig) # Remove it properly. |
| 110 | else: |
| 111 | self._add_callback_signalsafe(handle) |
| 112 | |
| 113 | def remove_signal_handler(self, sig): |
| 114 | """Remove a handler for a signal. UNIX only. |
| 115 | |
| 116 | Return True if a signal handler was removed, False if not. |
| 117 | """ |
| 118 | self._check_signal(sig) |
| 119 | try: |
| 120 | del self._signal_handlers[sig] |
| 121 | except KeyError: |
| 122 | return False |
| 123 | |
| 124 | if sig == signal.SIGINT: |
| 125 | handler = signal.default_int_handler |
| 126 | else: |
| 127 | handler = signal.SIG_DFL |
| 128 | |
| 129 | try: |
| 130 | signal.signal(sig, handler) |
| 131 | except OSError as exc: |
| 132 | if exc.errno == errno.EINVAL: |
| 133 | raise RuntimeError('sig {} cannot be caught'.format(sig)) |
| 134 | else: |
| 135 | raise |
| 136 | |
| 137 | if not self._signal_handlers: |
| 138 | try: |
| 139 | signal.set_wakeup_fd(-1) |
Victor Stinner | c4c4649 | 2014-07-23 18:21:45 +0200 | [diff] [blame] | 140 | except (ValueError, OSError) as exc: |
Guido van Rossum | fc29e0f | 2013-10-17 15:39:45 -0700 | [diff] [blame] | 141 | logger.info('set_wakeup_fd(-1) failed: %s', exc) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 142 | |
| 143 | return True |
| 144 | |
| 145 | def _check_signal(self, sig): |
| 146 | """Internal helper to validate a signal. |
| 147 | |
| 148 | Raise ValueError if the signal number is invalid or uncatchable. |
| 149 | Raise RuntimeError if there is a problem setting up the handler. |
| 150 | """ |
| 151 | if not isinstance(sig, int): |
| 152 | raise TypeError('sig must be an int, not {!r}'.format(sig)) |
| 153 | |
| 154 | if not (1 <= sig < signal.NSIG): |
| 155 | raise ValueError( |
| 156 | 'sig {} out of range(1, {})'.format(sig, signal.NSIG)) |
| 157 | |
| 158 | def _make_read_pipe_transport(self, pipe, protocol, waiter=None, |
| 159 | extra=None): |
| 160 | return _UnixReadPipeTransport(self, pipe, protocol, waiter, extra) |
| 161 | |
| 162 | def _make_write_pipe_transport(self, pipe, protocol, waiter=None, |
| 163 | extra=None): |
| 164 | return _UnixWritePipeTransport(self, pipe, protocol, waiter, extra) |
| 165 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 166 | @coroutine |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 167 | def _make_subprocess_transport(self, protocol, args, shell, |
| 168 | stdin, stdout, stderr, bufsize, |
| 169 | extra=None, **kwargs): |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 170 | with events.get_child_watcher() as watcher: |
| 171 | transp = _UnixSubprocessTransport(self, protocol, args, shell, |
| 172 | stdin, stdout, stderr, bufsize, |
Victor Stinner | 73f10fd | 2014-01-29 14:32:20 -0800 | [diff] [blame] | 173 | extra=extra, **kwargs) |
Guido van Rossum | 4835f17 | 2014-01-10 13:28:59 -0800 | [diff] [blame] | 174 | yield from transp._post_init() |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 175 | watcher.add_child_handler(transp.get_pid(), |
| 176 | self._child_watcher_callback, transp) |
Guido van Rossum | 4835f17 | 2014-01-10 13:28:59 -0800 | [diff] [blame] | 177 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 178 | return transp |
| 179 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 180 | def _child_watcher_callback(self, pid, returncode, transp): |
| 181 | self.call_soon_threadsafe(transp._process_exited, returncode) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 182 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 183 | @coroutine |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 184 | def create_unix_connection(self, protocol_factory, path, *, |
| 185 | ssl=None, sock=None, |
| 186 | server_hostname=None): |
| 187 | assert server_hostname is None or isinstance(server_hostname, str) |
| 188 | if ssl: |
| 189 | if server_hostname is None: |
| 190 | raise ValueError( |
| 191 | 'you have to pass server_hostname when using ssl') |
| 192 | else: |
| 193 | if server_hostname is not None: |
| 194 | raise ValueError('server_hostname is only meaningful with ssl') |
| 195 | |
| 196 | if path is not None: |
| 197 | if sock is not None: |
| 198 | raise ValueError( |
| 199 | 'path and sock can not be specified at the same time') |
| 200 | |
Victor Stinner | 79a2952 | 2014-02-19 01:45:59 +0100 | [diff] [blame] | 201 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0) |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 202 | try: |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 203 | sock.setblocking(False) |
| 204 | yield from self.sock_connect(sock, path) |
Victor Stinner | 79a2952 | 2014-02-19 01:45:59 +0100 | [diff] [blame] | 205 | except: |
| 206 | sock.close() |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 207 | raise |
| 208 | |
| 209 | else: |
| 210 | if sock is None: |
| 211 | raise ValueError('no path and sock were specified') |
| 212 | sock.setblocking(False) |
| 213 | |
| 214 | transport, protocol = yield from self._create_connection_transport( |
| 215 | sock, protocol_factory, ssl, server_hostname) |
| 216 | return transport, protocol |
| 217 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 218 | @coroutine |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 219 | def create_unix_server(self, protocol_factory, path=None, *, |
| 220 | sock=None, backlog=100, ssl=None): |
| 221 | if isinstance(ssl, bool): |
| 222 | raise TypeError('ssl argument must be an SSLContext or None') |
| 223 | |
| 224 | if path is not None: |
Victor Stinner | 1fd03a4 | 2014-04-07 11:18:54 +0200 | [diff] [blame] | 225 | if sock is not None: |
| 226 | raise ValueError( |
| 227 | 'path and sock can not be specified at the same time') |
| 228 | |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 229 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 230 | |
| 231 | try: |
| 232 | sock.bind(path) |
| 233 | except OSError as exc: |
Victor Stinner | 79a2952 | 2014-02-19 01:45:59 +0100 | [diff] [blame] | 234 | sock.close() |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 235 | if exc.errno == errno.EADDRINUSE: |
| 236 | # Let's improve the error message by adding |
| 237 | # with what exact address it occurs. |
| 238 | msg = 'Address {!r} is already in use'.format(path) |
| 239 | raise OSError(errno.EADDRINUSE, msg) from None |
| 240 | else: |
| 241 | raise |
Victor Stinner | 223a624 | 2014-06-04 00:11:52 +0200 | [diff] [blame] | 242 | except: |
| 243 | sock.close() |
| 244 | raise |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 245 | else: |
| 246 | if sock is None: |
| 247 | raise ValueError( |
| 248 | 'path was not specified, and no sock specified') |
| 249 | |
| 250 | if sock.family != socket.AF_UNIX: |
| 251 | raise ValueError( |
| 252 | 'A UNIX Domain Socket was expected, got {!r}'.format(sock)) |
| 253 | |
| 254 | server = base_events.Server(self, [sock]) |
| 255 | sock.listen(backlog) |
| 256 | sock.setblocking(False) |
| 257 | self._start_serving(protocol_factory, sock, ssl, server) |
| 258 | return server |
| 259 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 260 | |
Victor Stinner | f2ed889 | 2014-07-29 23:08:00 +0200 | [diff] [blame] | 261 | if hasattr(os, 'set_blocking'): |
| 262 | def _set_nonblocking(fd): |
| 263 | os.set_blocking(fd, False) |
| 264 | else: |
Yury Selivanov | 8c0e0ab | 2014-09-24 23:21:39 -0400 | [diff] [blame^] | 265 | import fcntl |
| 266 | |
Victor Stinner | f2ed889 | 2014-07-29 23:08:00 +0200 | [diff] [blame] | 267 | def _set_nonblocking(fd): |
| 268 | flags = fcntl.fcntl(fd, fcntl.F_GETFL) |
| 269 | flags = flags | os.O_NONBLOCK |
| 270 | fcntl.fcntl(fd, fcntl.F_SETFL, flags) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 271 | |
| 272 | |
| 273 | class _UnixReadPipeTransport(transports.ReadTransport): |
| 274 | |
Yury Selivanov | dec1a45 | 2014-02-18 22:27:48 -0500 | [diff] [blame] | 275 | max_size = 256 * 1024 # max bytes we read in one event loop iteration |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 276 | |
| 277 | def __init__(self, loop, pipe, protocol, waiter=None, extra=None): |
| 278 | super().__init__(extra) |
| 279 | self._extra['pipe'] = pipe |
| 280 | self._loop = loop |
| 281 | self._pipe = pipe |
| 282 | self._fileno = pipe.fileno() |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 283 | mode = os.fstat(self._fileno).st_mode |
Guido van Rossum | 02757ea | 2014-01-10 13:30:04 -0800 | [diff] [blame] | 284 | if not (stat.S_ISFIFO(mode) or |
| 285 | stat.S_ISSOCK(mode) or |
| 286 | stat.S_ISCHR(mode)): |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 287 | raise ValueError("Pipe transport is for pipes/sockets only.") |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 288 | _set_nonblocking(self._fileno) |
| 289 | self._protocol = protocol |
| 290 | self._closing = False |
| 291 | self._loop.add_reader(self._fileno, self._read_ready) |
| 292 | self._loop.call_soon(self._protocol.connection_made, self) |
| 293 | if waiter is not None: |
Victor Stinner | bfff45d | 2014-07-08 23:57:31 +0200 | [diff] [blame] | 294 | # wait until protocol.connection_made() has been called |
Victor Stinner | a9acbe8 | 2014-07-05 15:29:41 +0200 | [diff] [blame] | 295 | self._loop.call_soon(waiter._set_result_unless_cancelled, None) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 296 | |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 297 | def __repr__(self): |
| 298 | info = [self.__class__.__name__, 'fd=%s' % self._fileno] |
| 299 | if self._pipe is not None: |
| 300 | polling = selector_events._test_selector_event( |
| 301 | self._loop._selector, |
| 302 | self._fileno, selectors.EVENT_READ) |
| 303 | if polling: |
| 304 | info.append('polling') |
| 305 | else: |
| 306 | info.append('idle') |
| 307 | else: |
| 308 | info.append('closed') |
| 309 | return '<%s>' % ' '.join(info) |
| 310 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 311 | def _read_ready(self): |
| 312 | try: |
| 313 | data = os.read(self._fileno, self.max_size) |
| 314 | except (BlockingIOError, InterruptedError): |
| 315 | pass |
| 316 | except OSError as exc: |
Victor Stinner | 0ee29c2 | 2014-02-19 01:40:41 +0100 | [diff] [blame] | 317 | self._fatal_error(exc, 'Fatal read error on pipe transport') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 318 | else: |
| 319 | if data: |
| 320 | self._protocol.data_received(data) |
| 321 | else: |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 322 | if self._loop.get_debug(): |
| 323 | logger.info("%r was closed by peer", self) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 324 | self._closing = True |
| 325 | self._loop.remove_reader(self._fileno) |
| 326 | self._loop.call_soon(self._protocol.eof_received) |
| 327 | self._loop.call_soon(self._call_connection_lost, None) |
| 328 | |
Guido van Rossum | 57497ad | 2013-10-18 07:58:20 -0700 | [diff] [blame] | 329 | def pause_reading(self): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 330 | self._loop.remove_reader(self._fileno) |
| 331 | |
Guido van Rossum | 57497ad | 2013-10-18 07:58:20 -0700 | [diff] [blame] | 332 | def resume_reading(self): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 333 | self._loop.add_reader(self._fileno, self._read_ready) |
| 334 | |
| 335 | def close(self): |
| 336 | if not self._closing: |
| 337 | self._close(None) |
| 338 | |
Victor Stinner | 0ee29c2 | 2014-02-19 01:40:41 +0100 | [diff] [blame] | 339 | def _fatal_error(self, exc, message='Fatal error on pipe transport'): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 340 | # should be called by exception handler only |
Victor Stinner | b261475 | 2014-08-25 23:20:52 +0200 | [diff] [blame] | 341 | if (isinstance(exc, OSError) and exc.errno == errno.EIO): |
| 342 | if self._loop.get_debug(): |
| 343 | logger.debug("%r: %s", self, message, exc_info=True) |
| 344 | else: |
Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 345 | self._loop.call_exception_handler({ |
Victor Stinner | 0ee29c2 | 2014-02-19 01:40:41 +0100 | [diff] [blame] | 346 | 'message': message, |
Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 347 | 'exception': exc, |
| 348 | 'transport': self, |
| 349 | 'protocol': self._protocol, |
| 350 | }) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 351 | self._close(exc) |
| 352 | |
| 353 | def _close(self, exc): |
| 354 | self._closing = True |
| 355 | self._loop.remove_reader(self._fileno) |
| 356 | self._loop.call_soon(self._call_connection_lost, exc) |
| 357 | |
| 358 | def _call_connection_lost(self, exc): |
| 359 | try: |
| 360 | self._protocol.connection_lost(exc) |
| 361 | finally: |
| 362 | self._pipe.close() |
| 363 | self._pipe = None |
| 364 | self._protocol = None |
| 365 | self._loop = None |
| 366 | |
| 367 | |
Yury Selivanov | 3cb9914 | 2014-02-18 18:41:13 -0500 | [diff] [blame] | 368 | class _UnixWritePipeTransport(transports._FlowControlMixin, |
Guido van Rossum | 47fb97e | 2014-01-29 13:20:39 -0800 | [diff] [blame] | 369 | transports.WriteTransport): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 370 | |
| 371 | def __init__(self, loop, pipe, protocol, waiter=None, extra=None): |
| 372 | super().__init__(extra) |
| 373 | self._extra['pipe'] = pipe |
| 374 | self._loop = loop |
| 375 | self._pipe = pipe |
| 376 | self._fileno = pipe.fileno() |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 377 | mode = os.fstat(self._fileno).st_mode |
| 378 | is_socket = stat.S_ISSOCK(mode) |
Victor Stinner | 8dffc45 | 2014-01-25 15:32:06 +0100 | [diff] [blame] | 379 | if not (is_socket or |
| 380 | stat.S_ISFIFO(mode) or |
| 381 | stat.S_ISCHR(mode)): |
| 382 | raise ValueError("Pipe transport is only for " |
| 383 | "pipes, sockets and character devices") |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 384 | _set_nonblocking(self._fileno) |
| 385 | self._protocol = protocol |
| 386 | self._buffer = [] |
| 387 | self._conn_lost = 0 |
| 388 | self._closing = False # Set when close() or write_eof() called. |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 389 | |
| 390 | # On AIX, the reader trick only works for sockets. |
| 391 | # On other platforms it works for pipes and sockets. |
| 392 | # (Exception: OS X 10.4? Issue #19294.) |
| 393 | if is_socket or not sys.platform.startswith("aix"): |
| 394 | self._loop.add_reader(self._fileno, self._read_ready) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 395 | |
| 396 | self._loop.call_soon(self._protocol.connection_made, self) |
| 397 | if waiter is not None: |
Victor Stinner | bfff45d | 2014-07-08 23:57:31 +0200 | [diff] [blame] | 398 | # wait until protocol.connection_made() has been called |
Victor Stinner | a9acbe8 | 2014-07-05 15:29:41 +0200 | [diff] [blame] | 399 | self._loop.call_soon(waiter._set_result_unless_cancelled, None) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 400 | |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 401 | def __repr__(self): |
| 402 | info = [self.__class__.__name__, 'fd=%s' % self._fileno] |
| 403 | if self._pipe is not None: |
| 404 | polling = selector_events._test_selector_event( |
| 405 | self._loop._selector, |
| 406 | self._fileno, selectors.EVENT_WRITE) |
| 407 | if polling: |
| 408 | info.append('polling') |
| 409 | else: |
| 410 | info.append('idle') |
| 411 | |
| 412 | bufsize = self.get_write_buffer_size() |
| 413 | info.append('bufsize=%s' % bufsize) |
| 414 | else: |
| 415 | info.append('closed') |
| 416 | return '<%s>' % ' '.join(info) |
| 417 | |
Guido van Rossum | 47fb97e | 2014-01-29 13:20:39 -0800 | [diff] [blame] | 418 | def get_write_buffer_size(self): |
| 419 | return sum(len(data) for data in self._buffer) |
| 420 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 421 | def _read_ready(self): |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 422 | # Pipe was closed by peer. |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 423 | if self._loop.get_debug(): |
| 424 | logger.info("%r was closed by peer", self) |
Victor Stinner | 61b3c9b | 2014-01-31 13:04:28 +0100 | [diff] [blame] | 425 | if self._buffer: |
| 426 | self._close(BrokenPipeError()) |
| 427 | else: |
| 428 | self._close() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 429 | |
| 430 | def write(self, data): |
Guido van Rossum | 47fb97e | 2014-01-29 13:20:39 -0800 | [diff] [blame] | 431 | assert isinstance(data, (bytes, bytearray, memoryview)), repr(data) |
| 432 | if isinstance(data, bytearray): |
| 433 | data = memoryview(data) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 434 | if not data: |
| 435 | return |
| 436 | |
| 437 | if self._conn_lost or self._closing: |
| 438 | if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES: |
Guido van Rossum | fc29e0f | 2013-10-17 15:39:45 -0700 | [diff] [blame] | 439 | logger.warning('pipe closed by peer or ' |
| 440 | 'os.write(pipe, data) raised exception.') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 441 | self._conn_lost += 1 |
| 442 | return |
| 443 | |
| 444 | if not self._buffer: |
| 445 | # Attempt to send it right away first. |
| 446 | try: |
| 447 | n = os.write(self._fileno, data) |
| 448 | except (BlockingIOError, InterruptedError): |
| 449 | n = 0 |
| 450 | except Exception as exc: |
| 451 | self._conn_lost += 1 |
Victor Stinner | 0ee29c2 | 2014-02-19 01:40:41 +0100 | [diff] [blame] | 452 | self._fatal_error(exc, 'Fatal write error on pipe transport') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 453 | return |
| 454 | if n == len(data): |
| 455 | return |
| 456 | elif n > 0: |
| 457 | data = data[n:] |
| 458 | self._loop.add_writer(self._fileno, self._write_ready) |
| 459 | |
| 460 | self._buffer.append(data) |
Guido van Rossum | 47fb97e | 2014-01-29 13:20:39 -0800 | [diff] [blame] | 461 | self._maybe_pause_protocol() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 462 | |
| 463 | def _write_ready(self): |
| 464 | data = b''.join(self._buffer) |
| 465 | assert data, 'Data should not be empty' |
| 466 | |
| 467 | self._buffer.clear() |
| 468 | try: |
| 469 | n = os.write(self._fileno, data) |
| 470 | except (BlockingIOError, InterruptedError): |
| 471 | self._buffer.append(data) |
| 472 | except Exception as exc: |
| 473 | self._conn_lost += 1 |
| 474 | # Remove writer here, _fatal_error() doesn't it |
| 475 | # because _buffer is empty. |
| 476 | self._loop.remove_writer(self._fileno) |
Victor Stinner | 0ee29c2 | 2014-02-19 01:40:41 +0100 | [diff] [blame] | 477 | self._fatal_error(exc, 'Fatal write error on pipe transport') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 478 | else: |
| 479 | if n == len(data): |
| 480 | self._loop.remove_writer(self._fileno) |
Guido van Rossum | 47fb97e | 2014-01-29 13:20:39 -0800 | [diff] [blame] | 481 | self._maybe_resume_protocol() # May append to buffer. |
| 482 | if not self._buffer and self._closing: |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 483 | self._loop.remove_reader(self._fileno) |
| 484 | self._call_connection_lost(None) |
| 485 | return |
| 486 | elif n > 0: |
| 487 | data = data[n:] |
| 488 | |
| 489 | self._buffer.append(data) # Try again later. |
| 490 | |
| 491 | def can_write_eof(self): |
| 492 | return True |
| 493 | |
| 494 | # TODO: Make the relationships between write_eof(), close(), |
| 495 | # abort(), _fatal_error() and _close() more straightforward. |
| 496 | |
| 497 | def write_eof(self): |
| 498 | if self._closing: |
| 499 | return |
| 500 | assert self._pipe |
| 501 | self._closing = True |
| 502 | if not self._buffer: |
| 503 | self._loop.remove_reader(self._fileno) |
| 504 | self._loop.call_soon(self._call_connection_lost, None) |
| 505 | |
| 506 | def close(self): |
| 507 | if not self._closing: |
| 508 | # write_eof is all what we needed to close the write pipe |
| 509 | self.write_eof() |
| 510 | |
| 511 | def abort(self): |
| 512 | self._close(None) |
| 513 | |
Victor Stinner | 0ee29c2 | 2014-02-19 01:40:41 +0100 | [diff] [blame] | 514 | def _fatal_error(self, exc, message='Fatal error on pipe transport'): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 515 | # should be called by exception handler only |
Victor Stinner | b261475 | 2014-08-25 23:20:52 +0200 | [diff] [blame] | 516 | if isinstance(exc, (BrokenPipeError, ConnectionResetError)): |
| 517 | if self._loop.get_debug(): |
| 518 | logger.debug("%r: %s", self, message, exc_info=True) |
| 519 | else: |
Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 520 | self._loop.call_exception_handler({ |
Victor Stinner | 0ee29c2 | 2014-02-19 01:40:41 +0100 | [diff] [blame] | 521 | 'message': message, |
Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 522 | 'exception': exc, |
| 523 | 'transport': self, |
| 524 | 'protocol': self._protocol, |
| 525 | }) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 526 | self._close(exc) |
| 527 | |
| 528 | def _close(self, exc=None): |
| 529 | self._closing = True |
| 530 | if self._buffer: |
| 531 | self._loop.remove_writer(self._fileno) |
| 532 | self._buffer.clear() |
| 533 | self._loop.remove_reader(self._fileno) |
| 534 | self._loop.call_soon(self._call_connection_lost, exc) |
| 535 | |
| 536 | def _call_connection_lost(self, exc): |
| 537 | try: |
| 538 | self._protocol.connection_lost(exc) |
| 539 | finally: |
| 540 | self._pipe.close() |
| 541 | self._pipe = None |
| 542 | self._protocol = None |
| 543 | self._loop = None |
| 544 | |
| 545 | |
Guido van Rossum | 5969128 | 2013-10-30 14:52:03 -0700 | [diff] [blame] | 546 | class _UnixSubprocessTransport(base_subprocess.BaseSubprocessTransport): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 547 | |
Guido van Rossum | 5969128 | 2013-10-30 14:52:03 -0700 | [diff] [blame] | 548 | def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs): |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 549 | stdin_w = None |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 550 | if stdin == subprocess.PIPE: |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 551 | # Use a socket pair for stdin, since not all platforms |
| 552 | # support selecting read events on the write end of a |
| 553 | # socket (which we use in order to detect closing of the |
| 554 | # other end). Notably this is needed on AIX, and works |
| 555 | # just fine on other platforms. |
| 556 | stdin, stdin_w = self._loop._socketpair() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 557 | self._proc = subprocess.Popen( |
| 558 | args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr, |
| 559 | universal_newlines=False, bufsize=bufsize, **kwargs) |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 560 | if stdin_w is not None: |
| 561 | stdin.close() |
Victor Stinner | 2dba23a | 2014-07-03 00:59:00 +0200 | [diff] [blame] | 562 | self._proc.stdin = open(stdin_w.detach(), 'wb', buffering=bufsize) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 563 | |
| 564 | |
| 565 | class AbstractChildWatcher: |
| 566 | """Abstract base class for monitoring child processes. |
| 567 | |
| 568 | Objects derived from this class monitor a collection of subprocesses and |
| 569 | report their termination or interruption by a signal. |
| 570 | |
| 571 | New callbacks are registered with .add_child_handler(). Starting a new |
| 572 | process must be done within a 'with' block to allow the watcher to suspend |
| 573 | its activity until the new process if fully registered (this is needed to |
| 574 | prevent a race condition in some implementations). |
| 575 | |
| 576 | Example: |
| 577 | with watcher: |
| 578 | proc = subprocess.Popen("sleep 1") |
| 579 | watcher.add_child_handler(proc.pid, callback) |
| 580 | |
| 581 | Notes: |
| 582 | Implementations of this class must be thread-safe. |
| 583 | |
| 584 | Since child watcher objects may catch the SIGCHLD signal and call |
| 585 | waitpid(-1), there should be only one active object per process. |
| 586 | """ |
| 587 | |
| 588 | def add_child_handler(self, pid, callback, *args): |
| 589 | """Register a new child handler. |
| 590 | |
| 591 | Arrange for callback(pid, returncode, *args) to be called when |
| 592 | process 'pid' terminates. Specifying another callback for the same |
| 593 | process replaces the previous handler. |
| 594 | |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 595 | Note: callback() must be thread-safe. |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 596 | """ |
| 597 | raise NotImplementedError() |
| 598 | |
| 599 | def remove_child_handler(self, pid): |
| 600 | """Removes the handler for process 'pid'. |
| 601 | |
| 602 | The function returns True if the handler was successfully removed, |
| 603 | False if there was nothing to remove.""" |
| 604 | |
| 605 | raise NotImplementedError() |
| 606 | |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 607 | def attach_loop(self, loop): |
| 608 | """Attach the watcher to an event loop. |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 609 | |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 610 | If the watcher was previously attached to an event loop, then it is |
| 611 | first detached before attaching to the new loop. |
| 612 | |
| 613 | Note: loop may be None. |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 614 | """ |
| 615 | raise NotImplementedError() |
| 616 | |
| 617 | def close(self): |
| 618 | """Close the watcher. |
| 619 | |
| 620 | This must be called to make sure that any underlying resource is freed. |
| 621 | """ |
| 622 | raise NotImplementedError() |
| 623 | |
| 624 | def __enter__(self): |
| 625 | """Enter the watcher's context and allow starting new processes |
| 626 | |
| 627 | This function must return self""" |
| 628 | raise NotImplementedError() |
| 629 | |
| 630 | def __exit__(self, a, b, c): |
| 631 | """Exit the watcher's context""" |
| 632 | raise NotImplementedError() |
| 633 | |
| 634 | |
| 635 | class BaseChildWatcher(AbstractChildWatcher): |
| 636 | |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 637 | def __init__(self): |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 638 | self._loop = None |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 639 | |
| 640 | def close(self): |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 641 | self.attach_loop(None) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 642 | |
| 643 | def _do_waitpid(self, expected_pid): |
| 644 | raise NotImplementedError() |
| 645 | |
| 646 | def _do_waitpid_all(self): |
| 647 | raise NotImplementedError() |
| 648 | |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 649 | def attach_loop(self, loop): |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 650 | assert loop is None or isinstance(loop, events.AbstractEventLoop) |
| 651 | |
| 652 | if self._loop is not None: |
| 653 | self._loop.remove_signal_handler(signal.SIGCHLD) |
| 654 | |
| 655 | self._loop = loop |
| 656 | if loop is not None: |
| 657 | loop.add_signal_handler(signal.SIGCHLD, self._sig_chld) |
| 658 | |
| 659 | # Prevent a race condition in case a child terminated |
| 660 | # during the switch. |
| 661 | self._do_waitpid_all() |
| 662 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 663 | def _sig_chld(self): |
| 664 | try: |
| 665 | self._do_waitpid_all() |
Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 666 | except Exception as exc: |
| 667 | # self._loop should always be available here |
| 668 | # as '_sig_chld' is added as a signal handler |
| 669 | # in 'attach_loop' |
| 670 | self._loop.call_exception_handler({ |
| 671 | 'message': 'Unknown exception in SIGCHLD handler', |
| 672 | 'exception': exc, |
| 673 | }) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 674 | |
| 675 | def _compute_returncode(self, status): |
| 676 | if os.WIFSIGNALED(status): |
| 677 | # The child process died because of a signal. |
| 678 | return -os.WTERMSIG(status) |
| 679 | elif os.WIFEXITED(status): |
| 680 | # The child process exited (e.g sys.exit()). |
| 681 | return os.WEXITSTATUS(status) |
| 682 | else: |
| 683 | # The child exited, but we don't understand its status. |
| 684 | # This shouldn't happen, but if it does, let's just |
| 685 | # return that status; perhaps that helps debug it. |
| 686 | return status |
| 687 | |
| 688 | |
| 689 | class SafeChildWatcher(BaseChildWatcher): |
| 690 | """'Safe' child watcher implementation. |
| 691 | |
| 692 | This implementation avoids disrupting other code spawning processes by |
| 693 | polling explicitly each process in the SIGCHLD handler instead of calling |
| 694 | os.waitpid(-1). |
| 695 | |
| 696 | This is a safe solution but it has a significant overhead when handling a |
| 697 | big number of children (O(n) each time SIGCHLD is raised) |
| 698 | """ |
| 699 | |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 700 | def __init__(self): |
| 701 | super().__init__() |
| 702 | self._callbacks = {} |
| 703 | |
| 704 | def close(self): |
| 705 | self._callbacks.clear() |
| 706 | super().close() |
| 707 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 708 | def __enter__(self): |
| 709 | return self |
| 710 | |
| 711 | def __exit__(self, a, b, c): |
| 712 | pass |
| 713 | |
| 714 | def add_child_handler(self, pid, callback, *args): |
| 715 | self._callbacks[pid] = callback, args |
| 716 | |
| 717 | # Prevent a race condition in case the child is already terminated. |
| 718 | self._do_waitpid(pid) |
| 719 | |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 720 | def remove_child_handler(self, pid): |
| 721 | try: |
| 722 | del self._callbacks[pid] |
| 723 | return True |
| 724 | except KeyError: |
| 725 | return False |
| 726 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 727 | def _do_waitpid_all(self): |
| 728 | |
| 729 | for pid in list(self._callbacks): |
| 730 | self._do_waitpid(pid) |
| 731 | |
| 732 | def _do_waitpid(self, expected_pid): |
| 733 | assert expected_pid > 0 |
| 734 | |
| 735 | try: |
| 736 | pid, status = os.waitpid(expected_pid, os.WNOHANG) |
| 737 | except ChildProcessError: |
| 738 | # The child process is already reaped |
| 739 | # (may happen if waitpid() is called elsewhere). |
| 740 | pid = expected_pid |
| 741 | returncode = 255 |
| 742 | logger.warning( |
| 743 | "Unknown child process pid %d, will report returncode 255", |
| 744 | pid) |
| 745 | else: |
| 746 | if pid == 0: |
| 747 | # The child process is still alive. |
| 748 | return |
| 749 | |
| 750 | returncode = self._compute_returncode(status) |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 751 | if self._loop.get_debug(): |
| 752 | logger.debug('process %s exited with returncode %s', |
| 753 | expected_pid, returncode) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 754 | |
| 755 | try: |
| 756 | callback, args = self._callbacks.pop(pid) |
| 757 | except KeyError: # pragma: no cover |
| 758 | # May happen if .remove_child_handler() is called |
| 759 | # after os.waitpid() returns. |
Victor Stinner | b261475 | 2014-08-25 23:20:52 +0200 | [diff] [blame] | 760 | if self._loop.get_debug(): |
| 761 | logger.warning("Child watcher got an unexpected pid: %r", |
| 762 | pid, exc_info=True) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 763 | else: |
| 764 | callback(pid, returncode, *args) |
| 765 | |
| 766 | |
| 767 | class FastChildWatcher(BaseChildWatcher): |
| 768 | """'Fast' child watcher implementation. |
| 769 | |
| 770 | This implementation reaps every terminated processes by calling |
| 771 | os.waitpid(-1) directly, possibly breaking other code spawning processes |
| 772 | and waiting for their termination. |
| 773 | |
| 774 | There is no noticeable overhead when handling a big number of children |
| 775 | (O(1) each time a child terminates). |
| 776 | """ |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 777 | def __init__(self): |
| 778 | super().__init__() |
| 779 | self._callbacks = {} |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 780 | self._lock = threading.Lock() |
| 781 | self._zombies = {} |
| 782 | self._forks = 0 |
| 783 | |
| 784 | def close(self): |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 785 | self._callbacks.clear() |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 786 | self._zombies.clear() |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 787 | super().close() |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 788 | |
| 789 | def __enter__(self): |
| 790 | with self._lock: |
| 791 | self._forks += 1 |
| 792 | |
| 793 | return self |
| 794 | |
| 795 | def __exit__(self, a, b, c): |
| 796 | with self._lock: |
| 797 | self._forks -= 1 |
| 798 | |
| 799 | if self._forks or not self._zombies: |
| 800 | return |
| 801 | |
| 802 | collateral_victims = str(self._zombies) |
| 803 | self._zombies.clear() |
| 804 | |
| 805 | logger.warning( |
| 806 | "Caught subprocesses termination from unknown pids: %s", |
| 807 | collateral_victims) |
| 808 | |
| 809 | def add_child_handler(self, pid, callback, *args): |
| 810 | assert self._forks, "Must use the context manager" |
Guido van Rossum | ab27a9f | 2014-01-25 16:32:17 -0800 | [diff] [blame] | 811 | with self._lock: |
| 812 | try: |
| 813 | returncode = self._zombies.pop(pid) |
| 814 | except KeyError: |
| 815 | # The child is running. |
| 816 | self._callbacks[pid] = callback, args |
| 817 | return |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 818 | |
Guido van Rossum | ab27a9f | 2014-01-25 16:32:17 -0800 | [diff] [blame] | 819 | # The child is dead already. We can fire the callback. |
| 820 | callback(pid, returncode, *args) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 821 | |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 822 | def remove_child_handler(self, pid): |
| 823 | try: |
| 824 | del self._callbacks[pid] |
| 825 | return True |
| 826 | except KeyError: |
| 827 | return False |
| 828 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 829 | def _do_waitpid_all(self): |
| 830 | # Because of signal coalescing, we must keep calling waitpid() as |
| 831 | # long as we're able to reap a child. |
| 832 | while True: |
| 833 | try: |
| 834 | pid, status = os.waitpid(-1, os.WNOHANG) |
| 835 | except ChildProcessError: |
| 836 | # No more child processes exist. |
| 837 | return |
| 838 | else: |
| 839 | if pid == 0: |
| 840 | # A child process is still alive. |
| 841 | return |
| 842 | |
| 843 | returncode = self._compute_returncode(status) |
| 844 | |
Guido van Rossum | ab27a9f | 2014-01-25 16:32:17 -0800 | [diff] [blame] | 845 | with self._lock: |
| 846 | try: |
| 847 | callback, args = self._callbacks.pop(pid) |
| 848 | except KeyError: |
| 849 | # unknown child |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 850 | if self._forks: |
| 851 | # It may not be registered yet. |
| 852 | self._zombies[pid] = returncode |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 853 | if self._loop.get_debug(): |
| 854 | logger.debug('unknown process %s exited ' |
| 855 | 'with returncode %s', |
| 856 | pid, returncode) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 857 | continue |
Guido van Rossum | ab27a9f | 2014-01-25 16:32:17 -0800 | [diff] [blame] | 858 | callback = None |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 859 | else: |
| 860 | if self._loop.get_debug(): |
| 861 | logger.debug('process %s exited with returncode %s', |
| 862 | pid, returncode) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 863 | |
Guido van Rossum | ab27a9f | 2014-01-25 16:32:17 -0800 | [diff] [blame] | 864 | if callback is None: |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 865 | logger.warning( |
| 866 | "Caught subprocess termination from unknown pid: " |
| 867 | "%d -> %d", pid, returncode) |
| 868 | else: |
| 869 | callback(pid, returncode, *args) |
| 870 | |
| 871 | |
| 872 | class _UnixDefaultEventLoopPolicy(events.BaseDefaultEventLoopPolicy): |
| 873 | """XXX""" |
| 874 | _loop_factory = _UnixSelectorEventLoop |
| 875 | |
| 876 | def __init__(self): |
| 877 | super().__init__() |
| 878 | self._watcher = None |
| 879 | |
| 880 | def _init_watcher(self): |
| 881 | with events._lock: |
| 882 | if self._watcher is None: # pragma: no branch |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 883 | self._watcher = SafeChildWatcher() |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 884 | if isinstance(threading.current_thread(), |
| 885 | threading._MainThread): |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 886 | self._watcher.attach_loop(self._local._loop) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 887 | |
| 888 | def set_event_loop(self, loop): |
| 889 | """Set the event loop. |
| 890 | |
| 891 | As a side effect, if a child watcher was set before, then calling |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 892 | .set_event_loop() from the main thread will call .attach_loop(loop) on |
| 893 | the child watcher. |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 894 | """ |
| 895 | |
| 896 | super().set_event_loop(loop) |
| 897 | |
| 898 | if self._watcher is not None and \ |
| 899 | isinstance(threading.current_thread(), threading._MainThread): |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 900 | self._watcher.attach_loop(loop) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 901 | |
| 902 | def get_child_watcher(self): |
Victor Stinner | f9e49dd | 2014-06-05 12:06:44 +0200 | [diff] [blame] | 903 | """Get the watcher for child processes. |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 904 | |
| 905 | If not yet set, a SafeChildWatcher object is automatically created. |
| 906 | """ |
| 907 | if self._watcher is None: |
| 908 | self._init_watcher() |
| 909 | |
| 910 | return self._watcher |
| 911 | |
| 912 | def set_child_watcher(self, watcher): |
Victor Stinner | f9e49dd | 2014-06-05 12:06:44 +0200 | [diff] [blame] | 913 | """Set the watcher for child processes.""" |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 914 | |
| 915 | assert watcher is None or isinstance(watcher, AbstractChildWatcher) |
| 916 | |
| 917 | if self._watcher is not None: |
| 918 | self._watcher.close() |
| 919 | |
| 920 | self._watcher = watcher |
| 921 | |
| 922 | SelectorEventLoop = _UnixSelectorEventLoop |
| 923 | DefaultEventLoopPolicy = _UnixDefaultEventLoopPolicy |