Skip to content

Stop the whole process tree of a direct-mode job - #569

Draft
nkeilbart wants to merge 2 commits into
mainfrom
feat/process-tree-termination
Draft

nkeilbart wants to merge 2 commits into
mainfrom
feat/process-tree-termination

Conversation

@nkeilbart

@nkeilbart nkeilbart commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This is the first of four stacked PRs that make direct mode usable as a local execution backend for
an external engine that submits, polls and cancels jobs one at a time. This PR changes the runner
only: no API, database or Slurm change. The later PRs are listed at the end.

Before this PR, a direct-mode job was stopped by a signal to its shell only:

  • Processes that the job started (MPI ranks, a pipeline, a background child) could survive a cancel,
    a timeout, an OOM kill or a runner shutdown.
  • SIGINT or SIGHUP killed the runner and left its jobs running, with status running on the server.
  • SIGTERM stopped the jobs, but the runner then waited the full sigterm_lead_seconds even when all
    jobs had already exited, and retried forever when the server was unreachable.

What changes

Each job runs in a session of its own. The job is started with setsid and stdin from
/dev/null. Its process group ID and session ID equal the job's PID, so the runner can signal the
group as a whole. The job has no controlling terminal, so a job that reads the terminal gets an
error instead of being stopped by the kernel.

A stop reaches the whole tree. AsyncCliCommand::request_stop(reason, signal, grace) sends the
signal to the process group, waits for the grace period, then sends SIGKILL. All stop paths use it:
cancel, runtime limit, end of the allocation, OOM (SIGKILL at once) and runner shutdown.

Descendants outside the group. A launcher can put its children into groups of their own. After
the group is signaled, the runner lists the processes of the host once per pass (one listing for all
jobs), records every descendant outside the group with its start time, and sends it the same signal.
A descendant is a member of the job's session or a process reached from the job's shell by parent
PID.

A job is complete only when its tree is gone. That means: the shell is reaped, the group is
empty (or holds only zombies) and the recorded descendants are gone. Processes that are left after
the shell exits get sigterm_lead_seconds, then the termination signal, then SIGKILL. So a job that
ends with nohup worker & no longer leaves worker behind.

Signals to the runner.

  • SIGINT and SIGHUP now stop the jobs like SIGTERM does. After cleanup the runner dies by the same
    signal, so a script torc run ...; next_step stops on Ctrl-C. SIGTERM still exits 0.
  • A SIGINT or SIGHUP that was ignored at start (nohup torc run) stays ignored.
  • The runner exits as soon as all trees are gone instead of waiting the full grace period.
  • When the server is unreachable during a stop, the runner stops its jobs with the usual grace and
    exits. In Slurm mode the retry loop is unchanged.
  • An exiting runner gives up on a tree that survives SIGKILL after 30 s and leaves that job
    running.

Standalone mode. torc -s run starts its server in a process group of its own. Before, Ctrl-C
in the terminal killed the server before the runner could record the results.

Behavior changes to review

Case Before After
Ctrl-C / SIGHUP on torc run Runner dies, jobs keep running, status running Jobs stopped and recorded as terminated, runner dies by the signal
SIGTERM on torc run, jobs exit fast Waits sigterm_lead_seconds Exits when the trees are gone
SIGTERM, server unreachable Retries, does not exit Stops jobs, exits
Job's shell exits, child still running Job complete, child survives Job complete after the child is gone or stopped
Stopped job exits 0, declared output file missing completed failed (goes through output-file validation)
Job opens /dev/tty or runs bash -i Could be stopped by the kernel and hang Open fails, job goes on

Measurements (Linux x86_64, 8 cores, debug build)

  • Stop of 200 running jobs with SIGTERM to the runner: the runner exits in about 0.8 s (main: 20
    s, the full sigterm_lead_seconds).
  • Delay from SIGTERM to the runner until a job gets its signal, 200 jobs: first 0.00 s, median 0.07
    s, last 0.13 s. main: first 0.00 s, median 0.05 to 0.08 s, last 0.11 to 0.12 s.
  • Job start: setsid needs pre_exec, so the standard library uses fork instead of posix_spawn.
    That costs about 1 ms per job start (1000 short jobs: 4.4 s, main: 3.3 s).

