Skip to content
// EFFECT DAYS 2026

NodeChildProcessSpawner unstable

Shared Node.js implementation of the child process spawner service.

This module adapts node:child_process.spawn to the Effect ChildProcessSpawner service. Provide layer to run ChildProcess commands in Node-compatible runtimes: commands get scoped process handles with stdin sinks, stdout and stderr streams, exit-code waiting, interruption-time cleanup, process killing, and custom file-descriptor pipes. Provide layerProxyProcess instead to launch commands from a separate proxy process, for Node.js builds where spawning from a process that holds a lot of memory stalls the event loop. Packaged applications that start the proxy process themselves run it with NodeChildProcessProxy.run.

The implementation sits below the command-building API. It validates and resolves cwd through the Effect FileSystem and Path services, translates Node errno failures to PlatformError, and uses scopes to terminate referenced children when the owning effect is interrupted or finalized. Pipelines are flattened by flattenCommand and spawned one process at a time, wiring the selected source stream (stdout, stderr, all, or fdN) to the destination stdin or fdN.

Scoped release and kill wait for the signalled process group. Without forceKillAfter, cleanup waits for the leader to exit with no time limit, then gives remaining group members up to one second to exit, without escalating. With it, cleanup may take the configured duration plus a final one-second wait after SIGKILL. Zombie descendants can consume either full bound. On Windows, taskkill terminates the tree while the leader is running, and cleanup waits only for the leader's exit. Descendants that outlive the leader are left running.

POSIX cleanup targets a numeric process-group ID. If the group disappears and its ID is reused before cleanup, an unrelated group may be signalled.

5 exports Since v4.0.0 Source

Layers

layer

unstable
Since v4.0.0

Layer that provides the NodeChildProcessSpawner implementation.

Signature

declare const layer: Layer.Layer<ChildProcessSpawner, never, FileSystem.FileSystem | Path.Path>
Since v4.1.6

Creates a layer that provides a ChildProcessSpawner which launches commands from a small, long-lived proxy process instead of the current process, along with the ProxyProcess service.

When to use

Use when spawning a command measurably stalls the event loop of a Node.js application on Linux that holds a lot of memory outside the JavaScript heap, such as large Buffers. The stall depends on the Node.js build, so measure a direct spawn with layer on the deployed build first.

Details

Only the spawn request goes through the proxy process. Piped stdio is connected to the application over Unix domain sockets, and signals are sent to the command directly, so output does not pass through the proxy process.

Building the layer starts the proxy process and waits until it is ready. The build fails with a PlatformError if it cannot start, does not answer within responseTimeout (10 seconds by default), or runs on Windows. Fall back with Layer.catch(() => NodeChildProcessSpawner.layer). The proxy process stops when the layer's scope closes, once its remaining referenced commands have exited. Commands keep running if the application exits, as with layer.

By default the proxy process runs process.execPath --input-type=module -e <program> with the application's environment minus NODE_OPTIONS. executable, args and env replace these. A custom entry point must call NodeChildProcessProxy.run. Commands always receive their own environment.

If the proxy process exits, or the application changes its user or group IDs, the next spawn starts a new proxy process and waits until it is ready. Spawns are not queued: each one waits, interruptibly, and its request is created only when it is sent, so a spawn interrupted while waiting never starts its command. A spawn whose proxy process fails to start fails with a PlatformError. Its command was not sent, so retrying is safe. After the layer's scope closes, spawns fail without starting a proxy process.

A spawn request is sent at most once. If the proxy process exits or does not reply within responseTimeout after a request is sent, the command may already be running. The proxy process is stopped, the spawn fails with a PlatformError, and the request is not sent again.

Gotchas

  • A proxy process that does not respond in time is killed, so the exit status of every command it launched is lost.
  • A command that started but was never acknowledged cannot be signalled, since its process ID was never received. Its stdio is closed.
  • Commands run with the user and group IDs the proxy process started with. Build the layer after the application has changed its credentials, and before it has grown large, since starting the proxy process from a large process stalls once.
  • If the proxy process exits early, commands it launched keep their stdio, but exitCode and isRunning fail with a PlatformError because their exit can no longer be observed. kill still sends its first signal, without escalating, and that signal can reach an unrelated process if the command has already exited and its ID has been reused. On a single-command handle kill then fails with a PlatformError. A pipeline handle's kill ignores the failures of its commands, so it succeeds. Scope release does not signal these commands, so they keep running.
  • Every call creates a separate layer, with its own proxy process. Keep the result in one constant to share a proxy process.
  • The socket path under os.tmpdir() must fit the platform limit of about 100 bytes.
  • Numeric file descriptors in stdin, stdout, stderr, or additionalFds fail with BadArgument, since descriptors belong to the application process rather than the proxy process.

Signature

declare function layerProxyProcess(options?: {
readonly args?: readonly Array<string>;
readonly env?: Record<string, string | undefined>;
readonly executable?: string;
readonly responseTimeout?: Input;
}): Layer<ChildProcessSpawner | ProxyProcess, PlatformError, FileSystem | Path>

Example Wiring a packaged command-line application

// bin.ts dispatches `__proxy-process` to `NodeChildProcessProxy.run` before
// it imports the application
import * as NodeChildProcessSpawner from "@effect/platform-node/NodeChildProcessSpawner"
import { Effect, Layer, Option } from "effect"
import * as NodeSea from "node:sea"
// One constant, so the whole application shares one proxy process
export const SpawnerLayer = NodeChildProcessSpawner.layerProxyProcess({
// In a single executable application, process.execPath is the application
args: NodeSea.isSea() ? ["__proxy-process"] : undefined
}).pipe(Layer.catch(() => NodeChildProcessSpawner.layer))
// The parent process ID of commands spawned from now on
export const spawnParentPid = Effect.gen(function*() {
const proxy = yield* Effect.serviceOption(NodeChildProcessSpawner.ProxyProcess)
const pid = Option.isSome(proxy) ? yield* proxy.value.currentPid : Option.none()
return Option.getOrElse(pid, () => process.pid)
})

Models

FlattenedPipeline

interface unstable
Since v4.0.0

Result of flattening a pipeline of commands.

Signature

interface FlattenedPipeline {
readonly commands: readonly [StandardCommand, StandardCommand];
readonly pipeOptions: readonly Array<PipeOptions>;
}

Services

Since v4.1.6

Service that reports the proxy process started by layerProxyProcess.

Details

currentPid is the process ID of the proxy process that new spawns go to, which is the parent process ID of the commands it launches. It is None from the moment that proxy process is seen to exit, or is replaced after a credential change, until the replacement started by a later spawn is ready. It is also None after the layer's scope closes.

Gotchas

  • The service does not show which spawner is in context. If a layer providing another ChildProcessSpawner, such as layer, is merged over layerProxyProcess, commands are spawned directly while this service still reports the proxy process.
  • Commands of a proxy process that was replaced keep their old parent.

Signature

declare class ProxyProcess extends Shape<"@effect/platform-node/NodeChildProcessSpawner/ProxyProcess", {
readonly currentPid: Effect<Option<ProcessId>>;
}, this> {
constructor(_: never);
}

Transforming

Since v4.0.0

Flattens a Command into an array of StandardCommands along with pipe options for each connection.

Signature

declare function flattenCommand(command: Command): FlattenedPipeline