Cotton code
72 lines · 3.2 KB · ShellRaw
# The sccache server, probed without starting one and started in a session of# its own. Sourced by scripts/ci/setup-sccache.sh and scripts/ci/sccache-up.sh.## WHY A SESSION OF ITS OWN. The server is spawned by whichever sccache client# finds none listening — `--start-server`, `--show-stats`, or a rustc call# through the shim — and it stays in THAT client's process group after the# client exits. A group kill meant for the client's job then takes the server# with it: a build lock's yield, a deadline's stop, a runner reaping a finished# job, a shell tool ending its command. Run 5645's mac job on one Mac (11 Oct 2026)# lost it two minutes into the release core — "The server looks like it shut# down unexpectedly, compiling locally instead" — and compiled 19m33s uncached.# Started from a fresh session, no one else's group holds it.## WHY THE PROBE IS NOT `sccache --show-stats`. Every sccache client command# starts a server when none is listening — in the caller's group, the very# thing this avoids. The probe connects to the server's socket and nothing else.# Whether a server is listening: the Unix socket when SCCACHE_SERVER_UDS names# one (the Linux host), else sccache's TCP port on this machine.sccache_listening() {  python3 - <<'PY'import os, socket, sysuds = os.environ.get("SCCACHE_SERVER_UDS")try:    if uds:        s = socket.socket(socket.AF_UNIX)        s.settimeout(2)        s.connect(uds)    else:        s = socket.create_connection(("127.0.0.1", int(os.environ.get("SCCACHE_SERVER_PORT", "4226"))), timeout=2)    s.close()except OSError:    sys.exit(1)PY}# Runs a command in a new session and waits for it: what it starts and leaves# behind belongs to no caller's process group. Its stdio is /dev/null, so a# server it leaves behind holds no step's log pipe open.# Stop this user's sccache server on this port when the temp folder it was# started with is gone: every compile it serves then fails "Failed to create# temp dir" (rust-main 15232, android 15230: a server started inside a job whose# own temp folder the job removed). The next start gives it the box's /tmp.sccache_heal_tmp() {  local bin="${1:-sccache}" pid dir  for pid in $(pgrep -u "$(id -u)" -x sccache 2>/dev/null || true); do    # A server this user may not read (another runner unit's) is skipped, never an error.    [ -r "/proc/$pid/environ" ] || continue    dir="$({ tr '\0' '\n' < "/proc/$pid/environ"; } 2>/dev/null | sed -n 's/^TMPDIR=//p' | head -1)" || dir=""    [ -n "$dir" ] && [ ! -d "$dir" ] || continue    echo "sccache: server $pid was started with TMPDIR $dir, which is gone: stopping it"    kill "$pid" 2>/dev/null || true  done  return 0}sccache_alone() {  python3 -c 'import os, syspid = os.fork()if pid == 0:    os.setsid()    # The server outlives this job, and the temp folder of the job (scripts/ci/job-tmp.sh)    # goes at its last step: the server keeps the box /tmp instead.    if os.environ.get("CI_JOB_TMP"):        for name in ("TMPDIR", "TMP", "TEMP", "CI_JOB_TMP"):            os.environ.pop(name, None)    os.execvp(sys.argv[1], sys.argv[1:])_, status = os.waitpid(pid, 0)sys.exit(os.waitstatus_to_exitcode(status))' "$@" </dev/null >/dev/null 2>&1}