Limits

  • A process that started a session of its own (setsid) is found only if its parent is still
    running right after the signal, as a launcher that shuts its children down is. A daemon whose
    parent exited is not found and keeps running. Listing the processes before the signal would find
    more of these, but it delays the signal to every job by the time of the listing (about 0.2 s with
    200 jobs).
  • A runner that is killed with SIGKILL still orphans its jobs.
  • Windows stops only the job's own process, as before.
  • CPU counts are not enforced; this PR does not change resource accounting.

Tests

  • tests/test_async_cli_command.rs: stop of a tree with children, grace then SIGKILL, a child in a
    process group of its own, a child in a session of its own, leftovers after the shell exits, a
    shared process table, session and stdin of the job.
  • tests/test_direct_mode_e2e.rs: SIGINT, SIGHUP and SIGTERM to the runner (exit status and job
    status), unreachable server, ignored SIGINT, a stopped job without its output file, a job that
    uses the terminal.
  • tests/test_standalone.rs: Ctrl-C on torc -s run records the jobs.
  • cargo fmt -- --check, cargo clippy --all --all-targets --all-features -- -D warnings and
    dprint check pass.
  • cargo nextest run --all-features --no-fail-fast on the branch merged with main (7811e80b):
    2020 of 2021 passed. The one failure, test_get_active_task_returns_running_task in
    tests/test_tasks.rs, also fails now and then on main: it races an async initialize_jobs task
    against a second request.

Not verified

  • macOS and Windows: not built or run here. The code is gated with #[cfg(unix)] and has non-Unix
    fallbacks, but only CI will show whether Windows builds. There is no macOS CI job.
  • Real MPI: test_request_stop_terminates_mpi_ranks is ignored by default and needs mpirun. Run
    it with
    cargo nextest run --all-features --run-ignored only -E 'test(test_request_stop_terminates_mpi_ranks)'.

Docs

  • docs/src/core/concepts/execution-modes.md, job-runners.md, job-states.md and
    docs/src/core/how-to/cancel-workflow.md describe the new behavior and the limits.
  • New design page docs/src/specialized/design/process-tree-termination.md.

Follow-up PRs

This PR is draft until the design is agreed. Three more PRs finish the work, each based on the one
before:

  1. Per-job cancel. An endpoint to cancel one job. The runner learns of it through the
    is_workflow_complete poll, stops the job through the stop path of this PR and reports
    canceled.
  2. Idempotent job submission. Submit one job to a running workflow with an external_id (a
    repeated submit returns the same job) and a working_dir.
  3. Admission and diagnostics. Opt-in resource enforcement at claim time, an opt-in exclusive
    runner (torc run --exclusive), a termination_reason on results and an opt-in per-job runtime
    limit.

A direct-mode job was stopped by a signal to its shell only. Processes
that the job started could survive a cancel, a timeout, an OOM kill or
Ctrl-C, and SIGINT or SIGHUP killed the runner and left its jobs
running.

- Start each job in a session of its own, with stdin from /dev/null. The
  job's process group is signaled as a whole. A job that uses the
  terminal can no longer be stopped by the kernel.
- A stop signals the process group first, then lists the processes of
  the host once per pass and signals the descendants outside the group.
  The delay until the signal is the same as before.
- A job is complete when its shell is reaped, its group is empty and the
  recorded descendants are gone. Processes left after the shell exits
  get sigterm_lead_seconds, then the termination signal, then SIGKILL.
- SIGINT and SIGHUP stop the jobs like SIGTERM. After cleanup the runner
  dies by the same signal, so a calling script stops. An ignored SIGINT
  stays ignored.
- When the server is unreachable during a stop, the runner stops its
  jobs with the usual grace and exits instead of retrying.
- A stopped job goes through output-file validation.
- torc -s run starts the standalone server in a process group of its
  own, so Ctrl-C does not kill it before results are recorded.

Slurm mode and Windows are unchanged. Windows stops only the job's own
process.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant