proctree

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package proctree contains a launched process and everything it spawns, so a worker can wait for, signal and stop the whole tree rather than only its direct child. A task's process and an OpenJD environment action's process are both launched this way.

A Tree is the containment for one launched os/exec.Cmd. It is built in three steps: Configure before Start, then Supervise right after Start, then Tree.Release once the process has been dealt with.

On POSIX the tree is the process group Configure makes the command the leader of; there is nothing to create at Supervise and nothing to undo at Tree.Release. On Windows Configure starts the process suspended, Supervise assigns it to a fresh job object and only then resumes it, so every process the tree creates starts inside the job, and the job (not the parent-PID chain, which an orphaned grandchild escapes) is what Tree.Kill terminates and Tree.Alive counts.

Tree.Notify asks the tree to stop (SIGTERM, or a best-effort taskkill), Tree.Kill stops it for certain, and Resolve turns an OpenJD Action.cancelation method into the choice between the two.

Tree.Release on a normal exit does not kill: a background process still running in the tree when its command exits successfully is left running on both platforms. On Windows the job's kill-on-close flag, which stops the tree if the worker process itself exits mid-run, is cleared first.

On POSIX nothing outlives the worker to stop a tree when the worker itself is killed, so a launch site marks each tree's environment with a fresh token (MarkEnv) and records it in the worker's Ledger with Tree.Track, which reads the token back from the command; Tree.Release deletes the record. The next worker to start on the same data directory runs Ledger.Sweep before it registers, killing every process that still carries a recorded token. Where the kernel withholds a process's environment (macOS 27, for Apple's own binaries) such a process is matched by its process group, which the record makes trustworthy by also holding the group leader's start time. Windows records nothing: its job objects die with the worker.

The package imports only the standard library, golang.org/x/sys and internal/worker/protocol, so both internal/worker/executor and internal/worker/session can use it.

Index

Constants

View Source
const EnvVar = "SQI_PROCESS_TREE"

EnvVar is the environment variable that marks every process of a tree with the tree's token. Descendants inherit it, so a later Ledger.Sweep finds a tree's processes by it even after the process group's leader has exited.

View Source
const MaxNotifyPeriod = 600 * time.Second

MaxNotifyPeriod is the OpenJD maximum notify period; a larger notifyPeriodInSeconds is clamped down to this ceiling.

Variables

This section is empty.

Functions

func Configure

func Configure(cmd *exec.Cmd)

Configure places the command in its own process group so that the whole group (the task's process and every child it spawns) can be signaled together on timeout or cancellation. Without this, Kill/Notify reach only the direct child; a shell's grandchildren (for example a `sleep`) survive, keep the inherited stdout/stderr pipes open, and stall the caller's cmd.Wait() until they exit on their own.

func MarkEnv

func MarkEnv(env []string) []string

MarkEnv returns env with EnvVar set to a fresh tree token, replacing any inherited value (a worker started from another worker's task inherits that task's marker). A launch site assigns the result to the command's Env before Start, and Tree.Track reads the token back from it. A nil env means the current process's environment, as it does to os/exec, so marking it never empties the process's environment.

func Resolve

func Resolve(c *protocol.CancelationMethod, defaultPeriod time.Duration) (notify bool, period time.Duration)

Resolve turns an OpenJD Action.cancelation method into whether to notify the tree before killing it and, if so, how long to wait before the kill.

A nil method (no cancelation specified) yields (false, 0): TERMINATE, the OpenJD spec default, which kills immediately. Any mode other than NOTIFY_THEN_TERMINATE is likewise treated as TERMINATE and its period is ignored.

For NOTIFY_THEN_TERMINATE the period is NotifyPeriodSeconds when it is positive, otherwise defaultPeriod (the spec's default depends on the kind of action, which only the caller knows), clamped to MaxNotifyPeriod.

Types

type Label

type Label struct {
	// Kind is "task" or "environment-action".
	Kind      string `json:"kind"`
	AttemptID string `json:"attempt_id,omitempty"`
	TaskID    string `json:"task_id,omitempty"`
	SessionID string `json:"session_id,omitempty"`
	Command   string `json:"command,omitempty"`
}

Label says what a recorded tree was, for the sweep's log line.

type Ledger

type Ledger struct {
	// contains filtered or unexported fields
}

Ledger is the directory where a worker records every process tree it starts, so that the next worker to start on the same data directory can kill the trees a killed worker left running (see Ledger.Sweep). A nil *Ledger is valid and records nothing.

func OpenLedger

func OpenLedger(dir string) (*Ledger, error)

OpenLedger creates dir (owner-only) if needed and returns the ledger in it.

func (*Ledger) Sweep

func (l *Ledger) Sweep(ctx context.Context, logger *slog.Logger) error

Sweep kills every process a previous worker on this data directory left running: each process whose EnvVar carries a token one of the ledger's records names. It then deletes every record, unless ctx has ended. Run it before the worker registers, since the registration is what reclaims (and re-runs) the dead worker's tasks, and only while holding the data directory's lock, so that a live worker's trees are never swept. It waits at most 5 seconds. Only an unreadable ledger directory is returned as an error; everything else is logged. A nil ledger does nothing.

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

Tree is the POSIX counterpart of the Windows job object. Configure's Setpgid establishes containment before Start, so there is nothing to create here; the type gives callers one shape on both platforms.

func Supervise

func Supervise(cmd *exec.Cmd) (*Tree, error)

Supervise records cmd. The process group was configured before Start.

func (*Tree) Alive

func (t *Tree) Alive() bool

Alive reports whether any process of the task's process group is still running. A group whose processes belong to another account (run-as-user) answers EPERM, which still means it has members.

func (*Tree) Kill

func (t *Tree) Kill() error

Kill sends SIGKILL to the task's process group, forcibly terminating the task and every child it spawned. It returns an error if the signal could not be delivered.

func (*Tree) Notify

func (t *Tree) Notify() error

Notify sends SIGTERM to the task's process group, requesting graceful termination of the task and every child it spawned. It returns an error if the signal could not be delivered (for example, the process has already exited).

func (*Tree) Release

func (t *Tree) Release() error

Release removes the tree's ledger record and otherwise does nothing: processes still running in the task's process group when the task exits successfully are left running. internal/worker/staging's stage-out checks (openStageOutSource) allow for such a process. The Windows Release behaves the same.

func (*Tree) Stop

func (t *Tree) Stop(notify bool, period time.Duration) error

Stop ends every process of the tree by an OpenJD cancelation method, as Resolve reports it.

Without notify it kills the tree at once (TERMINATE). With notify (NOTIFY_THEN_TERMINATE) it notifies the tree and waits for it to exit for at most period, then kills whatever is left; if the notify cannot be delivered it kills at once rather than wait for an exit nothing asked for.

Kill is always called, so a process that outlived the tree's leader is ended too. A kill error is returned only when the tree is still alive afterwards: killing a tree that has already emptied fails on POSIX (the process group no longer exists), and is not an error.

func (*Tree) Track

func (t *Tree) Track(l *Ledger, label Label) error

Track records the tree in l under the token MarkEnv put in the command's environment, so that if this worker is killed the next one to start on the same data directory kills the tree (Ledger.Sweep). Release removes the record. A nil l records nothing. Track fails when the record cannot be written or the command's environment carries no token; the tree is then left running and unrecorded.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL