util: process signal to exit code utility
Description
Adds util.convertProcessSignalToExitCode() utility function to convert signal names (e.g., SIGTERM, SIGKILL) to their corresponding POSIX exit codes.
When a child process is terminated by a signal, the code parameter in the 'exit' and 'close' events is null. This utility allows users to convert the signal parameter to the POSIX standard exit code.
Example
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import { convertProcessSignalToExitCode } from 'node:util';
const ls = spawn('ls', ['-lh', '/usr']);
ls.kill();
const [code, signal] = await once(ls, 'exit');
const exitCode = convertProcessSignalToExitCode(signal);
console.log(`signal ${signal}, POSIX exit code: ${exitCode}`);
// signal SIGTERM, POSIX exit code: 143
Note: While Windows doesn't natively support POSIX signals, Node.js provides a cross-platform abstraction that emulates signal behavior. This allows convertProcessSignalToExitCode() to work consistently across all platforms, returning the same POSIX exit codes on both Unix-like systems and Windows.
Refs
- https://github.com/nodejs/node/issues/60285
- https://github.com/nodejs/node/pull/60720
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 88.51%. Comparing base (cbe0233) to head (8bbb912).
:warning: Report is 30 commits behind head on main.
Additional details and impacted files
@@ Coverage Diff @@
## main #60963 +/- ##
==========================================
- Coverage 88.53% 88.51% -0.03%
==========================================
Files 703 703
Lines 208413 208512 +99
Branches 40191 40215 +24
==========================================
+ Hits 184521 184565 +44
- Misses 15902 15963 +61
+ Partials 7990 7984 -6
| Files with missing lines | Coverage Δ | |
|---|---|---|
| lib/internal/util.js | 96.81% <100.00%> (+0.43%) |
:arrow_up: |
| lib/util.js | 97.97% <100.00%> (+<0.01%) |
:arrow_up: |
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
CI: https://ci.nodejs.org/job/node-test-pull-request/70458/
Seemingly related failure on Windows:
---
duration_ms: 175.001
exitcode: 1
severity: fail
stack: |-
node:internal/modules/run_main:107
triggerUncaughtException(
^
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
150 !== 134
at file:///c:/workspace/node-test-binary-windows-js-suites/node/test/parallel/test-util-convert-signal-to-exit-code.mjs:20:10
at ModuleJob.run (node:internal/modules/esm/module_job:430:25)
at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:654:26)
at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5) {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: 150,
expected: 134,
operator: 'strictEqual',
diff: 'simple'
}
Node.js v26.0.0-pre
CI: https://ci.nodejs.org/job/node-test-pull-request/70463/
Seemingly related failure on Windows:
--- duration_ms: 175.001 exitcode: 1 severity: fail stack: |- node:internal/modules/run_main:107 triggerUncaughtException( ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 150 !== 134 at file:///c:/workspace/node-test-binary-windows-js-suites/node/test/parallel/test-util-convert-signal-to-exit-code.mjs:20:10 at ModuleJob.run (node:internal/modules/esm/module_job:430:25) at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:654:26) at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 150, expected: 134, operator: 'strictEqual', diff: 'simple' } Node.js v26.0.0-pre
This was due I fogot to use the constants.signals in the test. Now it should pass,
CI: https://ci.nodejs.org/job/node-test-pull-request/70483/
CI: https://ci.nodejs.org/job/node-test-pull-request/70487/
CI: https://ci.nodejs.org/job/node-test-pull-request/70491/
Landed in e705603a6f0a5803599f4b85e9072737291d2db6