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 |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 4 | import io |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 5 | import itertools |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 6 | import os |
Victor Stinner | 4271dfd | 2017-11-28 15:19:56 +0100 | [diff] [blame] | 7 | import selectors |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 8 | import signal |
| 9 | import socket |
| 10 | import stat |
| 11 | import subprocess |
| 12 | import sys |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 13 | import threading |
Victor Stinner | 978a9af | 2015-01-29 17:50:58 +0100 | [diff] [blame] | 14 | import warnings |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 15 | |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 16 | from . import base_events |
Guido van Rossum | 5969128 | 2013-10-30 14:52:03 -0700 | [diff] [blame] | 17 | from . import base_subprocess |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 18 | from . import constants |
Guido van Rossum | e36fcde | 2014-11-14 11:45:47 -0800 | [diff] [blame] | 19 | from . import coroutines |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 20 | from . import events |
Andrew Svetlov | 0baa72f | 2018-09-11 10:13:04 -0700 | [diff] [blame] | 21 | from . import exceptions |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 22 | from . import futures |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 23 | from . import selector_events |
Yury Selivanov | dbf1022 | 2018-05-28 14:31:28 -0400 | [diff] [blame] | 24 | from . import tasks |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 25 | from . import transports |
Guido van Rossum | fc29e0f | 2013-10-17 15:39:45 -0700 | [diff] [blame] | 26 | from .log import logger |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 27 | |
| 28 | |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 29 | __all__ = ( |
| 30 | 'SelectorEventLoop', |
| 31 | 'AbstractChildWatcher', 'SafeChildWatcher', |
Kyle Stanley | 3f8cebd | 2019-11-14 21:47:56 -0500 | [diff] [blame] | 32 | 'FastChildWatcher', 'PidfdChildWatcher', |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 33 | 'MultiLoopChildWatcher', 'ThreadedChildWatcher', |
| 34 | 'DefaultEventLoopPolicy', |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 35 | ) |
| 36 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 37 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 38 | if sys.platform == 'win32': # pragma: no cover |
| 39 | raise ImportError('Signals are not really supported on Windows') |
| 40 | |
| 41 | |
Victor Stinner | fe5649c | 2014-07-17 22:43:40 +0200 | [diff] [blame] | 42 | def _sighandler_noop(signum, frame): |
| 43 | """Dummy signal handler.""" |
| 44 | pass |
| 45 | |
| 46 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 47 | class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop): |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 48 | """Unix event loop. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 49 | |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 50 | Adds signal handling and UNIX Domain Socket support to SelectorEventLoop. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 51 | """ |
| 52 | |
| 53 | def __init__(self, selector=None): |
| 54 | super().__init__(selector) |
| 55 | self._signal_handlers = {} |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 56 | |
Guido van Rossum | 0b69fbc | 2013-11-06 20:25:50 -0800 | [diff] [blame] | 57 | def close(self): |
Victor Stinner | f328c7d | 2014-06-23 01:02:37 +0200 | [diff] [blame] | 58 | super().close() |
Andrew Svetlov | 4a02543 | 2017-12-21 17:06:46 +0200 | [diff] [blame] | 59 | if not sys.is_finalizing(): |
| 60 | for sig in list(self._signal_handlers): |
| 61 | self.remove_signal_handler(sig) |
| 62 | else: |
Andrew Svetlov | 4f146f9 | 2017-12-24 13:50:03 +0200 | [diff] [blame] | 63 | if self._signal_handlers: |
Andrew Svetlov | a8f4e15 | 2017-12-26 11:53:38 +0200 | [diff] [blame] | 64 | warnings.warn(f"Closing the loop {self!r} " |
Andrew Svetlov | 4f146f9 | 2017-12-24 13:50:03 +0200 | [diff] [blame] | 65 | f"on interpreter shutdown " |
Andrew Svetlov | a8f4e15 | 2017-12-26 11:53:38 +0200 | [diff] [blame] | 66 | f"stage, skipping signal handlers removal", |
Andrew Svetlov | 4f146f9 | 2017-12-24 13:50:03 +0200 | [diff] [blame] | 67 | ResourceWarning, |
| 68 | source=self) |
| 69 | self._signal_handlers.clear() |
Guido van Rossum | 0b69fbc | 2013-11-06 20:25:50 -0800 | [diff] [blame] | 70 | |
Victor Stinner | fe5649c | 2014-07-17 22:43:40 +0200 | [diff] [blame] | 71 | def _process_self_data(self, data): |
| 72 | for signum in data: |
| 73 | if not signum: |
| 74 | # ignore null bytes written by _write_to_self() |
| 75 | continue |
| 76 | self._handle_signal(signum) |
| 77 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 78 | def add_signal_handler(self, sig, callback, *args): |
| 79 | """Add a handler for a signal. UNIX only. |
| 80 | |
| 81 | Raise ValueError if the signal number is invalid or uncatchable. |
| 82 | Raise RuntimeError if there is a problem setting up the handler. |
| 83 | """ |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 84 | if (coroutines.iscoroutine(callback) or |
| 85 | coroutines.iscoroutinefunction(callback)): |
Victor Stinner | 15cc678 | 2015-01-09 00:09:10 +0100 | [diff] [blame] | 86 | raise TypeError("coroutines cannot be used " |
| 87 | "with add_signal_handler()") |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 88 | self._check_signal(sig) |
Victor Stinner | e80bf0d | 2014-12-04 23:07:47 +0100 | [diff] [blame] | 89 | self._check_closed() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 90 | try: |
| 91 | # set_wakeup_fd() raises ValueError if this is not the |
| 92 | # main thread. By calling it early we ensure that an |
| 93 | # event loop running in another thread cannot add a signal |
| 94 | # handler. |
| 95 | signal.set_wakeup_fd(self._csock.fileno()) |
Victor Stinner | c4c4649 | 2014-07-23 18:21:45 +0200 | [diff] [blame] | 96 | except (ValueError, OSError) as exc: |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 97 | raise RuntimeError(str(exc)) |
| 98 | |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 99 | handle = events.Handle(callback, args, self, None) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 100 | self._signal_handlers[sig] = handle |
| 101 | |
| 102 | try: |
Victor Stinner | fe5649c | 2014-07-17 22:43:40 +0200 | [diff] [blame] | 103 | # Register a dummy signal handler to ask Python to write the signal |
Kunal Bhalla | f2947e3 | 2020-05-20 13:12:37 -0400 | [diff] [blame] | 104 | # number in the wakeup file descriptor. _process_self_data() will |
Victor Stinner | fe5649c | 2014-07-17 22:43:40 +0200 | [diff] [blame] | 105 | # read signal numbers from this file descriptor to handle signals. |
| 106 | signal.signal(sig, _sighandler_noop) |
| 107 | |
Charles-François Natali | 74e7cf3 | 2013-12-05 22:47:19 +0100 | [diff] [blame] | 108 | # Set SA_RESTART to limit EINTR occurrences. |
| 109 | signal.siginterrupt(sig, False) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 110 | except OSError as exc: |
| 111 | del self._signal_handlers[sig] |
| 112 | if not self._signal_handlers: |
| 113 | try: |
| 114 | signal.set_wakeup_fd(-1) |
Victor Stinner | c4c4649 | 2014-07-23 18:21:45 +0200 | [diff] [blame] | 115 | except (ValueError, OSError) as nexc: |
Guido van Rossum | fc29e0f | 2013-10-17 15:39:45 -0700 | [diff] [blame] | 116 | logger.info('set_wakeup_fd(-1) failed: %s', nexc) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 117 | |
| 118 | if exc.errno == errno.EINVAL: |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 119 | raise RuntimeError(f'sig {sig} cannot be caught') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 120 | else: |
| 121 | raise |
| 122 | |
Victor Stinner | fe5649c | 2014-07-17 22:43:40 +0200 | [diff] [blame] | 123 | def _handle_signal(self, sig): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 124 | """Internal helper that is the actual signal handler.""" |
| 125 | handle = self._signal_handlers.get(sig) |
| 126 | if handle is None: |
| 127 | return # Assume it's some race condition. |
| 128 | if handle._cancelled: |
| 129 | self.remove_signal_handler(sig) # Remove it properly. |
| 130 | else: |
| 131 | self._add_callback_signalsafe(handle) |
| 132 | |
| 133 | def remove_signal_handler(self, sig): |
| 134 | """Remove a handler for a signal. UNIX only. |
| 135 | |
| 136 | Return True if a signal handler was removed, False if not. |
| 137 | """ |
| 138 | self._check_signal(sig) |
| 139 | try: |
| 140 | del self._signal_handlers[sig] |
| 141 | except KeyError: |
| 142 | return False |
| 143 | |
| 144 | if sig == signal.SIGINT: |
| 145 | handler = signal.default_int_handler |
| 146 | else: |
| 147 | handler = signal.SIG_DFL |
| 148 | |
| 149 | try: |
| 150 | signal.signal(sig, handler) |
| 151 | except OSError as exc: |
| 152 | if exc.errno == errno.EINVAL: |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 153 | raise RuntimeError(f'sig {sig} cannot be caught') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 154 | else: |
| 155 | raise |
| 156 | |
| 157 | if not self._signal_handlers: |
| 158 | try: |
| 159 | signal.set_wakeup_fd(-1) |
Victor Stinner | c4c4649 | 2014-07-23 18:21:45 +0200 | [diff] [blame] | 160 | except (ValueError, OSError) as exc: |
Guido van Rossum | fc29e0f | 2013-10-17 15:39:45 -0700 | [diff] [blame] | 161 | logger.info('set_wakeup_fd(-1) failed: %s', exc) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 162 | |
| 163 | return True |
| 164 | |
| 165 | def _check_signal(self, sig): |
| 166 | """Internal helper to validate a signal. |
| 167 | |
| 168 | Raise ValueError if the signal number is invalid or uncatchable. |
| 169 | Raise RuntimeError if there is a problem setting up the handler. |
| 170 | """ |
| 171 | if not isinstance(sig, int): |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 172 | raise TypeError(f'sig must be an int, not {sig!r}') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 173 | |
Antoine Pitrou | 9d3627e | 2018-05-04 13:00:50 +0200 | [diff] [blame] | 174 | if sig not in signal.valid_signals(): |
| 175 | raise ValueError(f'invalid signal number {sig}') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 176 | |
| 177 | def _make_read_pipe_transport(self, pipe, protocol, waiter=None, |
| 178 | extra=None): |
| 179 | return _UnixReadPipeTransport(self, pipe, protocol, waiter, extra) |
| 180 | |
| 181 | def _make_write_pipe_transport(self, pipe, protocol, waiter=None, |
| 182 | extra=None): |
| 183 | return _UnixWritePipeTransport(self, pipe, protocol, waiter, extra) |
| 184 | |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 185 | async def _make_subprocess_transport(self, protocol, args, shell, |
| 186 | stdin, stdout, stderr, bufsize, |
| 187 | extra=None, **kwargs): |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 188 | with events.get_child_watcher() as watcher: |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 189 | if not watcher.is_active(): |
| 190 | # Check early. |
| 191 | # Raising exception before process creation |
| 192 | # prevents subprocess execution if the watcher |
| 193 | # is not ready to handle it. |
| 194 | raise RuntimeError("asyncio.get_child_watcher() is not activated, " |
| 195 | "subprocess support is not installed.") |
Yury Selivanov | 7661db6 | 2016-05-16 15:38:39 -0400 | [diff] [blame] | 196 | waiter = self.create_future() |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 197 | transp = _UnixSubprocessTransport(self, protocol, args, shell, |
| 198 | stdin, stdout, stderr, bufsize, |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 199 | waiter=waiter, extra=extra, |
| 200 | **kwargs) |
| 201 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 202 | watcher.add_child_handler(transp.get_pid(), |
| 203 | self._child_watcher_callback, transp) |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 204 | try: |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 205 | await waiter |
Yury Selivanov | 431b540 | 2019-05-27 14:45:12 +0200 | [diff] [blame] | 206 | except (SystemExit, KeyboardInterrupt): |
| 207 | raise |
| 208 | except BaseException: |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 209 | transp.close() |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 210 | await transp._wait() |
| 211 | raise |
Guido van Rossum | 4835f17 | 2014-01-10 13:28:59 -0800 | [diff] [blame] | 212 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 213 | return transp |
| 214 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 215 | def _child_watcher_callback(self, pid, returncode, transp): |
| 216 | self.call_soon_threadsafe(transp._process_exited, returncode) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 217 | |
Neil Aspinall | f7686c1 | 2017-12-19 19:45:42 +0000 | [diff] [blame] | 218 | async def create_unix_connection( |
| 219 | self, protocol_factory, path=None, *, |
| 220 | ssl=None, sock=None, |
| 221 | server_hostname=None, |
Andrew Svetlov | 51eb1c6 | 2017-12-20 20:24:43 +0200 | [diff] [blame] | 222 | ssl_handshake_timeout=None): |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 223 | assert server_hostname is None or isinstance(server_hostname, str) |
| 224 | if ssl: |
| 225 | if server_hostname is None: |
| 226 | raise ValueError( |
| 227 | 'you have to pass server_hostname when using ssl') |
| 228 | else: |
| 229 | if server_hostname is not None: |
| 230 | raise ValueError('server_hostname is only meaningful with ssl') |
Andrew Svetlov | 51eb1c6 | 2017-12-20 20:24:43 +0200 | [diff] [blame] | 231 | if ssl_handshake_timeout is not None: |
| 232 | raise ValueError( |
| 233 | 'ssl_handshake_timeout is only meaningful with ssl') |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 234 | |
| 235 | if path is not None: |
| 236 | if sock is not None: |
| 237 | raise ValueError( |
| 238 | 'path and sock can not be specified at the same time') |
| 239 | |
Andrew Svetlov | cc83920 | 2017-11-29 18:23:43 +0200 | [diff] [blame] | 240 | path = os.fspath(path) |
Victor Stinner | 79a2952 | 2014-02-19 01:45:59 +0100 | [diff] [blame] | 241 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0) |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 242 | try: |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 243 | sock.setblocking(False) |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 244 | await self.sock_connect(sock, path) |
Victor Stinner | 79a2952 | 2014-02-19 01:45:59 +0100 | [diff] [blame] | 245 | except: |
| 246 | sock.close() |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 247 | raise |
| 248 | |
| 249 | else: |
| 250 | if sock is None: |
| 251 | raise ValueError('no path and sock were specified') |
Yury Selivanov | 36e7e97 | 2016-10-07 12:39:57 -0400 | [diff] [blame] | 252 | if (sock.family != socket.AF_UNIX or |
Yury Selivanov | a7bd64c | 2017-12-19 06:44:37 -0500 | [diff] [blame] | 253 | sock.type != socket.SOCK_STREAM): |
Yury Selivanov | 36e7e97 | 2016-10-07 12:39:57 -0400 | [diff] [blame] | 254 | raise ValueError( |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 255 | f'A UNIX Domain Stream Socket was expected, got {sock!r}') |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 256 | sock.setblocking(False) |
| 257 | |
Andrew Svetlov | 5f841b5 | 2017-12-09 00:23:48 +0200 | [diff] [blame] | 258 | transport, protocol = await self._create_connection_transport( |
Neil Aspinall | f7686c1 | 2017-12-19 19:45:42 +0000 | [diff] [blame] | 259 | sock, protocol_factory, ssl, server_hostname, |
| 260 | ssl_handshake_timeout=ssl_handshake_timeout) |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 261 | return transport, protocol |
| 262 | |
Neil Aspinall | f7686c1 | 2017-12-19 19:45:42 +0000 | [diff] [blame] | 263 | async def create_unix_server( |
| 264 | self, protocol_factory, path=None, *, |
| 265 | sock=None, backlog=100, ssl=None, |
Yury Selivanov | c9070d0 | 2018-01-25 18:08:09 -0500 | [diff] [blame] | 266 | ssl_handshake_timeout=None, |
| 267 | start_serving=True): |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 268 | if isinstance(ssl, bool): |
| 269 | raise TypeError('ssl argument must be an SSLContext or None') |
| 270 | |
Andrew Svetlov | 51eb1c6 | 2017-12-20 20:24:43 +0200 | [diff] [blame] | 271 | if ssl_handshake_timeout is not None and not ssl: |
| 272 | raise ValueError( |
| 273 | 'ssl_handshake_timeout is only meaningful with ssl') |
| 274 | |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 275 | if path is not None: |
Victor Stinner | 1fd03a4 | 2014-04-07 11:18:54 +0200 | [diff] [blame] | 276 | if sock is not None: |
| 277 | raise ValueError( |
| 278 | 'path and sock can not be specified at the same time') |
| 279 | |
Andrew Svetlov | cc83920 | 2017-11-29 18:23:43 +0200 | [diff] [blame] | 280 | path = os.fspath(path) |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 281 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 282 | |
Yury Selivanov | 908d55d | 2016-10-09 12:15:08 -0400 | [diff] [blame] | 283 | # Check for abstract socket. `str` and `bytes` paths are supported. |
| 284 | if path[0] not in (0, '\x00'): |
| 285 | try: |
| 286 | if stat.S_ISSOCK(os.stat(path).st_mode): |
| 287 | os.remove(path) |
| 288 | except FileNotFoundError: |
| 289 | pass |
| 290 | except OSError as err: |
| 291 | # Directory may have permissions only to create socket. |
Andrew Svetlov | cc83920 | 2017-11-29 18:23:43 +0200 | [diff] [blame] | 292 | logger.error('Unable to check or remove stale UNIX socket ' |
| 293 | '%r: %r', path, err) |
Yury Selivanov | 908d55d | 2016-10-09 12:15:08 -0400 | [diff] [blame] | 294 | |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 295 | try: |
| 296 | sock.bind(path) |
| 297 | except OSError as exc: |
Victor Stinner | 79a2952 | 2014-02-19 01:45:59 +0100 | [diff] [blame] | 298 | sock.close() |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 299 | if exc.errno == errno.EADDRINUSE: |
| 300 | # Let's improve the error message by adding |
| 301 | # with what exact address it occurs. |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 302 | msg = f'Address {path!r} is already in use' |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 303 | raise OSError(errno.EADDRINUSE, msg) from None |
| 304 | else: |
| 305 | raise |
Victor Stinner | 223a624 | 2014-06-04 00:11:52 +0200 | [diff] [blame] | 306 | except: |
| 307 | sock.close() |
| 308 | raise |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 309 | else: |
| 310 | if sock is None: |
| 311 | raise ValueError( |
| 312 | 'path was not specified, and no sock specified') |
| 313 | |
Yury Selivanov | 36e7e97 | 2016-10-07 12:39:57 -0400 | [diff] [blame] | 314 | if (sock.family != socket.AF_UNIX or |
Yury Selivanov | a7bd64c | 2017-12-19 06:44:37 -0500 | [diff] [blame] | 315 | sock.type != socket.SOCK_STREAM): |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 316 | raise ValueError( |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 317 | f'A UNIX Domain Stream Socket was expected, got {sock!r}') |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 318 | |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 319 | sock.setblocking(False) |
Yury Selivanov | c9070d0 | 2018-01-25 18:08:09 -0500 | [diff] [blame] | 320 | server = base_events.Server(self, [sock], protocol_factory, |
| 321 | ssl, backlog, ssl_handshake_timeout) |
| 322 | if start_serving: |
| 323 | server._start_serving() |
Yury Selivanov | dbf1022 | 2018-05-28 14:31:28 -0400 | [diff] [blame] | 324 | # Skip one loop iteration so that all 'loop.add_reader' |
| 325 | # go through. |
Yurii Karabas | e4fe303 | 2020-11-28 10:21:17 +0200 | [diff] [blame^] | 326 | await tasks.sleep(0) |
Yury Selivanov | c9070d0 | 2018-01-25 18:08:09 -0500 | [diff] [blame] | 327 | |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 328 | return server |
| 329 | |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 330 | async def _sock_sendfile_native(self, sock, file, offset, count): |
| 331 | try: |
| 332 | os.sendfile |
Pablo Galindo | 293dd23 | 2019-11-19 21:34:03 +0000 | [diff] [blame] | 333 | except AttributeError: |
Andrew Svetlov | 0baa72f | 2018-09-11 10:13:04 -0700 | [diff] [blame] | 334 | raise exceptions.SendfileNotAvailableError( |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 335 | "os.sendfile() is not available") |
| 336 | try: |
| 337 | fileno = file.fileno() |
| 338 | except (AttributeError, io.UnsupportedOperation) as err: |
Andrew Svetlov | 0baa72f | 2018-09-11 10:13:04 -0700 | [diff] [blame] | 339 | raise exceptions.SendfileNotAvailableError("not a regular file") |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 340 | try: |
| 341 | fsize = os.fstat(fileno).st_size |
Pablo Galindo | 293dd23 | 2019-11-19 21:34:03 +0000 | [diff] [blame] | 342 | except OSError: |
Andrew Svetlov | 0baa72f | 2018-09-11 10:13:04 -0700 | [diff] [blame] | 343 | raise exceptions.SendfileNotAvailableError("not a regular file") |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 344 | blocksize = count if count else fsize |
| 345 | if not blocksize: |
| 346 | return 0 # empty file |
| 347 | |
| 348 | fut = self.create_future() |
| 349 | self._sock_sendfile_native_impl(fut, None, sock, fileno, |
| 350 | offset, count, blocksize, 0) |
| 351 | return await fut |
| 352 | |
| 353 | def _sock_sendfile_native_impl(self, fut, registered_fd, sock, fileno, |
| 354 | offset, count, blocksize, total_sent): |
| 355 | fd = sock.fileno() |
| 356 | if registered_fd is not None: |
| 357 | # Remove the callback early. It should be rare that the |
| 358 | # selector says the fd is ready but the call still returns |
| 359 | # EAGAIN, and I am willing to take a hit in that case in |
| 360 | # order to simplify the common case. |
| 361 | self.remove_writer(registered_fd) |
| 362 | if fut.cancelled(): |
| 363 | self._sock_sendfile_update_filepos(fileno, offset, total_sent) |
| 364 | return |
| 365 | if count: |
| 366 | blocksize = count - total_sent |
| 367 | if blocksize <= 0: |
| 368 | self._sock_sendfile_update_filepos(fileno, offset, total_sent) |
| 369 | fut.set_result(total_sent) |
| 370 | return |
| 371 | |
| 372 | try: |
| 373 | sent = os.sendfile(fd, fileno, offset, blocksize) |
| 374 | except (BlockingIOError, InterruptedError): |
| 375 | if registered_fd is None: |
| 376 | self._sock_add_cancellation_callback(fut, sock) |
| 377 | self.add_writer(fd, self._sock_sendfile_native_impl, fut, |
| 378 | fd, sock, fileno, |
| 379 | offset, count, blocksize, total_sent) |
| 380 | except OSError as exc: |
Yury Selivanov | 2a2247c | 2018-01-27 17:22:01 -0500 | [diff] [blame] | 381 | if (registered_fd is not None and |
| 382 | exc.errno == errno.ENOTCONN and |
| 383 | type(exc) is not ConnectionError): |
| 384 | # If we have an ENOTCONN and this isn't a first call to |
| 385 | # sendfile(), i.e. the connection was closed in the middle |
| 386 | # of the operation, normalize the error to ConnectionError |
| 387 | # to make it consistent across all Posix systems. |
| 388 | new_exc = ConnectionError( |
| 389 | "socket is not connected", errno.ENOTCONN) |
| 390 | new_exc.__cause__ = exc |
| 391 | exc = new_exc |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 392 | if total_sent == 0: |
| 393 | # We can get here for different reasons, the main |
| 394 | # one being 'file' is not a regular mmap(2)-like |
| 395 | # file, in which case we'll fall back on using |
| 396 | # plain send(). |
Andrew Svetlov | 0baa72f | 2018-09-11 10:13:04 -0700 | [diff] [blame] | 397 | err = exceptions.SendfileNotAvailableError( |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 398 | "os.sendfile call failed") |
| 399 | self._sock_sendfile_update_filepos(fileno, offset, total_sent) |
| 400 | fut.set_exception(err) |
| 401 | else: |
| 402 | self._sock_sendfile_update_filepos(fileno, offset, total_sent) |
| 403 | fut.set_exception(exc) |
Yury Selivanov | 431b540 | 2019-05-27 14:45:12 +0200 | [diff] [blame] | 404 | except (SystemExit, KeyboardInterrupt): |
| 405 | raise |
| 406 | except BaseException as exc: |
Andrew Svetlov | 6b5a279 | 2018-01-16 19:59:34 +0200 | [diff] [blame] | 407 | self._sock_sendfile_update_filepos(fileno, offset, total_sent) |
| 408 | fut.set_exception(exc) |
| 409 | else: |
| 410 | if sent == 0: |
| 411 | # EOF |
| 412 | self._sock_sendfile_update_filepos(fileno, offset, total_sent) |
| 413 | fut.set_result(total_sent) |
| 414 | else: |
| 415 | offset += sent |
| 416 | total_sent += sent |
| 417 | if registered_fd is None: |
| 418 | self._sock_add_cancellation_callback(fut, sock) |
| 419 | self.add_writer(fd, self._sock_sendfile_native_impl, fut, |
| 420 | fd, sock, fileno, |
| 421 | offset, count, blocksize, total_sent) |
| 422 | |
| 423 | def _sock_sendfile_update_filepos(self, fileno, offset, total_sent): |
| 424 | if total_sent > 0: |
| 425 | os.lseek(fileno, offset, os.SEEK_SET) |
| 426 | |
| 427 | def _sock_add_cancellation_callback(self, fut, sock): |
| 428 | def cb(fut): |
| 429 | if fut.cancelled(): |
| 430 | fd = sock.fileno() |
| 431 | if fd != -1: |
| 432 | self.remove_writer(fd) |
| 433 | fut.add_done_callback(cb) |
| 434 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 435 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 436 | class _UnixReadPipeTransport(transports.ReadTransport): |
| 437 | |
Yury Selivanov | dec1a45 | 2014-02-18 22:27:48 -0500 | [diff] [blame] | 438 | 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] | 439 | |
| 440 | def __init__(self, loop, pipe, protocol, waiter=None, extra=None): |
| 441 | super().__init__(extra) |
| 442 | self._extra['pipe'] = pipe |
| 443 | self._loop = loop |
| 444 | self._pipe = pipe |
| 445 | self._fileno = pipe.fileno() |
Guido van Rossum | 4786787 | 2016-08-31 09:42:38 -0700 | [diff] [blame] | 446 | self._protocol = protocol |
| 447 | self._closing = False |
Andrew Svetlov | 58498bc | 2019-09-29 15:00:35 +0300 | [diff] [blame] | 448 | self._paused = False |
Guido van Rossum | 4786787 | 2016-08-31 09:42:38 -0700 | [diff] [blame] | 449 | |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 450 | mode = os.fstat(self._fileno).st_mode |
Guido van Rossum | 02757ea | 2014-01-10 13:30:04 -0800 | [diff] [blame] | 451 | if not (stat.S_ISFIFO(mode) or |
| 452 | stat.S_ISSOCK(mode) or |
| 453 | stat.S_ISCHR(mode)): |
Guido van Rossum | 4786787 | 2016-08-31 09:42:38 -0700 | [diff] [blame] | 454 | self._pipe = None |
| 455 | self._fileno = None |
| 456 | self._protocol = None |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 457 | raise ValueError("Pipe transport is for pipes/sockets only.") |
Guido van Rossum | 4786787 | 2016-08-31 09:42:38 -0700 | [diff] [blame] | 458 | |
Andrew Svetlov | cc83920 | 2017-11-29 18:23:43 +0200 | [diff] [blame] | 459 | os.set_blocking(self._fileno, False) |
Guido van Rossum | 4786787 | 2016-08-31 09:42:38 -0700 | [diff] [blame] | 460 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 461 | self._loop.call_soon(self._protocol.connection_made, self) |
Victor Stinner | 2934262 | 2015-01-29 14:15:19 +0100 | [diff] [blame] | 462 | # only start reading when connection_made() has been called |
Yury Selivanov | 5b8d4f9 | 2016-10-05 17:48:59 -0400 | [diff] [blame] | 463 | self._loop.call_soon(self._loop._add_reader, |
Victor Stinner | 2934262 | 2015-01-29 14:15:19 +0100 | [diff] [blame] | 464 | self._fileno, self._read_ready) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 465 | if waiter is not None: |
Victor Stinner | f07801b | 2015-01-29 00:36:35 +0100 | [diff] [blame] | 466 | # only wake up the waiter when connection_made() has been called |
Yury Selivanov | 5d7e3b6 | 2015-11-17 12:19:41 -0500 | [diff] [blame] | 467 | self._loop.call_soon(futures._set_result_unless_cancelled, |
| 468 | waiter, None) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 469 | |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 470 | def __repr__(self): |
Victor Stinner | 29ad011 | 2015-01-15 00:04:21 +0100 | [diff] [blame] | 471 | info = [self.__class__.__name__] |
| 472 | if self._pipe is None: |
| 473 | info.append('closed') |
| 474 | elif self._closing: |
| 475 | info.append('closing') |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 476 | info.append(f'fd={self._fileno}') |
Yury Selivanov | 5dc0933 | 2016-05-13 16:04:43 -0400 | [diff] [blame] | 477 | selector = getattr(self._loop, '_selector', None) |
| 478 | if self._pipe is not None and selector is not None: |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 479 | polling = selector_events._test_selector_event( |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 480 | selector, self._fileno, selectors.EVENT_READ) |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 481 | if polling: |
| 482 | info.append('polling') |
| 483 | else: |
| 484 | info.append('idle') |
Yury Selivanov | 5dc0933 | 2016-05-13 16:04:43 -0400 | [diff] [blame] | 485 | elif self._pipe is not None: |
| 486 | info.append('open') |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 487 | else: |
| 488 | info.append('closed') |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 489 | return '<{}>'.format(' '.join(info)) |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 490 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 491 | def _read_ready(self): |
| 492 | try: |
| 493 | data = os.read(self._fileno, self.max_size) |
| 494 | except (BlockingIOError, InterruptedError): |
| 495 | pass |
| 496 | except OSError as exc: |
Victor Stinner | 0ee29c2 | 2014-02-19 01:40:41 +0100 | [diff] [blame] | 497 | self._fatal_error(exc, 'Fatal read error on pipe transport') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 498 | else: |
| 499 | if data: |
| 500 | self._protocol.data_received(data) |
| 501 | else: |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 502 | if self._loop.get_debug(): |
| 503 | logger.info("%r was closed by peer", self) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 504 | self._closing = True |
Yury Selivanov | 5b8d4f9 | 2016-10-05 17:48:59 -0400 | [diff] [blame] | 505 | self._loop._remove_reader(self._fileno) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 506 | self._loop.call_soon(self._protocol.eof_received) |
| 507 | self._loop.call_soon(self._call_connection_lost, None) |
| 508 | |
Guido van Rossum | 57497ad | 2013-10-18 07:58:20 -0700 | [diff] [blame] | 509 | def pause_reading(self): |
Andrew Svetlov | 58498bc | 2019-09-29 15:00:35 +0300 | [diff] [blame] | 510 | if self._closing or self._paused: |
| 511 | return |
| 512 | self._paused = True |
Yury Selivanov | 5b8d4f9 | 2016-10-05 17:48:59 -0400 | [diff] [blame] | 513 | self._loop._remove_reader(self._fileno) |
Andrew Svetlov | 58498bc | 2019-09-29 15:00:35 +0300 | [diff] [blame] | 514 | if self._loop.get_debug(): |
| 515 | logger.debug("%r pauses reading", self) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 516 | |
Guido van Rossum | 57497ad | 2013-10-18 07:58:20 -0700 | [diff] [blame] | 517 | def resume_reading(self): |
Andrew Svetlov | 58498bc | 2019-09-29 15:00:35 +0300 | [diff] [blame] | 518 | if self._closing or not self._paused: |
| 519 | return |
| 520 | self._paused = False |
Yury Selivanov | 5b8d4f9 | 2016-10-05 17:48:59 -0400 | [diff] [blame] | 521 | self._loop._add_reader(self._fileno, self._read_ready) |
Andrew Svetlov | 58498bc | 2019-09-29 15:00:35 +0300 | [diff] [blame] | 522 | if self._loop.get_debug(): |
| 523 | logger.debug("%r resumes reading", self) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 524 | |
Yury Selivanov | a05a6ef | 2016-09-11 21:11:02 -0400 | [diff] [blame] | 525 | def set_protocol(self, protocol): |
| 526 | self._protocol = protocol |
| 527 | |
| 528 | def get_protocol(self): |
| 529 | return self._protocol |
| 530 | |
Yury Selivanov | 5bb1afb | 2015-11-16 12:43:21 -0500 | [diff] [blame] | 531 | def is_closing(self): |
| 532 | return self._closing |
| 533 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 534 | def close(self): |
| 535 | if not self._closing: |
| 536 | self._close(None) |
| 537 | |
Victor Stinner | fb2c346 | 2019-01-10 11:24:40 +0100 | [diff] [blame] | 538 | def __del__(self, _warn=warnings.warn): |
INADA Naoki | 3e2ad8e | 2017-04-25 10:57:18 +0900 | [diff] [blame] | 539 | if self._pipe is not None: |
Victor Stinner | fb2c346 | 2019-01-10 11:24:40 +0100 | [diff] [blame] | 540 | _warn(f"unclosed transport {self!r}", ResourceWarning, source=self) |
INADA Naoki | 3e2ad8e | 2017-04-25 10:57:18 +0900 | [diff] [blame] | 541 | self._pipe.close() |
Victor Stinner | 978a9af | 2015-01-29 17:50:58 +0100 | [diff] [blame] | 542 | |
Victor Stinner | 0ee29c2 | 2014-02-19 01:40:41 +0100 | [diff] [blame] | 543 | def _fatal_error(self, exc, message='Fatal error on pipe transport'): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 544 | # should be called by exception handler only |
Victor Stinner | b261475 | 2014-08-25 23:20:52 +0200 | [diff] [blame] | 545 | if (isinstance(exc, OSError) and exc.errno == errno.EIO): |
| 546 | if self._loop.get_debug(): |
| 547 | logger.debug("%r: %s", self, message, exc_info=True) |
| 548 | else: |
Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 549 | self._loop.call_exception_handler({ |
Victor Stinner | 0ee29c2 | 2014-02-19 01:40:41 +0100 | [diff] [blame] | 550 | 'message': message, |
Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 551 | 'exception': exc, |
| 552 | 'transport': self, |
| 553 | 'protocol': self._protocol, |
| 554 | }) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 555 | self._close(exc) |
| 556 | |
| 557 | def _close(self, exc): |
| 558 | self._closing = True |
Yury Selivanov | 5b8d4f9 | 2016-10-05 17:48:59 -0400 | [diff] [blame] | 559 | self._loop._remove_reader(self._fileno) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 560 | self._loop.call_soon(self._call_connection_lost, exc) |
| 561 | |
| 562 | def _call_connection_lost(self, exc): |
| 563 | try: |
| 564 | self._protocol.connection_lost(exc) |
| 565 | finally: |
| 566 | self._pipe.close() |
| 567 | self._pipe = None |
| 568 | self._protocol = None |
| 569 | self._loop = None |
| 570 | |
| 571 | |
Yury Selivanov | 3cb9914 | 2014-02-18 18:41:13 -0500 | [diff] [blame] | 572 | class _UnixWritePipeTransport(transports._FlowControlMixin, |
Guido van Rossum | 47fb97e | 2014-01-29 13:20:39 -0800 | [diff] [blame] | 573 | transports.WriteTransport): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 574 | |
| 575 | def __init__(self, loop, pipe, protocol, waiter=None, extra=None): |
Victor Stinner | 004adb9 | 2014-11-05 15:27:41 +0100 | [diff] [blame] | 576 | super().__init__(extra, loop) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 577 | self._extra['pipe'] = pipe |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 578 | self._pipe = pipe |
| 579 | self._fileno = pipe.fileno() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 580 | self._protocol = protocol |
Yury Selivanov | 4c5bf3b | 2016-09-15 16:51:48 -0400 | [diff] [blame] | 581 | self._buffer = bytearray() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 582 | self._conn_lost = 0 |
| 583 | self._closing = False # Set when close() or write_eof() called. |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 584 | |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 585 | mode = os.fstat(self._fileno).st_mode |
Guido van Rossum | 8b7918a | 2016-08-31 09:40:18 -0700 | [diff] [blame] | 586 | is_char = stat.S_ISCHR(mode) |
| 587 | is_fifo = stat.S_ISFIFO(mode) |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 588 | is_socket = stat.S_ISSOCK(mode) |
Guido van Rossum | 8b7918a | 2016-08-31 09:40:18 -0700 | [diff] [blame] | 589 | if not (is_char or is_fifo or is_socket): |
Guido van Rossum | 4786787 | 2016-08-31 09:42:38 -0700 | [diff] [blame] | 590 | self._pipe = None |
| 591 | self._fileno = None |
| 592 | self._protocol = None |
Victor Stinner | 8dffc45 | 2014-01-25 15:32:06 +0100 | [diff] [blame] | 593 | raise ValueError("Pipe transport is only for " |
| 594 | "pipes, sockets and character devices") |
Guido van Rossum | 4786787 | 2016-08-31 09:42:38 -0700 | [diff] [blame] | 595 | |
Andrew Svetlov | cc83920 | 2017-11-29 18:23:43 +0200 | [diff] [blame] | 596 | os.set_blocking(self._fileno, False) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 597 | self._loop.call_soon(self._protocol.connection_made, self) |
Victor Stinner | 2934262 | 2015-01-29 14:15:19 +0100 | [diff] [blame] | 598 | |
| 599 | # On AIX, the reader trick (to be notified when the read end of the |
| 600 | # socket is closed) only works for sockets. On other platforms it |
| 601 | # works for pipes and sockets. (Exception: OS X 10.4? Issue #19294.) |
Guido van Rossum | 8b7918a | 2016-08-31 09:40:18 -0700 | [diff] [blame] | 602 | if is_socket or (is_fifo and not sys.platform.startswith("aix")): |
Victor Stinner | 2934262 | 2015-01-29 14:15:19 +0100 | [diff] [blame] | 603 | # only start reading when connection_made() has been called |
Yury Selivanov | 5b8d4f9 | 2016-10-05 17:48:59 -0400 | [diff] [blame] | 604 | self._loop.call_soon(self._loop._add_reader, |
Victor Stinner | 2934262 | 2015-01-29 14:15:19 +0100 | [diff] [blame] | 605 | self._fileno, self._read_ready) |
| 606 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 607 | if waiter is not None: |
Victor Stinner | f07801b | 2015-01-29 00:36:35 +0100 | [diff] [blame] | 608 | # only wake up the waiter when connection_made() has been called |
Yury Selivanov | 5d7e3b6 | 2015-11-17 12:19:41 -0500 | [diff] [blame] | 609 | self._loop.call_soon(futures._set_result_unless_cancelled, |
| 610 | waiter, None) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 611 | |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 612 | def __repr__(self): |
Victor Stinner | 29ad011 | 2015-01-15 00:04:21 +0100 | [diff] [blame] | 613 | info = [self.__class__.__name__] |
| 614 | if self._pipe is None: |
| 615 | info.append('closed') |
| 616 | elif self._closing: |
| 617 | info.append('closing') |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 618 | info.append(f'fd={self._fileno}') |
Yury Selivanov | 5dc0933 | 2016-05-13 16:04:43 -0400 | [diff] [blame] | 619 | selector = getattr(self._loop, '_selector', None) |
| 620 | if self._pipe is not None and selector is not None: |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 621 | polling = selector_events._test_selector_event( |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 622 | selector, self._fileno, selectors.EVENT_WRITE) |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 623 | if polling: |
| 624 | info.append('polling') |
| 625 | else: |
| 626 | info.append('idle') |
| 627 | |
| 628 | bufsize = self.get_write_buffer_size() |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 629 | info.append(f'bufsize={bufsize}') |
Yury Selivanov | 5dc0933 | 2016-05-13 16:04:43 -0400 | [diff] [blame] | 630 | elif self._pipe is not None: |
| 631 | info.append('open') |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 632 | else: |
| 633 | info.append('closed') |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 634 | return '<{}>'.format(' '.join(info)) |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 635 | |
Guido van Rossum | 47fb97e | 2014-01-29 13:20:39 -0800 | [diff] [blame] | 636 | def get_write_buffer_size(self): |
Yury Selivanov | 4c5bf3b | 2016-09-15 16:51:48 -0400 | [diff] [blame] | 637 | return len(self._buffer) |
Guido van Rossum | 47fb97e | 2014-01-29 13:20:39 -0800 | [diff] [blame] | 638 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 639 | def _read_ready(self): |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 640 | # Pipe was closed by peer. |
Victor Stinner | e912e65 | 2014-07-12 03:11:53 +0200 | [diff] [blame] | 641 | if self._loop.get_debug(): |
| 642 | logger.info("%r was closed by peer", self) |
Victor Stinner | 61b3c9b | 2014-01-31 13:04:28 +0100 | [diff] [blame] | 643 | if self._buffer: |
| 644 | self._close(BrokenPipeError()) |
| 645 | else: |
| 646 | self._close() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 647 | |
| 648 | def write(self, data): |
Guido van Rossum | 47fb97e | 2014-01-29 13:20:39 -0800 | [diff] [blame] | 649 | assert isinstance(data, (bytes, bytearray, memoryview)), repr(data) |
| 650 | if isinstance(data, bytearray): |
| 651 | data = memoryview(data) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 652 | if not data: |
| 653 | return |
| 654 | |
| 655 | if self._conn_lost or self._closing: |
| 656 | if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES: |
Guido van Rossum | fc29e0f | 2013-10-17 15:39:45 -0700 | [diff] [blame] | 657 | logger.warning('pipe closed by peer or ' |
| 658 | 'os.write(pipe, data) raised exception.') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 659 | self._conn_lost += 1 |
| 660 | return |
| 661 | |
| 662 | if not self._buffer: |
| 663 | # Attempt to send it right away first. |
| 664 | try: |
| 665 | n = os.write(self._fileno, data) |
| 666 | except (BlockingIOError, InterruptedError): |
| 667 | n = 0 |
Yury Selivanov | 431b540 | 2019-05-27 14:45:12 +0200 | [diff] [blame] | 668 | except (SystemExit, KeyboardInterrupt): |
| 669 | raise |
| 670 | except BaseException as exc: |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 671 | self._conn_lost += 1 |
Victor Stinner | 0ee29c2 | 2014-02-19 01:40:41 +0100 | [diff] [blame] | 672 | self._fatal_error(exc, 'Fatal write error on pipe transport') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 673 | return |
| 674 | if n == len(data): |
| 675 | return |
| 676 | elif n > 0: |
Yury Selivanov | 4c5bf3b | 2016-09-15 16:51:48 -0400 | [diff] [blame] | 677 | data = memoryview(data)[n:] |
Yury Selivanov | 5b8d4f9 | 2016-10-05 17:48:59 -0400 | [diff] [blame] | 678 | self._loop._add_writer(self._fileno, self._write_ready) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 679 | |
Yury Selivanov | 4c5bf3b | 2016-09-15 16:51:48 -0400 | [diff] [blame] | 680 | self._buffer += data |
Guido van Rossum | 47fb97e | 2014-01-29 13:20:39 -0800 | [diff] [blame] | 681 | self._maybe_pause_protocol() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 682 | |
| 683 | def _write_ready(self): |
Yury Selivanov | 4c5bf3b | 2016-09-15 16:51:48 -0400 | [diff] [blame] | 684 | assert self._buffer, 'Data should not be empty' |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 685 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 686 | try: |
Yury Selivanov | 4c5bf3b | 2016-09-15 16:51:48 -0400 | [diff] [blame] | 687 | n = os.write(self._fileno, self._buffer) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 688 | except (BlockingIOError, InterruptedError): |
Yury Selivanov | 4c5bf3b | 2016-09-15 16:51:48 -0400 | [diff] [blame] | 689 | pass |
Yury Selivanov | 431b540 | 2019-05-27 14:45:12 +0200 | [diff] [blame] | 690 | except (SystemExit, KeyboardInterrupt): |
| 691 | raise |
| 692 | except BaseException as exc: |
Yury Selivanov | 4c5bf3b | 2016-09-15 16:51:48 -0400 | [diff] [blame] | 693 | self._buffer.clear() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 694 | self._conn_lost += 1 |
| 695 | # Remove writer here, _fatal_error() doesn't it |
| 696 | # because _buffer is empty. |
Yury Selivanov | 5b8d4f9 | 2016-10-05 17:48:59 -0400 | [diff] [blame] | 697 | self._loop._remove_writer(self._fileno) |
Victor Stinner | 0ee29c2 | 2014-02-19 01:40:41 +0100 | [diff] [blame] | 698 | self._fatal_error(exc, 'Fatal write error on pipe transport') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 699 | else: |
Yury Selivanov | 4c5bf3b | 2016-09-15 16:51:48 -0400 | [diff] [blame] | 700 | if n == len(self._buffer): |
| 701 | self._buffer.clear() |
Yury Selivanov | 5b8d4f9 | 2016-10-05 17:48:59 -0400 | [diff] [blame] | 702 | self._loop._remove_writer(self._fileno) |
Guido van Rossum | 47fb97e | 2014-01-29 13:20:39 -0800 | [diff] [blame] | 703 | self._maybe_resume_protocol() # May append to buffer. |
Yury Selivanov | 4c5bf3b | 2016-09-15 16:51:48 -0400 | [diff] [blame] | 704 | if self._closing: |
Yury Selivanov | 5b8d4f9 | 2016-10-05 17:48:59 -0400 | [diff] [blame] | 705 | self._loop._remove_reader(self._fileno) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 706 | self._call_connection_lost(None) |
| 707 | return |
| 708 | elif n > 0: |
Yury Selivanov | 4c5bf3b | 2016-09-15 16:51:48 -0400 | [diff] [blame] | 709 | del self._buffer[:n] |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 710 | |
| 711 | def can_write_eof(self): |
| 712 | return True |
| 713 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 714 | def write_eof(self): |
| 715 | if self._closing: |
| 716 | return |
| 717 | assert self._pipe |
| 718 | self._closing = True |
| 719 | if not self._buffer: |
Yury Selivanov | 5b8d4f9 | 2016-10-05 17:48:59 -0400 | [diff] [blame] | 720 | self._loop._remove_reader(self._fileno) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 721 | self._loop.call_soon(self._call_connection_lost, None) |
| 722 | |
Yury Selivanov | a05a6ef | 2016-09-11 21:11:02 -0400 | [diff] [blame] | 723 | def set_protocol(self, protocol): |
| 724 | self._protocol = protocol |
| 725 | |
| 726 | def get_protocol(self): |
| 727 | return self._protocol |
| 728 | |
Yury Selivanov | 5bb1afb | 2015-11-16 12:43:21 -0500 | [diff] [blame] | 729 | def is_closing(self): |
| 730 | return self._closing |
| 731 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 732 | def close(self): |
Victor Stinner | 41ed958 | 2015-01-15 13:16:50 +0100 | [diff] [blame] | 733 | if self._pipe is not None and not self._closing: |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 734 | # write_eof is all what we needed to close the write pipe |
| 735 | self.write_eof() |
| 736 | |
Victor Stinner | fb2c346 | 2019-01-10 11:24:40 +0100 | [diff] [blame] | 737 | def __del__(self, _warn=warnings.warn): |
INADA Naoki | 3e2ad8e | 2017-04-25 10:57:18 +0900 | [diff] [blame] | 738 | if self._pipe is not None: |
Victor Stinner | fb2c346 | 2019-01-10 11:24:40 +0100 | [diff] [blame] | 739 | _warn(f"unclosed transport {self!r}", ResourceWarning, source=self) |
INADA Naoki | 3e2ad8e | 2017-04-25 10:57:18 +0900 | [diff] [blame] | 740 | self._pipe.close() |
Victor Stinner | 978a9af | 2015-01-29 17:50:58 +0100 | [diff] [blame] | 741 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 742 | def abort(self): |
| 743 | self._close(None) |
| 744 | |
Victor Stinner | 0ee29c2 | 2014-02-19 01:40:41 +0100 | [diff] [blame] | 745 | def _fatal_error(self, exc, message='Fatal error on pipe transport'): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 746 | # should be called by exception handler only |
Andrew Svetlov | 1f39c28 | 2019-05-27 16:28:34 +0300 | [diff] [blame] | 747 | if isinstance(exc, OSError): |
Victor Stinner | b261475 | 2014-08-25 23:20:52 +0200 | [diff] [blame] | 748 | if self._loop.get_debug(): |
| 749 | logger.debug("%r: %s", self, message, exc_info=True) |
| 750 | else: |
Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 751 | self._loop.call_exception_handler({ |
Victor Stinner | 0ee29c2 | 2014-02-19 01:40:41 +0100 | [diff] [blame] | 752 | 'message': message, |
Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 753 | 'exception': exc, |
| 754 | 'transport': self, |
| 755 | 'protocol': self._protocol, |
| 756 | }) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 757 | self._close(exc) |
| 758 | |
| 759 | def _close(self, exc=None): |
| 760 | self._closing = True |
| 761 | if self._buffer: |
Yury Selivanov | 5b8d4f9 | 2016-10-05 17:48:59 -0400 | [diff] [blame] | 762 | self._loop._remove_writer(self._fileno) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 763 | self._buffer.clear() |
Yury Selivanov | 5b8d4f9 | 2016-10-05 17:48:59 -0400 | [diff] [blame] | 764 | self._loop._remove_reader(self._fileno) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 765 | self._loop.call_soon(self._call_connection_lost, exc) |
| 766 | |
| 767 | def _call_connection_lost(self, exc): |
| 768 | try: |
| 769 | self._protocol.connection_lost(exc) |
| 770 | finally: |
| 771 | self._pipe.close() |
| 772 | self._pipe = None |
| 773 | self._protocol = None |
| 774 | self._loop = None |
| 775 | |
| 776 | |
Guido van Rossum | 5969128 | 2013-10-30 14:52:03 -0700 | [diff] [blame] | 777 | class _UnixSubprocessTransport(base_subprocess.BaseSubprocessTransport): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 778 | |
Guido van Rossum | 5969128 | 2013-10-30 14:52:03 -0700 | [diff] [blame] | 779 | def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs): |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 780 | stdin_w = None |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 781 | if stdin == subprocess.PIPE: |
Guido van Rossum | 934f6ea | 2013-10-21 20:37:14 -0700 | [diff] [blame] | 782 | # Use a socket pair for stdin, since not all platforms |
| 783 | # support selecting read events on the write end of a |
| 784 | # socket (which we use in order to detect closing of the |
| 785 | # other end). Notably this is needed on AIX, and works |
| 786 | # just fine on other platforms. |
Victor Stinner | a10dc3e | 2017-11-28 11:15:26 +0100 | [diff] [blame] | 787 | stdin, stdin_w = socket.socketpair() |
Niklas Fiekas | 9932fd9 | 2019-05-20 14:02:17 +0200 | [diff] [blame] | 788 | try: |
| 789 | self._proc = subprocess.Popen( |
| 790 | args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr, |
| 791 | universal_newlines=False, bufsize=bufsize, **kwargs) |
| 792 | if stdin_w is not None: |
| 793 | stdin.close() |
| 794 | self._proc.stdin = open(stdin_w.detach(), 'wb', buffering=bufsize) |
| 795 | stdin_w = None |
| 796 | finally: |
| 797 | if stdin_w is not None: |
| 798 | stdin.close() |
| 799 | stdin_w.close() |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 800 | |
| 801 | |
| 802 | class AbstractChildWatcher: |
| 803 | """Abstract base class for monitoring child processes. |
| 804 | |
| 805 | Objects derived from this class monitor a collection of subprocesses and |
| 806 | report their termination or interruption by a signal. |
| 807 | |
| 808 | New callbacks are registered with .add_child_handler(). Starting a new |
| 809 | process must be done within a 'with' block to allow the watcher to suspend |
| 810 | its activity until the new process if fully registered (this is needed to |
| 811 | prevent a race condition in some implementations). |
| 812 | |
| 813 | Example: |
| 814 | with watcher: |
| 815 | proc = subprocess.Popen("sleep 1") |
| 816 | watcher.add_child_handler(proc.pid, callback) |
| 817 | |
| 818 | Notes: |
| 819 | Implementations of this class must be thread-safe. |
| 820 | |
| 821 | Since child watcher objects may catch the SIGCHLD signal and call |
| 822 | waitpid(-1), there should be only one active object per process. |
| 823 | """ |
| 824 | |
| 825 | def add_child_handler(self, pid, callback, *args): |
| 826 | """Register a new child handler. |
| 827 | |
| 828 | Arrange for callback(pid, returncode, *args) to be called when |
| 829 | process 'pid' terminates. Specifying another callback for the same |
| 830 | process replaces the previous handler. |
| 831 | |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 832 | Note: callback() must be thread-safe. |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 833 | """ |
| 834 | raise NotImplementedError() |
| 835 | |
| 836 | def remove_child_handler(self, pid): |
| 837 | """Removes the handler for process 'pid'. |
| 838 | |
| 839 | The function returns True if the handler was successfully removed, |
| 840 | False if there was nothing to remove.""" |
| 841 | |
| 842 | raise NotImplementedError() |
| 843 | |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 844 | def attach_loop(self, loop): |
| 845 | """Attach the watcher to an event loop. |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 846 | |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 847 | If the watcher was previously attached to an event loop, then it is |
| 848 | first detached before attaching to the new loop. |
| 849 | |
| 850 | Note: loop may be None. |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 851 | """ |
| 852 | raise NotImplementedError() |
| 853 | |
| 854 | def close(self): |
| 855 | """Close the watcher. |
| 856 | |
| 857 | This must be called to make sure that any underlying resource is freed. |
| 858 | """ |
| 859 | raise NotImplementedError() |
| 860 | |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 861 | def is_active(self): |
| 862 | """Return ``True`` if the watcher is active and is used by the event loop. |
| 863 | |
| 864 | Return True if the watcher is installed and ready to handle process exit |
| 865 | notifications. |
| 866 | |
| 867 | """ |
| 868 | raise NotImplementedError() |
| 869 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 870 | def __enter__(self): |
| 871 | """Enter the watcher's context and allow starting new processes |
| 872 | |
| 873 | This function must return self""" |
| 874 | raise NotImplementedError() |
| 875 | |
| 876 | def __exit__(self, a, b, c): |
| 877 | """Exit the watcher's context""" |
| 878 | raise NotImplementedError() |
| 879 | |
| 880 | |
Benjamin Peterson | 3ccdd9b | 2019-11-13 19:08:50 -0800 | [diff] [blame] | 881 | class PidfdChildWatcher(AbstractChildWatcher): |
| 882 | """Child watcher implementation using Linux's pid file descriptors. |
| 883 | |
| 884 | This child watcher polls process file descriptors (pidfds) to await child |
| 885 | process termination. In some respects, PidfdChildWatcher is a "Goldilocks" |
| 886 | child watcher implementation. It doesn't require signals or threads, doesn't |
| 887 | interfere with any processes launched outside the event loop, and scales |
| 888 | linearly with the number of subprocesses launched by the event loop. The |
| 889 | main disadvantage is that pidfds are specific to Linux, and only work on |
| 890 | recent (5.3+) kernels. |
| 891 | """ |
| 892 | |
| 893 | def __init__(self): |
| 894 | self._loop = None |
| 895 | self._callbacks = {} |
| 896 | |
| 897 | def __enter__(self): |
| 898 | return self |
| 899 | |
| 900 | def __exit__(self, exc_type, exc_value, exc_traceback): |
| 901 | pass |
| 902 | |
| 903 | def is_active(self): |
| 904 | return self._loop is not None and self._loop.is_running() |
| 905 | |
| 906 | def close(self): |
| 907 | self.attach_loop(None) |
| 908 | |
| 909 | def attach_loop(self, loop): |
| 910 | if self._loop is not None and loop is None and self._callbacks: |
| 911 | warnings.warn( |
| 912 | 'A loop is being detached ' |
| 913 | 'from a child watcher with pending handlers', |
| 914 | RuntimeWarning) |
| 915 | for pidfd, _, _ in self._callbacks.values(): |
| 916 | self._loop._remove_reader(pidfd) |
| 917 | os.close(pidfd) |
| 918 | self._callbacks.clear() |
| 919 | self._loop = loop |
| 920 | |
| 921 | def add_child_handler(self, pid, callback, *args): |
| 922 | existing = self._callbacks.get(pid) |
| 923 | if existing is not None: |
| 924 | self._callbacks[pid] = existing[0], callback, args |
| 925 | else: |
| 926 | pidfd = os.pidfd_open(pid) |
| 927 | self._loop._add_reader(pidfd, self._do_wait, pid) |
| 928 | self._callbacks[pid] = pidfd, callback, args |
| 929 | |
| 930 | def _do_wait(self, pid): |
| 931 | pidfd, callback, args = self._callbacks.pop(pid) |
| 932 | self._loop._remove_reader(pidfd) |
Victor Stinner | e76ee1a | 2019-12-06 16:32:41 +0100 | [diff] [blame] | 933 | try: |
| 934 | _, status = os.waitpid(pid, 0) |
| 935 | except ChildProcessError: |
| 936 | # The child process is already reaped |
| 937 | # (may happen if waitpid() is called elsewhere). |
| 938 | returncode = 255 |
| 939 | logger.warning( |
| 940 | "child process pid %d exit status already read: " |
| 941 | " will report returncode 255", |
| 942 | pid) |
| 943 | else: |
| 944 | returncode = _compute_returncode(status) |
| 945 | |
Benjamin Peterson | 3ccdd9b | 2019-11-13 19:08:50 -0800 | [diff] [blame] | 946 | os.close(pidfd) |
Benjamin Peterson | 3ccdd9b | 2019-11-13 19:08:50 -0800 | [diff] [blame] | 947 | callback(pid, returncode, *args) |
| 948 | |
| 949 | def remove_child_handler(self, pid): |
| 950 | try: |
| 951 | pidfd, _, _ = self._callbacks.pop(pid) |
| 952 | except KeyError: |
| 953 | return False |
| 954 | self._loop._remove_reader(pidfd) |
| 955 | os.close(pidfd) |
| 956 | return True |
| 957 | |
| 958 | |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 959 | def _compute_returncode(status): |
| 960 | if os.WIFSIGNALED(status): |
| 961 | # The child process died because of a signal. |
| 962 | return -os.WTERMSIG(status) |
| 963 | elif os.WIFEXITED(status): |
| 964 | # The child process exited (e.g sys.exit()). |
| 965 | return os.WEXITSTATUS(status) |
| 966 | else: |
| 967 | # The child exited, but we don't understand its status. |
| 968 | # This shouldn't happen, but if it does, let's just |
| 969 | # return that status; perhaps that helps debug it. |
| 970 | return status |
| 971 | |
| 972 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 973 | class BaseChildWatcher(AbstractChildWatcher): |
| 974 | |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 975 | def __init__(self): |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 976 | self._loop = None |
Yury Selivanov | 9eb6c67 | 2016-10-05 16:57:12 -0400 | [diff] [blame] | 977 | self._callbacks = {} |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 978 | |
| 979 | def close(self): |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 980 | self.attach_loop(None) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 981 | |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 982 | def is_active(self): |
| 983 | return self._loop is not None and self._loop.is_running() |
| 984 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 985 | def _do_waitpid(self, expected_pid): |
| 986 | raise NotImplementedError() |
| 987 | |
| 988 | def _do_waitpid_all(self): |
| 989 | raise NotImplementedError() |
| 990 | |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 991 | def attach_loop(self, loop): |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 992 | assert loop is None or isinstance(loop, events.AbstractEventLoop) |
| 993 | |
Yury Selivanov | 9eb6c67 | 2016-10-05 16:57:12 -0400 | [diff] [blame] | 994 | if self._loop is not None and loop is None and self._callbacks: |
| 995 | warnings.warn( |
| 996 | 'A loop is being detached ' |
| 997 | 'from a child watcher with pending handlers', |
| 998 | RuntimeWarning) |
| 999 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1000 | if self._loop is not None: |
| 1001 | self._loop.remove_signal_handler(signal.SIGCHLD) |
| 1002 | |
| 1003 | self._loop = loop |
| 1004 | if loop is not None: |
| 1005 | loop.add_signal_handler(signal.SIGCHLD, self._sig_chld) |
| 1006 | |
| 1007 | # Prevent a race condition in case a child terminated |
| 1008 | # during the switch. |
| 1009 | self._do_waitpid_all() |
| 1010 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1011 | def _sig_chld(self): |
| 1012 | try: |
| 1013 | self._do_waitpid_all() |
Yury Selivanov | 431b540 | 2019-05-27 14:45:12 +0200 | [diff] [blame] | 1014 | except (SystemExit, KeyboardInterrupt): |
| 1015 | raise |
| 1016 | except BaseException as exc: |
Yury Selivanov | 569efa2 | 2014-02-18 18:02:19 -0500 | [diff] [blame] | 1017 | # self._loop should always be available here |
| 1018 | # as '_sig_chld' is added as a signal handler |
| 1019 | # in 'attach_loop' |
| 1020 | self._loop.call_exception_handler({ |
| 1021 | 'message': 'Unknown exception in SIGCHLD handler', |
| 1022 | 'exception': exc, |
| 1023 | }) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1024 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1025 | |
| 1026 | class SafeChildWatcher(BaseChildWatcher): |
| 1027 | """'Safe' child watcher implementation. |
| 1028 | |
| 1029 | This implementation avoids disrupting other code spawning processes by |
| 1030 | polling explicitly each process in the SIGCHLD handler instead of calling |
| 1031 | os.waitpid(-1). |
| 1032 | |
| 1033 | This is a safe solution but it has a significant overhead when handling a |
| 1034 | big number of children (O(n) each time SIGCHLD is raised) |
| 1035 | """ |
| 1036 | |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 1037 | def close(self): |
| 1038 | self._callbacks.clear() |
| 1039 | super().close() |
| 1040 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1041 | def __enter__(self): |
| 1042 | return self |
| 1043 | |
| 1044 | def __exit__(self, a, b, c): |
| 1045 | pass |
| 1046 | |
| 1047 | def add_child_handler(self, pid, callback, *args): |
Victor Stinner | 47cd10d | 2015-01-30 00:05:19 +0100 | [diff] [blame] | 1048 | self._callbacks[pid] = (callback, args) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1049 | |
| 1050 | # Prevent a race condition in case the child is already terminated. |
| 1051 | self._do_waitpid(pid) |
| 1052 | |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 1053 | def remove_child_handler(self, pid): |
| 1054 | try: |
| 1055 | del self._callbacks[pid] |
| 1056 | return True |
| 1057 | except KeyError: |
| 1058 | return False |
| 1059 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1060 | def _do_waitpid_all(self): |
| 1061 | |
| 1062 | for pid in list(self._callbacks): |
| 1063 | self._do_waitpid(pid) |
| 1064 | |
| 1065 | def _do_waitpid(self, expected_pid): |
| 1066 | assert expected_pid > 0 |
| 1067 | |
| 1068 | try: |
| 1069 | pid, status = os.waitpid(expected_pid, os.WNOHANG) |
| 1070 | except ChildProcessError: |
| 1071 | # The child process is already reaped |
| 1072 | # (may happen if waitpid() is called elsewhere). |
| 1073 | pid = expected_pid |
| 1074 | returncode = 255 |
| 1075 | logger.warning( |
| 1076 | "Unknown child process pid %d, will report returncode 255", |
| 1077 | pid) |
| 1078 | else: |
| 1079 | if pid == 0: |
| 1080 | # The child process is still alive. |
| 1081 | return |
| 1082 | |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 1083 | returncode = _compute_returncode(status) |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 1084 | if self._loop.get_debug(): |
| 1085 | logger.debug('process %s exited with returncode %s', |
| 1086 | expected_pid, returncode) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1087 | |
| 1088 | try: |
| 1089 | callback, args = self._callbacks.pop(pid) |
| 1090 | except KeyError: # pragma: no cover |
| 1091 | # May happen if .remove_child_handler() is called |
| 1092 | # after os.waitpid() returns. |
Victor Stinner | b261475 | 2014-08-25 23:20:52 +0200 | [diff] [blame] | 1093 | if self._loop.get_debug(): |
| 1094 | logger.warning("Child watcher got an unexpected pid: %r", |
| 1095 | pid, exc_info=True) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1096 | else: |
| 1097 | callback(pid, returncode, *args) |
| 1098 | |
| 1099 | |
| 1100 | class FastChildWatcher(BaseChildWatcher): |
| 1101 | """'Fast' child watcher implementation. |
| 1102 | |
| 1103 | This implementation reaps every terminated processes by calling |
| 1104 | os.waitpid(-1) directly, possibly breaking other code spawning processes |
| 1105 | and waiting for their termination. |
| 1106 | |
| 1107 | There is no noticeable overhead when handling a big number of children |
| 1108 | (O(1) each time a child terminates). |
| 1109 | """ |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 1110 | def __init__(self): |
| 1111 | super().__init__() |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1112 | self._lock = threading.Lock() |
| 1113 | self._zombies = {} |
| 1114 | self._forks = 0 |
| 1115 | |
| 1116 | def close(self): |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 1117 | self._callbacks.clear() |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1118 | self._zombies.clear() |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 1119 | super().close() |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1120 | |
| 1121 | def __enter__(self): |
| 1122 | with self._lock: |
| 1123 | self._forks += 1 |
| 1124 | |
| 1125 | return self |
| 1126 | |
| 1127 | def __exit__(self, a, b, c): |
| 1128 | with self._lock: |
| 1129 | self._forks -= 1 |
| 1130 | |
| 1131 | if self._forks or not self._zombies: |
| 1132 | return |
| 1133 | |
| 1134 | collateral_victims = str(self._zombies) |
| 1135 | self._zombies.clear() |
| 1136 | |
| 1137 | logger.warning( |
| 1138 | "Caught subprocesses termination from unknown pids: %s", |
| 1139 | collateral_victims) |
| 1140 | |
| 1141 | def add_child_handler(self, pid, callback, *args): |
| 1142 | assert self._forks, "Must use the context manager" |
Yury Selivanov | 9eb6c67 | 2016-10-05 16:57:12 -0400 | [diff] [blame] | 1143 | |
Guido van Rossum | ab27a9f | 2014-01-25 16:32:17 -0800 | [diff] [blame] | 1144 | with self._lock: |
| 1145 | try: |
| 1146 | returncode = self._zombies.pop(pid) |
| 1147 | except KeyError: |
| 1148 | # The child is running. |
| 1149 | self._callbacks[pid] = callback, args |
| 1150 | return |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1151 | |
Guido van Rossum | ab27a9f | 2014-01-25 16:32:17 -0800 | [diff] [blame] | 1152 | # The child is dead already. We can fire the callback. |
| 1153 | callback(pid, returncode, *args) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1154 | |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 1155 | def remove_child_handler(self, pid): |
| 1156 | try: |
| 1157 | del self._callbacks[pid] |
| 1158 | return True |
| 1159 | except KeyError: |
| 1160 | return False |
| 1161 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1162 | def _do_waitpid_all(self): |
| 1163 | # Because of signal coalescing, we must keep calling waitpid() as |
| 1164 | # long as we're able to reap a child. |
| 1165 | while True: |
| 1166 | try: |
| 1167 | pid, status = os.waitpid(-1, os.WNOHANG) |
| 1168 | except ChildProcessError: |
| 1169 | # No more child processes exist. |
| 1170 | return |
| 1171 | else: |
| 1172 | if pid == 0: |
| 1173 | # A child process is still alive. |
| 1174 | return |
| 1175 | |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 1176 | returncode = _compute_returncode(status) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1177 | |
Guido van Rossum | ab27a9f | 2014-01-25 16:32:17 -0800 | [diff] [blame] | 1178 | with self._lock: |
| 1179 | try: |
| 1180 | callback, args = self._callbacks.pop(pid) |
| 1181 | except KeyError: |
| 1182 | # unknown child |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1183 | if self._forks: |
| 1184 | # It may not be registered yet. |
| 1185 | self._zombies[pid] = returncode |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 1186 | if self._loop.get_debug(): |
| 1187 | logger.debug('unknown process %s exited ' |
| 1188 | 'with returncode %s', |
| 1189 | pid, returncode) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1190 | continue |
Guido van Rossum | ab27a9f | 2014-01-25 16:32:17 -0800 | [diff] [blame] | 1191 | callback = None |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 1192 | else: |
| 1193 | if self._loop.get_debug(): |
| 1194 | logger.debug('process %s exited with returncode %s', |
| 1195 | pid, returncode) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1196 | |
Guido van Rossum | ab27a9f | 2014-01-25 16:32:17 -0800 | [diff] [blame] | 1197 | if callback is None: |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1198 | logger.warning( |
| 1199 | "Caught subprocess termination from unknown pid: " |
| 1200 | "%d -> %d", pid, returncode) |
| 1201 | else: |
| 1202 | callback(pid, returncode, *args) |
| 1203 | |
| 1204 | |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 1205 | class MultiLoopChildWatcher(AbstractChildWatcher): |
| 1206 | """A watcher that doesn't require running loop in the main thread. |
| 1207 | |
| 1208 | This implementation registers a SIGCHLD signal handler on |
| 1209 | instantiation (which may conflict with other code that |
| 1210 | install own handler for this signal). |
| 1211 | |
| 1212 | The solution is safe but it has a significant overhead when |
| 1213 | handling a big number of processes (*O(n)* each time a |
| 1214 | SIGCHLD is received). |
| 1215 | """ |
| 1216 | |
| 1217 | # Implementation note: |
| 1218 | # The class keeps compatibility with AbstractChildWatcher ABC |
| 1219 | # To achieve this it has empty attach_loop() method |
| 1220 | # and doesn't accept explicit loop argument |
| 1221 | # for add_child_handler()/remove_child_handler() |
| 1222 | # but retrieves the current loop by get_running_loop() |
| 1223 | |
| 1224 | def __init__(self): |
| 1225 | self._callbacks = {} |
| 1226 | self._saved_sighandler = None |
| 1227 | |
| 1228 | def is_active(self): |
| 1229 | return self._saved_sighandler is not None |
| 1230 | |
| 1231 | def close(self): |
| 1232 | self._callbacks.clear() |
| 1233 | if self._saved_sighandler is not None: |
| 1234 | handler = signal.getsignal(signal.SIGCHLD) |
| 1235 | if handler != self._sig_chld: |
| 1236 | logger.warning("SIGCHLD handler was changed by outside code") |
| 1237 | else: |
| 1238 | signal.signal(signal.SIGCHLD, self._saved_sighandler) |
| 1239 | self._saved_sighandler = None |
| 1240 | |
| 1241 | def __enter__(self): |
| 1242 | return self |
| 1243 | |
| 1244 | def __exit__(self, exc_type, exc_val, exc_tb): |
| 1245 | pass |
| 1246 | |
| 1247 | def add_child_handler(self, pid, callback, *args): |
| 1248 | loop = events.get_running_loop() |
| 1249 | self._callbacks[pid] = (loop, callback, args) |
| 1250 | |
| 1251 | # Prevent a race condition in case the child is already terminated. |
| 1252 | self._do_waitpid(pid) |
| 1253 | |
| 1254 | def remove_child_handler(self, pid): |
| 1255 | try: |
| 1256 | del self._callbacks[pid] |
| 1257 | return True |
| 1258 | except KeyError: |
| 1259 | return False |
| 1260 | |
| 1261 | def attach_loop(self, loop): |
| 1262 | # Don't save the loop but initialize itself if called first time |
| 1263 | # The reason to do it here is that attach_loop() is called from |
| 1264 | # unix policy only for the main thread. |
| 1265 | # Main thread is required for subscription on SIGCHLD signal |
| 1266 | if self._saved_sighandler is None: |
| 1267 | self._saved_sighandler = signal.signal(signal.SIGCHLD, self._sig_chld) |
| 1268 | if self._saved_sighandler is None: |
| 1269 | logger.warning("Previous SIGCHLD handler was set by non-Python code, " |
| 1270 | "restore to default handler on watcher close.") |
| 1271 | self._saved_sighandler = signal.SIG_DFL |
| 1272 | |
| 1273 | # Set SA_RESTART to limit EINTR occurrences. |
| 1274 | signal.siginterrupt(signal.SIGCHLD, False) |
| 1275 | |
| 1276 | def _do_waitpid_all(self): |
| 1277 | for pid in list(self._callbacks): |
| 1278 | self._do_waitpid(pid) |
| 1279 | |
| 1280 | def _do_waitpid(self, expected_pid): |
| 1281 | assert expected_pid > 0 |
| 1282 | |
| 1283 | try: |
| 1284 | pid, status = os.waitpid(expected_pid, os.WNOHANG) |
| 1285 | except ChildProcessError: |
| 1286 | # The child process is already reaped |
| 1287 | # (may happen if waitpid() is called elsewhere). |
| 1288 | pid = expected_pid |
| 1289 | returncode = 255 |
| 1290 | logger.warning( |
| 1291 | "Unknown child process pid %d, will report returncode 255", |
| 1292 | pid) |
| 1293 | debug_log = False |
| 1294 | else: |
| 1295 | if pid == 0: |
| 1296 | # The child process is still alive. |
| 1297 | return |
| 1298 | |
| 1299 | returncode = _compute_returncode(status) |
| 1300 | debug_log = True |
| 1301 | try: |
| 1302 | loop, callback, args = self._callbacks.pop(pid) |
| 1303 | except KeyError: # pragma: no cover |
| 1304 | # May happen if .remove_child_handler() is called |
| 1305 | # after os.waitpid() returns. |
| 1306 | logger.warning("Child watcher got an unexpected pid: %r", |
| 1307 | pid, exc_info=True) |
| 1308 | else: |
| 1309 | if loop.is_closed(): |
| 1310 | logger.warning("Loop %r that handles pid %r is closed", loop, pid) |
| 1311 | else: |
| 1312 | if debug_log and loop.get_debug(): |
| 1313 | logger.debug('process %s exited with returncode %s', |
| 1314 | expected_pid, returncode) |
| 1315 | loop.call_soon_threadsafe(callback, pid, returncode, *args) |
| 1316 | |
| 1317 | def _sig_chld(self, signum, frame): |
| 1318 | try: |
| 1319 | self._do_waitpid_all() |
| 1320 | except (SystemExit, KeyboardInterrupt): |
| 1321 | raise |
| 1322 | except BaseException: |
| 1323 | logger.warning('Unknown exception in SIGCHLD handler', exc_info=True) |
| 1324 | |
| 1325 | |
| 1326 | class ThreadedChildWatcher(AbstractChildWatcher): |
| 1327 | """Threaded child watcher implementation. |
| 1328 | |
| 1329 | The watcher uses a thread per process |
| 1330 | for waiting for the process finish. |
| 1331 | |
| 1332 | It doesn't require subscription on POSIX signal |
| 1333 | but a thread creation is not free. |
| 1334 | |
Min ho Kim | 96e12d5 | 2019-07-22 06:12:33 +1000 | [diff] [blame] | 1335 | The watcher has O(1) complexity, its performance doesn't depend |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 1336 | on amount of spawn processes. |
| 1337 | """ |
| 1338 | |
| 1339 | def __init__(self): |
| 1340 | self._pid_counter = itertools.count(0) |
| 1341 | self._threads = {} |
| 1342 | |
| 1343 | def is_active(self): |
| 1344 | return True |
| 1345 | |
| 1346 | def close(self): |
Kyle Stanley | 0ca7cc7 | 2020-01-12 06:02:50 -0500 | [diff] [blame] | 1347 | self._join_threads() |
| 1348 | |
| 1349 | def _join_threads(self): |
| 1350 | """Internal: Join all non-daemon threads""" |
| 1351 | threads = [thread for thread in list(self._threads.values()) |
| 1352 | if thread.is_alive() and not thread.daemon] |
| 1353 | for thread in threads: |
| 1354 | thread.join() |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 1355 | |
| 1356 | def __enter__(self): |
| 1357 | return self |
| 1358 | |
| 1359 | def __exit__(self, exc_type, exc_val, exc_tb): |
| 1360 | pass |
| 1361 | |
| 1362 | def __del__(self, _warn=warnings.warn): |
| 1363 | threads = [thread for thread in list(self._threads.values()) |
| 1364 | if thread.is_alive()] |
| 1365 | if threads: |
| 1366 | _warn(f"{self.__class__} has registered but not finished child processes", |
| 1367 | ResourceWarning, |
| 1368 | source=self) |
| 1369 | |
| 1370 | def add_child_handler(self, pid, callback, *args): |
| 1371 | loop = events.get_running_loop() |
| 1372 | thread = threading.Thread(target=self._do_waitpid, |
| 1373 | name=f"waitpid-{next(self._pid_counter)}", |
| 1374 | args=(loop, pid, callback, args), |
| 1375 | daemon=True) |
| 1376 | self._threads[pid] = thread |
| 1377 | thread.start() |
| 1378 | |
| 1379 | def remove_child_handler(self, pid): |
| 1380 | # asyncio never calls remove_child_handler() !!! |
| 1381 | # The method is no-op but is implemented because |
| 1382 | # abstract base classe requires it |
| 1383 | return True |
| 1384 | |
| 1385 | def attach_loop(self, loop): |
| 1386 | pass |
| 1387 | |
| 1388 | def _do_waitpid(self, loop, expected_pid, callback, args): |
| 1389 | assert expected_pid > 0 |
| 1390 | |
| 1391 | try: |
| 1392 | pid, status = os.waitpid(expected_pid, 0) |
| 1393 | except ChildProcessError: |
| 1394 | # The child process is already reaped |
| 1395 | # (may happen if waitpid() is called elsewhere). |
| 1396 | pid = expected_pid |
| 1397 | returncode = 255 |
| 1398 | logger.warning( |
| 1399 | "Unknown child process pid %d, will report returncode 255", |
| 1400 | pid) |
| 1401 | else: |
| 1402 | returncode = _compute_returncode(status) |
| 1403 | if loop.get_debug(): |
| 1404 | logger.debug('process %s exited with returncode %s', |
| 1405 | expected_pid, returncode) |
| 1406 | |
| 1407 | if loop.is_closed(): |
| 1408 | logger.warning("Loop %r that handles pid %r is closed", loop, pid) |
| 1409 | else: |
| 1410 | loop.call_soon_threadsafe(callback, pid, returncode, *args) |
| 1411 | |
| 1412 | self._threads.pop(expected_pid) |
| 1413 | |
| 1414 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1415 | class _UnixDefaultEventLoopPolicy(events.BaseDefaultEventLoopPolicy): |
Victor Stinner | 70db9e4 | 2015-01-09 21:32:05 +0100 | [diff] [blame] | 1416 | """UNIX event loop policy with a watcher for child processes.""" |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1417 | _loop_factory = _UnixSelectorEventLoop |
| 1418 | |
| 1419 | def __init__(self): |
| 1420 | super().__init__() |
| 1421 | self._watcher = None |
| 1422 | |
| 1423 | def _init_watcher(self): |
| 1424 | with events._lock: |
| 1425 | if self._watcher is None: # pragma: no branch |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 1426 | self._watcher = ThreadedChildWatcher() |
Hill Ma | 99eb70a | 2019-12-05 04:40:12 -0800 | [diff] [blame] | 1427 | if threading.current_thread() is threading.main_thread(): |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 1428 | self._watcher.attach_loop(self._local._loop) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1429 | |
| 1430 | def set_event_loop(self, loop): |
| 1431 | """Set the event loop. |
| 1432 | |
| 1433 | 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] | 1434 | .set_event_loop() from the main thread will call .attach_loop(loop) on |
| 1435 | the child watcher. |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1436 | """ |
| 1437 | |
| 1438 | super().set_event_loop(loop) |
| 1439 | |
Andrew Svetlov | cc83920 | 2017-11-29 18:23:43 +0200 | [diff] [blame] | 1440 | if (self._watcher is not None and |
Hill Ma | 99eb70a | 2019-12-05 04:40:12 -0800 | [diff] [blame] | 1441 | threading.current_thread() is threading.main_thread()): |
Guido van Rossum | 2bcae70 | 2013-11-13 15:50:08 -0800 | [diff] [blame] | 1442 | self._watcher.attach_loop(loop) |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1443 | |
| 1444 | def get_child_watcher(self): |
Victor Stinner | f9e49dd | 2014-06-05 12:06:44 +0200 | [diff] [blame] | 1445 | """Get the watcher for child processes. |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1446 | |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 1447 | If not yet set, a ThreadedChildWatcher object is automatically created. |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1448 | """ |
| 1449 | if self._watcher is None: |
| 1450 | self._init_watcher() |
| 1451 | |
| 1452 | return self._watcher |
| 1453 | |
| 1454 | def set_child_watcher(self, watcher): |
Victor Stinner | f9e49dd | 2014-06-05 12:06:44 +0200 | [diff] [blame] | 1455 | """Set the watcher for child processes.""" |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1456 | |
| 1457 | assert watcher is None or isinstance(watcher, AbstractChildWatcher) |
| 1458 | |
| 1459 | if self._watcher is not None: |
| 1460 | self._watcher.close() |
| 1461 | |
| 1462 | self._watcher = watcher |
| 1463 | |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 1464 | |
Guido van Rossum | 0eaa5ac | 2013-11-04 15:50:46 -0800 | [diff] [blame] | 1465 | SelectorEventLoop = _UnixSelectorEventLoop |
| 1466 | DefaultEventLoopPolicy = _UnixDefaultEventLoopPolicy |