proctree

package
v0.199.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 3 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 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

func Alive(pid int) bool

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

func (t *Tree) Attach(cmd *exec.Cmd) error

Attach records the started process's pid — with Configure's Setpgid, that pid IS the group id.

func (*Tree) Configure

func (t *Tree) Configure(cmd *exec.Cmd)

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).

Jump to

Keyboard shortcuts

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