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
- Variables
- func AllGone(outs []Outcome) bool
- func Kill(pid int) error
- func SupportsDieWithWrapper() bool
- type Deps
- type Member
- type Options
- type Outcome
- type Plan
- type Tree
- func (t *Tree) Close() error
- func (t *Tree) Contained() bool
- func (t *Tree) DisarmKillOnClose() error
- func (t *Tree) KillsOnClose() bool
- func (t *Tree) Mechanism() string
- func (t *Tree) PID() int
- func (t *Tree) PIDs() ([]int, error)
- func (t *Tree) Terminate() error
- func (t *Tree) TerminateExcept(protect map[int]bool) ([]int, error)
- func (t *Tree) Warning() string
Constants ¶
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 ¶
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.
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 ¶
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 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 ¶
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 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.
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree is a started command and the containment around it.
func Start ¶
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 ¶
Close releases the wrapper's handle on the containment. The tree keeps running unless the tree was started with DieWithWrapper.
func (*Tree) Contained ¶
Contained reports whether the command runs inside a containment the OS can enumerate and stop.
func (*Tree) DisarmKillOnClose ¶
DisarmKillOnClose has nothing to clear on Unix: no handle ends the tree when it closes.
func (*Tree) KillsOnClose ¶
KillsOnClose is always false on Unix: closing a process group ends nothing.
func (*Tree) Mechanism ¶
Mechanism names what contains the tree: "job object", "process group" or "none".
func (*Tree) PIDs ¶
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 ¶
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 ¶
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.