winjob

package
v0.71.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Rendered for windows/amd64

Overview

Package winjob provides Windows Job Object based process-tree cleanup.

Unix has process groups: kill(-pgid, sig) reaches every descendant a spawned command has forked. Windows has no equivalent for an arbitrary process tree — Process.Kill() only ever reaches the one PID mcpproxy itself started (e.g. cmd.exe), never the grandchildren cmd.exe/npx/bash go on to spawn (node.exe, python.exe, the real MCP server). Those grandchildren were left running forever every time a stdio server was restarted, reconnected, or disconnected — see internal/upstream/core/process_windows.go and internal/upstream/launcher/launcher_windows.go, both previously TODO stubs that only killed the immediate child.

A Job Object created with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE restores the process-group-kill guarantee on Windows: once the immediate child is assigned to the job, every process IT spawns afterwards automatically joins the same job (job membership is inherited on process creation), so terminating the job reaches the whole tree in one call — matching what killProcessGroup already does on Unix via kill(-pgid, ...).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsSuspended added in v0.71.0

func IsSuspended(cmd *exec.Cmd) bool

IsSuspended reports whether cmd was marked to start suspended.

func MarkSuspended added in v0.71.0

func MarkSuspended(cmd *exec.Cmd)

MarkSuspended makes cmd start with its main thread suspended. It keeps any SysProcAttr fields the caller already set. Call it before cmd.Start.

func Resume added in v0.71.0

func Resume(pid int) error

Resume lets a process started with CREATE_SUSPENDED run, using NtResumeProcess (it resumes the main thread, the only one that exists).

Types

type Job

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

Job wraps a Windows Job Object configured to kill every member process (including any it spawns after joining) as soon as Close is called.

Close may be reached from more than one goroutine at once — the launcher's Stop path and its reaper both close the job — so the handle is guarded by a mutex and Close is idempotent.

func New

func New() (*Job, error)

New creates a job object with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE set.

func (*Job) Assign

func (j *Job) Assign(pid int) error

Assign adds the process with the given PID to the job. Must be called as soon as possible after the process starts — everything it spawns AFTER joining inherits job membership, but anything it spawns BEFORE joining (a race in principle) would escape. In practice the caller starts the child and assigns it within the same goroutine with no intervening work, so the window is negligible — the child (a shell/npx wrapper) has not yet had a chance to exec its own grandchild.

func (*Job) AssignAndResume added in v0.71.0

func (j *Job) AssignAndResume(pid int) error

AssignAndResume adds a suspended process to the job and only then lets it run, so everything it ever spawns is a member of the job. On an error the process is still suspended; the caller kills it.

func (*Job) Close

func (j *Job) Close() error

Close terminates every process still in the job and releases the handle. Safe to call more than once, concurrently, and on a nil *Job.

Jump to

Keyboard shortcuts

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