Skip to main content

Module spawn

Module spawn 

Source
Expand description

Two-mode process spawning. Free functions only — no module-internal traits.

Modes (only two; the dangerous combination detached + caller-pipes has no API surface):

  • spawn_daemon — detached lifetime, NUL stdio, sanitized handle list, no console window, ignores parent’s Ctrl-C. The returned DaemonChild does NOT die when dropped.
  • spawn — contained lifetime, caller-controlled stdio via SpawnStdio, sanitized handle list, no console window by default (opt in via SpawnStdio::show_console), bounded drain. The returned SpawnedChild kills the child on Drop.

§Sanitized handle inheritance

Both modes inherit ONLY the three stdio handles we resolve here. On Windows we use PROC_THREAD_ATTRIBUTE_HANDLE_LIST to whitelist exactly the resolved handles. On Unix the spawned child runs a pre_exec closure that walks /proc/self/fd (or /dev/fd) and closes every fd > 2.

Motivation: when a process tree has a pipe-redirected ancestor (Python subprocess.Popen(stdout=PIPE), IDE language-server hosts, CI runners, etc.), every intermediate CreateProcessW(bInheritHandles=TRUE) on Windows — and every fork+exec of a non-O_CLOEXEC fd on Unix — duplicates that orphaned pipe write-end into the new child. The original reader at the top never sees EOF.

Issue: https://github.com/zackees/running-process/issues/110.

Structs§

DaemonChild
Handle to a detached daemon spawned via spawn_daemon.
SpawnStdio
Caller-supplied stdio bindings for spawn.
SpawnedChild
Handle to a contained child spawned via spawn.

Enums§

StdioSource
Per-slot source describing what the child should inherit for one of stdin / stdout / stderr.

Functions§

spawn
Spawn command as a contained child with caller-controlled stdio. Sanitized handles, CREATE_NO_WINDOW. Child dies when the returned SpawnedChild is dropped.
spawn_daemon
Spawn command as a detached daemon. NUL stdio, sanitized handles, no console window, ignores parent’s Ctrl-C / SIGINT (Windows: CREATE_NEW_PROCESS_GROUP + DETACHED_PROCESS; Unix: setsid puts the daemon in a new session so it’s not in the parent’s foreground group).
spawn_daemon_with_clear_env
Like spawn_daemon but with explicit control over whether the daemon’s inherited env is passed through to the child.