Skip to content

Run Runner.Worker in its own process group when there is no terminal - #4774

Open
thomhurst wants to merge 4 commits into
actions:mainfrom
thomhurst:fix/worker-own-process-group
Open

thomhurst wants to merge 4 commits into
actions:mainfrom
thomhurst:fix/worker-own-process-group

Conversation

@thomhurst

@thomhurst thomhurst commented Oct 12, 2026 •

Copy link
Copy Markdown

Fixes #4775. Also fixes actions/runner-images#14833. Related: dotnet/runtime#131944.

Problem

On hosted macOS runners, every job step shares one process group with the runner stack:

 1908  1002   967 runner /bin/bash -e /Users/runner/work/_temp/....sh
 1002   997   967 runner .../Runner.Worker spawnclient 143 146
  997   980   967 runner .../Runner.Listener run
  980   967   967 runner /opt/hca/hosted-compute-agent
  967     1   967 runner /bin/bash /opt/hca/start_hca.sh

That group is orphaned, because its leader's parent is launchd. If a member exits while the group has a stopped member, the kernel sends SIGHUP and SIGCONT to every process in the group. .NET's Process.Kill(entireProcessTree: true) SIGSTOPs each process before killing it, so a step that calls it can kill Runner.Listener and hosted-compute-agent. The job then ends with "The hosted runner lost communication with the server", step timeouts never fire and logs are lost. In our runs, 10 of 13 jobs lost the runner this way. Running the same repro in its own session and process group passed 3 of 3, with the runner still alive.

Captures (process tables, signal logs, spindump): https://github.com/thomhurst/ModularPipelines/releases/tag/diag-runtime-131944

Fix

After the job message arrives and before any step starts, Runner.Worker calls setpgid(0, 0) to move into its own process group. This only happens when the actions_runner_worker_own_process_group feature flag is true, so the change can be rolled out and turned off from the service side. Job steps inherit that group, so:

  • group signals caused by steps no longer reach Runner.Listener or whatever supervises it;
  • the worker's group is not orphaned, because its parent (Runner.Listener) is in the same session but a different group. Steps therefore don't get SIGHUP either. This is the layout a shell's job control creates, which is why the problem never reproduces from a terminal.

The worker leaves the process group as it is when:

  • it has a controlling terminal (/dev/tty opens), so interactive run.sh keeps today's Ctrl+C and job-control behaviour;
  • RUNNER_MANUALLY_TRAP_SIG is set, because run.sh then uses set -m (a group that is not orphaned) and forwards SIGINT to the whole group;
  • the terminal check fails for any reason other than a missing terminal (open(2) on /dev/tty fails with an errno other than ENXIO or ENOENT).

The change only applies outside Windows. Failures are best effort: they are logged and the job runs in the inherited group, as it does today.

Testing

  • Runner.Worker builds for osx-arm64, linux-x64 and win-x64 with 0 warnings.
  • New WorkerL0 tests cover the feature flag gate, the terminal and RUNNER_MANUALLY_TRAP_SIG bypasses, unexpected errno values, setpgid failure and exceptions from the native calls, using a mock of IProcessGroupNative. They pass on Windows and Linux.
  • In a Linux container, the /dev/tty probe returns ENXIO without a terminal and setpgid moves the process. Under a pty, the terminal is found and the group is left as it is.
  • Not yet run on a hosted macOS image, since hosted runners can't use a custom runner build. The isolation itself was checked on hosted macos-latest using setsid, as described above.

Job steps shared an orphaned process group with Runner.Listener and its
supervisor. A step that leaves a stopped process behind when a group member
exits (for example .NET's Process.Kill(entireProcessTree: true)) makes the
kernel send SIGHUP and SIGCONT to the whole group, killing the runner.

Fixes actions/runner-images#14833

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Terminal-probe failures are handled too broadly, and the process-wide behavior lacks a rollout feature flag.

2 open findings
What changed in this PR

Separates noninteractive Unix workers and job steps from the listener’s process group to prevent group signals from terminating the runner stack.

Changes:

  • Detects controlling terminals and manual signal trapping.
  • Moves eligible workers into a dedicated process group.
  • Logs process-group transition failures as best effort.
File Description
src/​Runner.Worker/​Program.cs Adds Unix process-group isolation during worker startup.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/Runner.Worker/Program.cs Outdated
Comment on lines +36 to +38
#if !OS_WINDOWS
MoveToOwnProcessGroup(trace);
#endif

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8b8c855. The process group move now happens in Worker.RunAsync, after the job message arrives and before any step launches, and only when the actions_runner_worker_own_process_group feature flag is true. It is off by default, so it can be rolled out and turned off from the service side.

Comment thread src/Runner.Worker/Program.cs Outdated
return;
}
}
catch (IOException)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8b8c855. /dev/tty is now probed with open(2) directly. Only ENXIO (no controlling terminal) and ENOENT (no /dev/tty) count as "no terminal". Any other errno keeps the current process group and logs the errno. I checked this in a Linux container: without a terminal, open returns ENXIO and setpgid moves the process; under a pty, the terminal is found and the group stays.

…robe

Move the process group change from Program.MainAsync into Worker.RunAsync,
after the job message arrives and before any step launches, so it can be
gated by the actions_runner_worker_own_process_group feature flag.

Probe /dev/tty with open(2) and treat only ENXIO and ENOENT as "no
terminal". Any other error keeps the current process group, instead of
treating every IOException as a missing terminal.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new OS-level behavior lacks automated coverage for its transition, bypass, and failure paths.

3 open findings

🧠 Review effort: Balanced

Comment thread src/Runner.Worker/Worker.cs Outdated
Comment on lines +243 to +245
private void MoveToOwnProcessGroup()
{
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("RUNNER_MANUALLY_TRAP_SIG")))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 94e92b8. The libc calls are now behind IProcessGroupNative, so Worker can be tested with a mock. New tests in WorkerL0 cover:

  • the feature flag gate (unset, false, true), including that the move happens before the job starts. On Windows the test checks that nothing moves even with the flag on.
  • the controlling terminal bypass, and RUNNER_MANUALLY_TRAP_SIG skipping the probe entirely.
  • ENXIO and ENOENT moving the process, and other errno values (EIO, EMFILE) leaving it in place.
  • setpgid failing, and the native calls throwing.

All 13 WorkerL0 tests pass on Windows and in a Linux container.

Move the libc calls behind IProcessGroupNative so Worker's logic can be
tested with a mock. The tests cover the feature flag gate (including that
the move happens before the job starts), the controlling terminal and
RUNNER_MANUALLY_TRAP_SIG bypasses, unexpected errno values from the
terminal probe, setpgid failure and exceptions from the native calls.
…ess-group

# Conflicts:
#	src/Runner.Common/Constants.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants