Kubernetes executor (attach strategy): stage script intermittently missing from emptyDir, hanging job to timeout

What we encountered

On the Kubernetes executor with the default attach strategy, jobs intermittently fail with:

/bin/bash: /scripts-<project>-<jobid>/<stage>: No such file or directory

Seen on step_script, after_script, and predefined stages like restore_cache. When it happens the shell has nothing to execute and the job hangs until its timeout, surfacing as job_execution_timeout.

Observed behavior / data

  • Intermittent and strongly load-correlated. Timeout rate tracks job throughput: roughly ~0.03% overnight rising to ~5% at the daily peak (a 6–10× swing). The same job/image/config succeeds on retry.

  • Scales with pod density / churn. Over a 3-day window the timeout rate rose with how many job pods are packed per node:

    • high-concurrency Kubernetes runners (many job pods per node): ~1.3–1.5%
    • low-concurrency Kubernetes runners (few job pods per node): ~0.13% (~10× lower)
    • a non-Kubernetes executor that does not use a shared-container script mount: ~0.01% (~100× lower)

    The ordering — dense k8s ≫ sparse k8s ≫ executor-without-the-shared-mount — points at the attach strategy's shared-emptyDir script delivery under concurrency, not the jobs themselves.

  • The script is not actually lost. When a hung pod is inspected, the referenced script is present, complete size, executable, and runs fine on manual retry — it is missing only at the instant the shell opens it.

  • No project or node pattern — occurs fleet-wide across unrelated projects and nodes.

  • Two distinct failure signatures. This hang is job_execution_timeout. A separate runner_system_failure (job pod couldn't start under load) is a different problem addressed by adding capacity — adding capacity does not reduce these timeouts.

Where it comes from

In runWithAttach (executors/kubernetes/kubernetes.go) each stage is handled over two separate exec/attach sessions: saveScriptOnEmptyDir() writes the stage script to the shared scripts emptyDir, then a separately-attached shell reads it back by path and execs it. The write (shells/bash.go generateSaveScript) is touch; chmod; echo > path — non-atomic. Under load the reading session can observe the file missing/partial — consistent with a write/visibility race across the two sessions, not a lost file.

FF_USE_LEGACY_KUBERNETES_EXECUTION_STRATEGY=true avoids it (legacy pipes the script via exec stdin, never touching the emptyDir) but is deprecated.

Related: !6941 (merged) makes a missing build-container script fail fast rather than hang, but does not prevent the script from going missing (and does not cover the predefined/helper-container path).

Environment

  • gitlab-runner 19.0.2, Kubernetes executor, attach strategy (default)
  • Linux, bash job images