Run forkserver.
(listener_fd, alive_r, preload, main_path=None, sys_path=None)
| 167 | # |
| 168 | |
| 169 | def main(listener_fd, alive_r, preload, main_path=None, sys_path=None): |
| 170 | '''Run forkserver.''' |
| 171 | if preload: |
| 172 | if sys_path is not None: |
| 173 | sys.path[:] = sys_path |
| 174 | if '__main__' in preload and main_path is not None: |
| 175 | process.current_process()._inheriting = True |
| 176 | try: |
| 177 | spawn.import_main_path(main_path) |
| 178 | finally: |
| 179 | del process.current_process()._inheriting |
| 180 | for modname in preload: |
| 181 | try: |
| 182 | __import__(modname) |
| 183 | except ImportError: |
| 184 | pass |
| 185 | |
| 186 | # gh-135335: flush stdout/stderr in case any of the preloaded modules |
| 187 | # wrote to them, otherwise children might inherit buffered data |
| 188 | util._flush_std_streams() |
| 189 | |
| 190 | util._close_stdin() |
| 191 | |
| 192 | sig_r, sig_w = os.pipe() |
| 193 | os.set_blocking(sig_r, False) |
| 194 | os.set_blocking(sig_w, False) |
| 195 | |
| 196 | def sigchld_handler(*_unused): |
| 197 | # Dummy signal handler, doesn't do anything |
| 198 | pass |
| 199 | |
| 200 | handlers = { |
| 201 | # unblocking SIGCHLD allows the wakeup fd to notify our event loop |
| 202 | signal.SIGCHLD: sigchld_handler, |
| 203 | # protect the process from ^C |
| 204 | signal.SIGINT: signal.SIG_IGN, |
| 205 | } |
| 206 | old_handlers = {sig: signal.signal(sig, val) |
| 207 | for (sig, val) in handlers.items()} |
| 208 | |
| 209 | # calling os.write() in the Python signal handler is racy |
| 210 | signal.set_wakeup_fd(sig_w) |
| 211 | |
| 212 | # map child pids to client fds |
| 213 | pid_to_fd = {} |
| 214 | |
| 215 | with socket.socket(socket.AF_UNIX, fileno=listener_fd) as listener, \ |
| 216 | selectors.DefaultSelector() as selector: |
| 217 | _forkserver._forkserver_address = listener.getsockname() |
| 218 | |
| 219 | selector.register(listener, selectors.EVENT_READ) |
| 220 | selector.register(alive_r, selectors.EVENT_READ) |
| 221 | selector.register(sig_r, selectors.EVENT_READ) |
| 222 | |
| 223 | while True: |
| 224 | try: |
| 225 | while True: |
| 226 | rfds = [key.fileobj for (key, events) in selector.select()] |
nothing calls this directly
no test coverage detected