| # Python forkserver | |
| A per-build server that starts the Python interpreter and imports common modules | |
| once, then runs each Python build action in a `fork()`ed copy of itself. Each | |
| action still gets its own process; it just skips interpreter startup and | |
| the imports the server already did. | |
| Design doc: [go/fuchsia-python-forkserver](http://go/fuchsia-python-forkserver). | |
| * `server.py`: the forkserver's controller: warms up, starts the forkers, and | |
| merges their stats when it stops. | |
| * `forker.py`: a forker process: accepts requests and forks a worker for each. | |
| * `stats.py`: the counters, per forker and merged. | |
| * `worker.py`: warms the server up, and turns a forked worker into the process | |
| that runs the script. | |
| * `client.py`: the launcher. `python3 -S client.py SCRIPT ARGS` runs `SCRIPT` on | |
| the server when one is advertised, or execs `python3 -S SCRIPT ARGS` | |
| otherwise. | |
| * `forkserver_wrap.py`: runs a command with a server for it. | |
| * `setting.py`: reads the `//.fx/config/python_forkserver` opt-in, | |
| with the same rule as `python_forkserver.gni`. `fx build` uses it to decide | |
| whether to start a server. | |
| * `python_forkserver.gni`: reads the `//.fx/config/python_forkserver` opt-in | |
| for GN and exposes the `enable_python_forkserver` GN argument (defaulting to | |
| that setting, overridable per build directory). | |
| * `example/`: a toy build action, and how to run it on a server by hand. | |
| Linux only. | |
| ## In `fx build` | |
| Turn it on for a checkout, then build as usual: | |
| ```sh | |
| fx build-forkserver enable | |
| fx build | |
| ``` | |
| `fx build-forkserver enable` writes `FX_BUILD_INTERNAL_PYTHON_FORKSERVER=1` to | |
| `//.fx/config/python_forkserver` and regenerates the current build directory | |
| (`fx gen`). GN (`python_forkserver.gni`) and `fx build` (`setting.py`) both read | |
| that file, with the same rule: on if a line is exactly | |
| `FX_BUILD_INTERNAL_PYTHON_FORKSERVER=1`, off otherwise or without the file. | |
| When it's on, `top_build_wrap.py` runs the build under `forkserver_wrap.py`, | |
| which starts one server per build, sets `FX_PYTHON_FORKSERVER_SOCKET` for the | |
| build, and stops the server afterwards. The server's log and stats land under | |
| the build's log directory as `python_forkserver.log` and | |
| `python_forkserver.stats.json` (and are included in ResultStore post-build | |
| uploads and `main_build.py`'s `--output-metadata-json` when enabled). | |
| Individual build directories (including infra builds via fint `gn_args`) can | |
| also override the checkout setting directly with | |
| `enable_python_forkserver = true` (or `false`) in `args.gn`, which writes the | |
| value into `args.json` so both GN (`client.py` in Ninja commands) and | |
| `main_build.py` stay in sync. | |
| To turn it off, run `fx build-forkserver disable`, which empties the file. | |
| `fx build-forkserver status` shows the setting. Only builds with it on have `client.py` in their commands, so toggling | |
| it reruns the actions that use it. | |
| The command empties the file rather than deleting it, and regenerates, because | |
| Ninja regenerates at the start of the next build when the file changes, but not | |
| when it appears or disappears. Other build directories of the checkout pick up | |
| a change to an existing file at their next build; after the file is first | |
| created, run `fx --dir DIR gen` in them. | |
| `fx ninja` doesn't start a server. To run Ninja by hand with one, wrap it with | |
| `top_build_wrap.py`, as `fx build` does: | |
| `PYTHONPATH=$PWD prebuilt/third_party/python3/linux-x64/bin/python3 -S | |
| build/scripts/top_build_wrap.py --build-dir out/default --python-forkserver -- | |
| prebuilt/third_party/ninja/linux-x64/ninja -C out/default`, which puts the | |
| server's log under `out/_build_logs`. Or wrap it with | |
| `build/scripts/python_forkserver/forkserver_wrap.py --build-dir out/default | |
| --logdir /tmp/forkserver --` directly. | |
| ## Cheat sheet | |
| | Want to... | Do | | |
| |---|---| | |
| | Run a script on the server | `FX_PYTHON_FORKSERVER_SOCKET=NAME python3 -S client.py SCRIPT ARGS` | | |
| | Run it without the server | Unset `FX_PYTHON_FORKSERVER_SOCKET` (same command) | | |
| | Change parallelism | `server.py --forkers N` (default: `min(8, cpu_count)`) | | |
| | Get totals on exit | `server.py --stats FILE` (one JSON line) | | |
| | Stop the server | `SIGTERM` (or `SIGINT`) to the controller (`server.py`) | | |
| ## What runs where | |
| The server is a controller process (`server.py`) and `--forkers` forker | |
| processes (`forker.py`). The controller binds the socket, warms up, forks the | |
| forkers, and waits for `SIGTERM` (or `SIGHUP` or `SIGINT`). Each forker accepts | |
| connections and forks a worker per request, which runs `SCRIPT` (`worker.py`). | |
| On `SIGTERM`, the controller shuts the socket down, each forker waits for its | |
| workers and writes its stats to `<stats>.<pid>`, and the controller merges them | |
| into `<stats>` (`stats.py`). | |
| ``` | |
| server.py (controller), started by forkserver_wrap.py for each build | |
| bind → warm up → ready byte → fork N forkers → sigwait() until SIGTERM/HUP/INT | |
| └─ forker × N loop: accept → check peer uid → send READY → fork worker | |
| │ → send PID | |
| │ on SIGCHLD (via a wakeup fd): reap worker → send STATUS → close | |
| └─ worker (one per action) | |
| receive request + client's stdio fds | |
| → set argv/env/cwd/umask/sys.path as `python3 -S SCRIPT` would | |
| → run SCRIPT in-process → exit with its status | |
| client.py (one per action) | |
| connect → wait READY → send request + stdio fds → wait PID | |
| → forward SIGINT/SIGTERM/SIGHUP to the worker → wait STATUS | |
| → exit with the same status (or die from the same signal) | |
| ``` | |
| The client is a messenger: it sends `SCRIPT`, its arguments, the environment, | |
| working directory, umask, and stdio to the server, and nothing about its own | |
| interpreter. Served scripts run with the server's interpreter and startup flags | |
| (`-S -W error::SyntaxWarning` in the build), and `PYTHON*` startup variables | |
| such as `PYTHONHASHSEED` and `PYTHONUTF8` are read once, when the server starts, | |
| not from each request (except for those listed below). The server needs its own | |
| directory on `sys.path`, so don't start it with `-P` or `PYTHONSAFEPATH`. Each | |
| worker gives `SCRIPT` the module search path that `python3 -S SCRIPT` would | |
| compute from the request's `PYTHONPATH`, plus its environment, working | |
| directory, umask, stdio, bytecode settings (`PYTHONDONTWRITEBYTECODE`, | |
| `PYTHONPYCACHEPREFIX`), and a fresh `__main__`. `SIGINT`, `SIGTERM`, and | |
| `SIGHUP` sent to the client reach the worker's process group, and the client | |
| exits with the worker's status (or dies from the same signal). | |
| So in GN, only put `client.py` in `python_exe_src -S` commands, right after | |
| the interpreter flags. A served script gets the server's flags, including | |
| `-W error::SyntaxWarning`, even if its command line only has `-S`. | |
| When `FX_PYTHON_FORKSERVER_SOCKET` is unset or empty, the client warns once per | |
| directory (it creates `.python_forkserver_slow_path` there) and execs `SCRIPT` | |
| with the command line's own interpreter and flags. | |
| Everything else fails the action, with a message saying what to do (usually, | |
| how to turn the forkserver off): | |
| * the server isn't running, closes the connection, or doesn't answer within | |
| 2 s, | |
| * the client's stdin, stdout, or stderr is closed, or | |
| * the request comes from another working directory than the one the server | |
| warmed up in (for example, a nested build). | |
| The RBE wrappers in `//build/rbe` (C++ compile, C++ link, Rust, and prebuilt | |
| tool) are imported once at warm-up, and a worker runs them by calling | |
| `module.main(argv, env)`, the same call as their `__main__` blocks. A script | |
| can be preloaded this way when it has a `main(argv, env)` entry point and | |
| importing it has no side effects. | |
| ## Protocol | |
| One request per connection on an abstract `AF_UNIX` stream socket. The | |
| constants and `Request`, the request encoding, live in `client.py`, which | |
| `server.py` imports. | |
| Server-to-client messages are 5 bytes: a tag byte and an i32le value. | |
| 1. Server to client: `R` (value 0) once the connection is accepted from a | |
| client running as the same user. The client only sends its stdio fds after | |
| this, so a hung server's queue of unaccepted connections never holds pipe | |
| write ends open. | |
| 2. Client to server: a u32le length, then NUL-separated fields: the magic | |
| string, cwd, umask, argc, `argv[0..argc)`, and `KEY=VALUE` environment | |
| entries. File descriptors 0, 1, and 2 ride along as `SCM_RIGHTS` on the | |
| first `sendmsg()`. The server closes the connection on a malformed request. | |
| 3. Server to client: | |
| * `P`: the worker's pid, which is also its process group. | |
| * `S`: the worker's wait status. | |
| ## Testing | |
| The tests are Bazel host tests, the same way CQ runs them: | |
| ```sh | |
| fx bazel test --config=host //build/scripts/python_forkserver:all | |
| ``` | |
| To iterate on one test file without a build, run it the way the build does: | |
| with `python3 -S`, `PYTHONPATH` set to the checkout root, and no `PATH`. | |
| ```sh | |
| cd build/scripts/python_forkserver | |
| env -i HOME="$HOME" PYTHONPATH="$PWD/../../.." \ | |
| ../../../prebuilt/third_party/python3/linux-x64/bin/python3 -S client_test.py | |
| ``` | |
| Build actions may have no `PATH` and may set `PYTHONPYCACHEPREFIX`, so tests | |
| start helper processes with `sys.executable` rather than host tools like | |
| `sleep`, and don't assume where bytecode caches go. | |
| ## Debugging | |
| * Server log: whatever you redirect the server's stderr to. It prints one line | |
| at warm-up and a stats line on exit. | |
| * If one of the server's forker processes exits early (for example, on | |
| EMFILE), the server logs which one, stops, and exits 1; actions after that | |
| fail with "isn't running". | |
| * To compare against a plain run, unset `FX_PYTHON_FORKSERVER_SOCKET` and rerun | |
| the same command. |