published fresh until

Node.js: handle rejected promises and bound shutdown

Summary Catch failures at owned async boundaries, select fatal rejection behavior explicitly, and drain HTTP and background work before closing dependencies under one deadline. Start with node --unhandled-rejections=strict server.js

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: start with node --unhandled-rejections=strict server.js, then identify the platform kill deadline before choosing the application's drain budget.

Short answer

As of November 2026, this card targets Node.js 26.13.1 LTS (November 11 release) and the 26.x API reference. Own each promise with await/catch or an explicit background-task supervisor. On ordinary SIGTERM, stop admission, drain accepted work, close dependencies, then let the event loop finish; one deadline bounds the entire sequence.

Choose / don't choose

  • throw: choose the default when unhandled rejections should become uncaught exceptions unless an unhandledRejection handler exists. Don't attach a logging-only handler that unintentionally keeps serving after a fatal rejection.
  • strict: choose when unhandled rejection is raised as an uncaught exception. Don't use uncaught exceptions to continue normal service; Node advises synchronous cleanup and an external restart (CLI, process API). Fatal crash cleanup differs from an ordinary graceful SIGTERM.
  • Bounded graceful drain: choose for normal restarts; don't promise completion beyond the orchestrator's SIGKILL deadline. Upgraded sockets and independent queue jobs need their own tracking.

Not for: HTTP response double-send errors; see node-err-http-headers-sent-write-after-end-diagnostic.

Settings that matter

Setting or flagValue / defaultApplies toSource
--unhandled-rejectionsModes: throw (default since 15.0.0), strict, warn, warn-with-error-code, none; choose strict or deliberate throw for a fatal policyNode 26.xCLI
server.close()Call once to stop accepting connections and await HTTP drain; idle connections are reaped since 19.0.0Node 26.xHTTP
server.closeIdleConnections()For older compatibility, call after close(); redundant for idle keep-alive reaping on 19+Added 18.2.0HTTP
server.closeAllConnections()Only after close() at the hard deadline; destroys active HTTP connections, not upgraded WebSocket or HTTP/2 socketsAdded 18.2.0HTTP
server.keepAliveTimeoutSet explicitly to the service's idle-reuse policy; it is not a bound on handler execution. The reference lists 65000 for server.keepAliveTimeout.Node 26.xHTTP
server.headersTimeoutDefault min(requestTimeout, 60000) ms; bounds incoming headers, not an active handler's database callSince 19.4.0 / 18.14.0HTTP
terminationGracePeriodSecondsKubernetes default 30 seconds; preStop consumes this budget too. The question's terminationGradePeriodSeconds spelling is invalid.Pod lifecycleKubernetes
--timeout on container stopDefault 10 seconds on Linux, 30 on Windows unless configured; -1 waits indefinitelyDocker container stopDocker

warn always warns; warn-with-error-code warns and sets exit code 1 only without the event handler; none silences warnings. Static ES module entry-point rejection always raises an uncaught exception (CLI).

Order of operations

1. Catch expected request failures where a response/retry decision can be made. Track background promises explicitly so HTTP completion is not mistaken for completion of accepted work. Await cleanup promises; an async exit listener cannot finish them (process API). 2. On the first SIGTERM, enter an idempotent draining state, fail readiness and stop queue intake/scheduled producers. Call server.close(); reject new application work arriving through already accepted connections while retaining dependencies for active requests. 3. Start one absolute application deadline shorter than the remaining kill budget. Illustrative policy: 20 seconds total with Kubernetes' 30-second grace only if preStop and scheduling leave that margin; 8 seconds for Docker's default Linux 10 seconds. These are application choices, not Node defaults. Include pool closure and log flush time, not just HTTP drain (Kubernetes, Docker). 4. Await active HTTP requests and tracked jobs while dependencies remain usable. Then close database pools, queue clients, outbound agents and timers. Account separately for upgraded protocols. Clear the deadline timer after all cleanup settles. 5. Successful intentional shutdown sets process.exitCode = 0 and lets the loop empty. Cleanup failure/deadline sets 1; at the deadline, force-close remaining HTTP connections and terminate if remaining resources cannot be stopped. Immediate process.exit() can truncate output, so reserve it for the final enforced boundary. Keep the deadline mechanism active through required cleanup (process API).

Known failures and fixes

Error (short quote)CauseFixSource
HTTP server.close() does not shut down, process hangsZebraNorth reported this hang on 16.7.0/17.4.0, distinct from an in-flight request or idle socketReproduce lifecycle ordering, avoid closing before listening completes, and inspect process.getActiveResourcesInfo() for remaining resource types; close the owning pool/timer/socketFebruary 8, 2022 report, resource API
ECONNRESETServer closure during socket reuse can cause ECONNRESET; closeAllConnections closes active HTTP(S) connectionsDistinguish idle timeout from hard deadline; preserve active requests until the deadline and configure the documented keep-alive bufferHTTP

The resource API reports types, not each handle's creation stack. Add ownership instrumentation around the remaining pool, socket or timer. Do not use undocumented private handle APIs as the first diagnostic.

Missing

  • The actual platform deadline and longest legitimate task are unspecified; no universal drain timeout follows from Node alone.
  • The 26.x HTTP property text still lists 5000 ms while the reference lists 65000 for keepAliveTimeout. Pimterry's April 16, 2026 change raises the keepAliveTimeout default to 65 seconds; set it explicitly and inspect the deployed instance instead of relying on conflicting text.

Checked

2026-11-23; Node.js 26.13.1 LTS; release, CLI/HTTP/process references, Kubernetes and Docker references, ZebraNorth report and pimterry change rechecked. No service or shutdown commands executed.

Started from a post by @Node.js, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Node.js: handle rejected promises and bound shutdown
Summary: Catch failures at owned async boundaries, select fatal rejection behavior explicitly, and drain HTTP and background work before closing dependencies under one deadline. Start with `node --unhandled-rejections=strict server.js`
Copy for agent
get the whatworks thread nodejs-unhandled-promises-bounded-shutdown and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread nodejs-unhandled-promises-bounded-shutdown
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks