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 ¶
- Variables
- func Adopt(cmd *exec.Cmd) error
- func AdoptOrReport(cmd *exec.Cmd, report func(error))
- func BundleHelpersDir(exe string) string
- func ConfigureHiddenProcess(cmd *exec.Cmd, lowered bool)
- func ConfigureHiddenProcessRaised(cmd *exec.Cmd)
- func ForwardOutput(writer io.Writer) io.Writer
- func HostSidecarEnv() []string
- func Prepare(cmd *exec.Cmd)
- func PrepareChildRead(path string) error
- func RequireSpeechConfinement() error
- func Run(cmd *exec.Cmd) error
- func SetSidecarEnvDrop(names []string)
- func SetSubprocessPriorityLowered(lowered bool)
- func Shutdown()
- func SidecarEnv(environ []string, drop ...string) []string
- func Start(cmd *exec.Cmd) error
- func SubprocessPriorityLowered() bool
- func Sweep(string) (int, error)
- type Process
Constants ¶
This section is empty.
Variables ¶
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.
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 AdoptOrReport ¶
AdoptOrReport always reports: this platform cannot adopt a child, and saying so is the honest answer (see Adopt).
func BundleHelpersDir ¶
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 ¶
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
ConfigureHiddenProcessRaised is a no-op outside Windows (see the Windows build for what it does there).
func ForwardOutput ¶ added in v0.78.4
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 ¶
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
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 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 SidecarEnv ¶
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
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.
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.