proctree

package
v0.209.1 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 7 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 and stops the process tree a command starts (plan P10).

WHY. A lease wrapper used to start its command with a bare exec and, when the lease was lost, kill the direct child only: a grandchild (a launcher's render server, a script's worker) kept running on a card another job was about to be handed. Nothing could enumerate what a wrapper had started either, so a wrapper that died took with it the only record of what held the card.

WHAT. Start runs a command inside a containment the OS can enumerate and stop as a whole:

  • Windows: a job object. The child is created suspended, assigned to the job and resumed, so it cannot spawn a process that escapes before it is inside; every descendant joins the job, the job lists them (PIDs) and TerminateJobObject stops them all. The job object code (Job) is the one the agent shell's cage (internal/sandbox) builds on.
  • Unix: the child leads its own session and process group, and the group is signalled as a whole (kill(-pgid)). A process that leaves the group is still found through its parent chain while that chain is alive. PR_SET_PDEATHSIG is not used: it is delivered per thread and covers the direct child only.

Letting go of the containment (Close, or the wrapper dying) leaves the tree running unless the caller opted in with DieWithWrapper: a wrapper that is a single point of failure for a twenty-hour render is worse than the orphan the lease then keeps as a tree-orphan.

This package never decides what to stop. Terminate stops everything the containment holds; PlanStop and Stop (stop.go) are the only path that stops a process the containment does not hold, and they stop nothing that is not named by a recorded tree or a launch marker.

Index

Constants

View Source
const (
	// StateStopped: the process was running as recorded and is gone now.
	StateStopped = "stopped"
	// StateGone: the process had already exited.
	StateGone = "already gone"
	// StateRecycled: the recorded process is gone and its pid names another one, left alone.
	StateRecycled = "pid recycled"
	// StateStillRunning: it was signalled and is still running.
	StateStillRunning = "still running"
	// StateFailed: it could not be signalled.
	StateFailed = "could not be stopped"
)

The states of one member after Stop.

Variables

View Source
var ErrDieWithWrapperUnsupported = errors.New("proctree: die-with-wrapper is not supported on this platform (there is no handle whose closing ends a whole process tree); the tree is left to the lease's tree-orphan rule")

ErrDieWithWrapperUnsupported is returned by Start where the platform cannot end a tree when its parent dies: a refusal, never a silent no-op, because the caller asked for a guarantee.

View Source
var ErrNotContained = errors.New("proctree: the command is not contained")

ErrNotContained is returned by PIDs and Terminate when the command runs without a containment (its Warning says why). The caller falls back to what it can enumerate itself.

Functions

func AllGone

func AllGone(outs []Outcome) bool

AllGone reports whether every member of a plan is verified gone (stopped, already gone, or its recorded process gone with the pid reused). A caller that frees a claim requires it.

func Kill

func Kill(pid int) error

Kill ends one process by pid; a process that is already gone is not an error.

func SupportsDieWithWrapper

func SupportsDieWithWrapper() bool

SupportsDieWithWrapper reports whether this platform can end a tree when its parent dies, so a caller can refuse the option before it does anything else (a wrapper that queued for hours for a card and then found it could not give the guarantee it was asked for has wasted the wait).

Types

type Deps

type Deps struct {
	Alive func(pid int) bool
	// Start reports a process's start identity; false when it cannot say.
	Start func(pid int) (int64, bool)
	Kill  func(pid int) error
	// Sleep pauses between the checks that a signalled process is gone.
	Sleep func(time.Duration)
	// Polls is how many times a signalled process is re-checked (spaced 500 ms) before it is
	// reported as still running.
	Polls int
}

Deps are the process seams. Alive, Start and Kill are required; the lease package's PIDAlive and ProcessStart are the identity the records were written with, and Kill is this package's.

type Member

type Member struct {
	PID     int
	StartMs int64
}

Member identifies one process: its pid and its start identity (the platform's own opaque value, compared only for equality; 0 = unknown, held by pid alone).

type Options

type Options struct {
	// DieWithWrapper ends the tree when the wrapper's last handle on the containment closes
	// (Windows: KILL_ON_JOB_CLOSE; the wrapper exiting or dying closes it). Off by default.
	// Platforms that cannot honour it refuse it with ErrDieWithWrapperUnsupported.
	DieWithWrapper bool
}

Options configures Start.

type Outcome

type Outcome struct {
	PID   int
	State string
	Why   string
}

Outcome is what happened to one member of a plan.

func Stop

func Stop(p Plan, d Deps) []Outcome

Stop stops every member of the plan and reports one Outcome per member. Members of NotOurs are never signalled.

type Plan

type Plan struct {
	// Stop are the processes of the recorded tree and of the launch markers, each once.
	Stop []Member
	// NotOurs are pids the driver shows on the card that are in neither list.
	NotOurs []int
}

Plan is what may be stopped and what must be left alone.

func PlanStop

func PlanStop(recorded, markers []Member, onCard []int) Plan

PlanStop decides what a stop may touch. recorded is the lease's recorded tree, markers the processes harness launch markers name, onCard the pids the driver lists on the lease's cards.

func (Plan) Report

func (p Plan) Report() []string

Report is one line per process on the card that is not ours.

type Tree

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

Tree is a started command and the containment around it.

func Start

func Start(cmd *exec.Cmd, opts Options) (*Tree, error)

Start starts cmd inside a containment and returns its handle. It fails when the command cannot be started, or when the options cannot be honoured. When the OS refuses the containment itself (a job that cannot be nested, a handle that cannot be opened) the command still runs, uncontained, and Warning says why: a wrapper must not fail the job it wraps over a missing safety net, and it must not hide that the net is missing.

func (*Tree) Close

func (t *Tree) Close() error

Close releases the wrapper's handle on the containment. The tree keeps running unless the tree was started with DieWithWrapper.

func (*Tree) Contained

func (t *Tree) Contained() bool

Contained reports whether the command runs inside a containment the OS can enumerate and stop.

func (*Tree) DisarmKillOnClose

func (t *Tree) DisarmKillOnClose() error

DisarmKillOnClose has nothing to clear on Unix: no handle ends the tree when it closes.

func (*Tree) KillsOnClose

func (t *Tree) KillsOnClose() bool

KillsOnClose is always false on Unix: closing a process group ends nothing.

func (*Tree) Mechanism

func (t *Tree) Mechanism() string

Mechanism names what contains the tree: "job object", "process group" or "none".

func (*Tree) PID

func (t *Tree) PID() int

PID is the command's own pid.

func (*Tree) PIDs

func (t *Tree) PIDs() ([]int, error)

PIDs lists the live members of the tree the containment knows of: the command and every descendant (Windows: the job's processes; Unix: the group's processes and the descendants reachable through live parent links). Order is not meaningful.

func (*Tree) Terminate

func (t *Tree) Terminate() error

Terminate stops every member of the tree at once. Members that already exited are not an error. It is the wrapper stopping its OWN tree (the lease was lost, the operator interrupted it); stopping anything the containment does not hold is PlanStop's.

func (*Tree) TerminateExcept

func (t *Tree) TerminateExcept(protect map[int]bool) ([]int, error)

TerminateExcept stops every member of the tree that is not in protect and returns the pids it left running. A process another lease took over (a kept ComfyUI instance it re-stamped to itself) must survive the wrapper that lost its lease; protect names those, descendants included. With nothing to protect it is Terminate.

func (*Tree) Warning

func (t *Tree) Warning() string

Warning says why the command runs uncontained ("" when it is contained).

Jump to

Keyboard shortcuts

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