Skip to main content
pipeTo handles SIGTERM and SIGINT by default for every target. On a signal, the pipe stops reading, finishes the batch it is writing, saves its cursor, closes the resources it created, and rejects with PipelineStoppedError.

Options

Configure shutdown behavior through the second argument to pipeTo:
timeout takes a positive duration string with a unit, such as '500ms', '30s', '1m', '2m 30s', or '1h'. Passing an invalid, zero, or negative duration—or a bare number—rejects with E0005 before the pipe starts. Set timeout below your orchestrator’s termination grace period (for example Kubernetes terminationGracePeriodSeconds) so the pipe finishes draining before SIGKILL.

What happens on a signal

  1. The pipe stops reading. Any in-flight read waiting for the next block, any retry, and any read-ahead batch are cancelled, including during startup.
  2. The batch the target is writing finishes and saves its cursor. A commit or fork rollback already in progress is never cancelled, and commit retries continue.
  3. Transformers stop, and the target flushes and closes what it opened. Parquet closes its open files at the last written batch.
  4. onShutdown runs after draining and cleanup settle, even if they failed.
  5. pipeTo rejects (see How pipeTo settles).
timeout bounds how long pipeTo waits. If it expires during draining, pipeTo rejects immediately with E3002, while onShutdown waits for draining and cleanup to settle (and never runs if draining never settles). Additional signals do not restart the timeout.

Portal reads in hooks and caches

Transformer start hooks receive the run’s effective finalized mode and an optional signal. Pass signal as abort to cancel extra Portal reads and retries on shutdown:
Custom PortalCache implementations receive the same finalized and signal fields. Forward them on a cache miss with portal.getStream(query, { finalized, request: { abort: signal } }) and stop iterating when signal aborts. The built-in portalSqliteCache handles this automatically.

How pipeTo settles

pipeTo resolves only when the configured range completes, so code after await stream.pipeTo(target) does not run on a signal stop. A signal that arrives while a completed range is closing also counts as a stop.

Exit codes

The SDK never calls process.exit(). An unhandled await stream.pipeTo(target) exits with code 1 on a signal stop. Catch PipelineStoppedError to exit cleanly with code 0 once nothing else keeps the event loop alive:
After a timeout (PipelineShutdownTimeoutError), uncancelled work may keep the process alive; decide explicitly when to exit if you catch it. The SDK removes its signal listeners as soon as no managed pipeTo runs remain.

Resource ownership

The SDK closes only resources it created itself. Clients you pass in stay open—for every stop reason, including range completion—so other pipes or application code can share them: When running several pipes against one shared client, close the client after all pipes settle:

Multiple pipes and custom signal handlers

One signal stops every managed pipe in the process, and each runs its own onShutdown callback with its own timeout. Any pipe configured with shutdown: false keeps running until your code stops it. Your own process.on('SIGTERM', ...) handlers still run alongside the SDK’s. A custom handler that calls process.exit() terminates the process before draining finishes—move that cleanup into onShutdown, or pass shutdown: false and coordinate shutdown yourself.

SIGKILL and crash recovery

SIGKILL, crashes, and lost machines skip draining. On restart, each target recovers to its saved cursor and resumes from the next block: The same recovery applies if a shutdown exceeds timeout and the process is killed.