procguard

package
v0.81.12 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 23 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 procguard ties long-lived child processes to the lifetime of the process that spawned them.

SpeechKit's CPU-heavy children — the wake-word sidecars, whisper-server and the local LLM server — are deliberately rooted in a background context so a short-lived HTTP request context cannot kill them (see app/cmd/speechkit/desktop_wakeword.go for that bug history). Their shutdown is owned by the host's own Close paths and the startup cleanup stack.

That covers every ORDERLY exit. It does not cover a crash, a taskkill, or a dev-loop rebuild that replaces the host binary: those skip the cleanup stack entirely and leave the children running forever. Each orphan holds on the order of a gigabyte of commit charge, so a day of restarts can exhaust the system commit limit while physical memory still looks healthy — at which point unrelated tools start failing to allocate (observed 2026-08-27: 6 orphans, ~7 GB, commit limit down to 0.4 GB free of 47.9 GB).

Adopt closes that gap by handing the child to the operating system: the OS terminates it when the parent goes away, however the parent goes away.

How each platform keeps that promise

Windows has a Job Object with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, which is a real parent-death guarantee: the kernel kills the children when the last job handle closes, and that happens even on an unhandled crash. Prepare is a no-op there and Sweep finds nothing, because nothing can be left.

macOS has no such primitive, so the guarantee is assembled from three weaker parts and is honest about being weaker: Prepare puts each child in its own process group before it starts, Shutdown kills those groups (SIGTERM, then SIGKILL) from the host's cleanup path and from a SIGTERM/SIGINT handler, and Sweep terminates whatever an earlier run still left behind. A kill -9 of the host defeats all three; the next start sweeps the remains.

Everywhere else Adopt and Sweep report ErrUnsupportedPlatform. They used to return nil, which told a caller that a child was tied to the host when nothing of the sort had happened.

The call order is Prepare before cmd.Start, Adopt straight after it. Both are robustness measures rather than preconditions: a child that could not be adopted still works, so callers log and carry on — AdoptOrReport is that call shape.

Index

Constants

This section is empty.

Variables

View Source
var ErrNetworkConfinementRequired = errors.New("data_profile_full_local: native runtime requires kernel-enforced child network confinement")

ErrNetworkConfinementRequired means the deployment must supply inherited child-process confinement. The Go dial guard, a loopback listen address, scrubbed environment and kill-on-exit custody cannot constrain native code.

View Source
var ErrUnsupportedPlatform = errors.New("procguard: child processes cannot be tied to this process on this platform")

ErrUnsupportedPlatform is returned by Adopt and Sweep on a platform with no mechanism to tie a child to this process.

It exists because the previous no-op returned nil: a caller could not tell "the child is now tied to us" from "nothing happened". On macOS that difference is a gigabyte of leftover whisper-server after every crash, so the honest answer has to be an error even though the caller only logs it.

Functions

func Adopt

func Adopt(cmd *exec.Cmd) error

Adopt reports that this platform has no parent-death mechanism.

func AdoptOrReport

func AdoptOrReport(cmd *exec.Cmd, report func(error))

AdoptOrReport always reports: this platform cannot adopt a child, and saying so is the honest answer (see Adopt).

func BundleHelpersDir

func BundleHelpersDir(exe string) string

BundleHelpersDir maps an executable path inside a macOS app bundle (…/SpeechKit.app/Contents/MacOS/SpeechKit) to the bundle's Contents/Helpers directory. It returns "" for a binary that does not live in a bundle, such as a `go build` output or a test binary. It is a pure path computation on slash-separated macOS paths and behaves the same on every platform.

func ConfigureHiddenProcess

func ConfigureHiddenProcess(cmd *exec.Cmd, lowered bool)

ConfigureHiddenProcess is a no-op outside Windows: POSIX hosts do not pop console windows for child processes, and priority classes are a Windows scheduling concept. It exists so callers need no build tags.

func ConfigureHiddenProcessRaised added in v0.80.18

func ConfigureHiddenProcessRaised(cmd *exec.Cmd)

ConfigureHiddenProcessRaised is a no-op outside Windows (see the Windows build for what it does there).

func ForwardOutput added in v0.78.4

func ForwardOutput(writer io.Writer) io.Writer

ForwardOutput makes exec create a pipe and forward through the parent under Full Local. A journal's socket descriptor must never be inherited by native code. Standard retains the original stream and lifecycle behavior.

func HostSidecarEnv

func HostSidecarEnv() []string

HostSidecarEnv is SidecarEnv applied to this process's environment and the names registered with SetSidecarEnvDrop. Assign it to exec.Cmd.Env before starting a local sidecar.

func Prepare

func Prepare(cmd *exec.Cmd)

Prepare retains the lifecycle API for existing consumers. Under Full Local a legacy Prepare + cmd.Start sequence is deliberately poisoned: only Start can atomically arm and launch. Ignoring a return value cannot bypass confinement.

func PrepareChildRead added in v0.78.4

func PrepareChildRead(path string) error

PrepareChildRead hands a newly-created private runtime identity file or directory to the confined child identity. Callers retain removal custody. Standard permissions and shared model assets are unchanged.

func RequireSpeechConfinement added in v0.78.4

func RequireSpeechConfinement() error

RequireSpeechConfinement checks inexpensive host prerequisites. It never attests a deployment: Start must arm and verify the kernel for every launch.

func Run added in v0.78.4

func Run(cmd *exec.Cmd) error

Run is Start followed by Wait, including short-lived health probes.

func SetSidecarEnvDrop

func SetSidecarEnvDrop(names []string)

SetSidecarEnvDrop registers further variable names that HostSidecarEnv drops, for credentials whose environment variable name is configurable and therefore matches no convention. It replaces the previous set.

func SetSubprocessPriorityLowered

func SetSubprocessPriorityLowered(lowered bool)

SetSubprocessPriorityLowered sets the process-wide default for whether background subprocesses (whisper-server, the local LLM server, wake-word sidecars) are started at BELOW_NORMAL priority. It defaults to true and only has an effect on Windows. Hosts wire their performance opt-out here once.

func Shutdown

func Shutdown()

Shutdown has nothing to terminate: Adopt never accepted a child.

func SidecarEnv

func SidecarEnv(environ []string, drop ...string) []string

SidecarEnv returns the entries of environ ("NAME=value") a local sidecar may inherit. It drops the credential and configuration families listed above and every name in drop; it keeps the operating-system basics (PATH, SystemRoot, TEMP/TMP, HOME/USERPROFILE, LANG and LC_*) and everything else, GPU runtime variables (CUDA_*, HIP_*, VULKAN_*, GGML_*, METAL_*) included. Names compare case-insensitively, as Windows does.

func Start added in v0.78.4

func Start(cmd *exec.Cmd) error

Start is the native launch boundary. Full Local never delegates a prepared command back to a caller for an unguarded Start. Standard retains Prepare's existing platform lifecycle behavior.

func SubprocessPriorityLowered

func SubprocessPriorityLowered() bool

SubprocessPriorityLowered reports the process-wide default set by SetSubprocessPriorityLowered.

func Sweep

func Sweep(string) (int, error)

Sweep reports that stale children cannot be identified on this platform. Answering "0 stale processes found" would be indistinguishable from a clean machine, which is the whole failure this package was fixed for.

Types

type Process

type Process struct {
	// PID is the process identifier.
	PID int
	// ExecutablePath is the absolute path of the running image. It is the
	// only field that can prove a process is ours, which is why the sweep
	// never looks at the process name.
	ExecutablePath string
}

Process is one running process, as the platform reports it to Sweep.

Jump to

Keyboard shortcuts

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