Help us shape the future of Go! Take the 2026 Go Developer Survey

proc

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package proc spawns and reaps one child process with explicit pipes, group control, and a single Wait.

It is the only lifecycle mechanism for harness children. exec.CommandContext is deliberately not used: a canceled context must not orphan a child in the middle of a protocol write, and a vendor child needs a flush window before it is signalled.

Index

Constants

View Source
const GracefulCloseTimeout = 5 * time.Second

GracefulCloseTimeout is the default flush window between close-stdin, SIGTERM, and SIGKILL. The vendor SDK comment is load-bearing: SIGTERM too early can truncate the vendor session file and lose the last assistant message.

Variables

This section is empty.

Functions

func Alive

func Alive(pid int) bool

Alive reports whether pid exists.

func ChildEnv

func ChildEnv(overrides map[string]string) []string

ChildEnv merges the process environment with overrides. Override keys win; an empty or reserved key is ignored. The result is in "K=V" form.

func FirstLine

func FirstLine(s string) string

FirstLine returns the first line of s, trimmed.

func KillGroup

func KillGroup(pgid, pid int)

KillGroup SIGKILLs the process group, then the process. It works on a process this library did not spawn, which is what ledger reconcile needs.

func StartToken

func StartToken(pid int) string

StartToken returns an identity token for a live pid: its start time. A mismatched token means the pid was reused and is not ours.

func TermSignal

func TermSignal() os.Signal

TermSignal is the graceful stop request this platform can deliver to a process group.

Types

type Opts

type Opts struct {
	// Path is the executable. It must be a native binary on Windows; .cmd and
	// .bat shims are refused.
	Path string
	Args []string
	Dir  string
	// Env is the full child environment in "K=V" form. Nil inherits the
	// parent environment.
	Env []string
	// OnStderr receives each stderr line as it is read. It is optional and
	// runs on the drain goroutine, so it must not block.
	OnStderr func(string)
}

Opts describes one child to spawn.

type Proc

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

Proc is a spawned child.

func Start

func Start(ctx context.Context, opts Opts) (*Proc, error)

Start spawns a child. The returned Proc has a bounded stderr tail reader attached; stdout is the caller's to frame.

func (*Proc) Argv

func (p *Proc) Argv() []string

Argv is the full command line of the child.

func (*Proc) CloseStdin

func (p *Proc) CloseStdin() error

CloseStdin closes the write side of the child's stdin once.

func (*Proc) Done

func (p *Proc) Done() <-chan struct{}

Done is closed when the child exits.

func (*Proc) ExitCode

func (p *Proc) ExitCode() int

ExitCode returns the code once Wait has completed (-1 before).

func (*Proc) GracefulClose

func (p *Proc) GracefulClose(timeout time.Duration) error

GracefulClose closes stdin, waits for the child to flush, then escalates SIGTERM and SIGKILL on the process group. timeout is per stage (default GracefulCloseTimeout).

func (*Proc) KillTree

func (p *Proc) KillTree() error

KillTree hard-kills the process tree and waits for the child.

func (*Proc) PID

func (p *Proc) PID() int

PID is the child process id.

func (*Proc) Pgid

func (p *Proc) Pgid() int

Pgid is the process group id (equal to PID on platforms without groups).

func (*Proc) SignalGroup

func (p *Proc) SignalGroup(sig os.Signal)

SignalGroup delivers sig to the whole process group when possible, else to the process.

func (*Proc) Stderr

func (p *Proc) Stderr() string

Stderr returns the accumulated stderr tail.

func (*Proc) Stdin

func (p *Proc) Stdin() io.Writer

Stdin is the child's stdin writer.

func (*Proc) Stdout

func (p *Proc) Stdout() io.Reader

Stdout is the child's stdout reader.

func (*Proc) Wait

func (p *Proc) Wait(ctx context.Context) (int, error)

Wait blocks until the child exits or ctx ends. It returns the exit code (-1 when ctx ended first) and the wait error.

func (*Proc) WaitTimeout

func (p *Proc) WaitTimeout(d time.Duration) bool

WaitTimeout waits up to d for the child to exit. ok=false means still alive.

Jump to

Keyboard shortcuts

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