Documentation
¶
Overview ¶
Package proctree holds one spawned process and the descendants it spawns, so a caller can kill the WHOLE tree instead of only the direct child.
Why it exists (ACP-02 real-machine finding F12): a job's agent process is often a launcher (npx → node → the adapter itself). Killing only the direct child leaves the grandchildren ALIVE — and typically still holding the job's stdout/stderr pipes — so os/exec's Wait never returns (its pipe-copy goroutine waits for an EOF that cannot come) and the job never reaches a terminal state: a cancelled, long-timed-out ACP job stayed `running` and kept its working-directory lock until the tree was killed by hand.
Platform containment:
- Windows: the process is put in a JOB OBJECT created with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE (CreateJobObject + AssignProcessToJobObject), so every descendant joins it unless it explicitly breaks away, and TerminateJobObject / closing the handle ends the whole tree in one call;
- unix: the process becomes its own process GROUP leader (Setpgid) and the group is signalled as a whole (kill(-pgid, SIGKILL)).
Usage is always the same three steps:
tree := proctree.New() tree.Configure(cmd) // BEFORE cmd.Start ... cmd.Start() ... _ = tree.Attach(cmd) // AFTER cmd.Start defer tree.Release() ... tree.Kill() ... // whenever the whole tree must die
Every method tolerates a nil receiver and a failed Attach, so a caller that could not build the containment still runs — degraded to the direct-child-only behaviour it had before. This package is a PRIMITIVE, not a policy: the caller decides whether ending the containment may take survivors with it (see Release).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Alive ¶
Alive reports whether a process with pid exists. Signal 0 performs error checking without delivering a signal: nil → alive; EPERM → exists but owned by another user (still alive); ESRCH → no such process. A zombie counts as alive (its pid is not reusable yet), which is what a test wants when it asks "has it been reaped/killed".
Types ¶
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree contains one process and its descendants through a process group.
func New ¶
func New() *Tree
New returns an empty containment; Attach records the started process's pid.
func (*Tree) Attach ¶
Attach records the started process's pid — with Configure's Setpgid, that pid IS the group id.
func (*Tree) Configure ¶
Configure makes the process its own process-group leader BEFORE it starts, so every descendant it spawns is in that group (children inherit the group unless they change it) and one signal can reach all of them.
func (*Tree) Kill ¶
func (t *Tree) Kill()
Kill sends SIGKILL to the whole process group. Idempotent: an already-empty group yields ESRCH, which is ignored.
func (*Tree) Release ¶
func (t *Tree) Release()
Release is a no-op on unix: unlike a Windows job handle, a process group has nothing to close, and signalling a group whose leader has already been REAPED is not safe (the leader's pid could have been recycled and would then be signalled as an unrelated group). Terminating survivors is therefore Kill's explicit job, and a caller that wants them gone calls it (Windows gets that cleanup for free through KILL_ON_JOB_CLOSE).