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.
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.
fx buildTurn it on for a checkout, then build as usual:
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.
| 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) |
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 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.
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.
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.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.P: the worker's pid, which is also its process group.S: the worker's wait status.The tests are Bazel host tests, the same way CQ runs them:
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.
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.
FX_PYTHON_FORKSERVER_SOCKET and rerun the same command.