-
-
Notifications
You must be signed in to change notification settings - Fork 34k
util: process signal to exit code utility #60963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ErickWendel
wants to merge
6
commits into
nodejs:main
Choose a base branch
from
ErickWendel:erickwendel/add-convert-process-signal-to-exit-code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−0
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
54035da
util: add convertProcessSignalToExitCode utility
ErickWendel 42dbd61
doc: document convertProcessSignalToExitCode usage
ErickWendel 51cfb9e
fixup! util: add convertProcessSignalToExitCode utility
ErickWendel b107877
fixup! util: add convertProcessSignalToExitCode utility
ErickWendel 8337df1
fixup! doc: document convertProcessSignalToExitCode usage
ErickWendel ccf99c9
fixup! doc: document convertProcessSignalToExitCode usage
ErickWendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1547,6 +1547,42 @@ exited, `code` is the final exit code of the process, otherwise `null`. If the | |
| process terminated due to receipt of a signal, `signal` is the string name of | ||
| the signal, otherwise `null`. One of the two will always be non-`null`. | ||
|
|
||
| When `code` is `null` due to signal termination, you can use | ||
| [`util.convertProcessSignalToExitCode()`][] to convert the signal to a POSIX | ||
| exit code. | ||
|
|
||
| ```cjs | ||
| const { spawn } = require('node:child_process'); | ||
| const { convertProcessSignalToExitCode } = require('node:util'); | ||
|
|
||
| const ls = spawn('ls', ['-lh', '/usr']); | ||
|
|
||
| ls.kill(); | ||
|
|
||
| ls.on('exit', (code, signal) => { | ||
| const exitCode = convertProcessSignalToExitCode(signal); | ||
| console.log(`signal ${signal}, POSIX exit code: ${exitCode}`); | ||
| }); | ||
|
|
||
| // signal SIGTERM, POSIX exit code: 143 | ||
| ``` | ||
|
|
||
| ```mjs | ||
| 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 | ||
| ``` | ||
|
|
||
| When the `'exit'` event is triggered, child process stdio streams might still be | ||
| open. | ||
|
|
||
|
|
@@ -1671,6 +1707,16 @@ within the child process to close the IPC channel as well. | |
| The `subprocess.exitCode` property indicates the exit code of the child process. | ||
| If the child process is still running, the field will be `null`. | ||
|
|
||
| When the child process is terminated by a signal, `subprocess.exitCode` will be | ||
| `null`. To get the corresponding POSIX exit code, use | ||
|
||
| [`util.convertProcessSignalToExitCode(subprocess.signalCode)`][`util.convertProcessSignalToExitCode()`]. | ||
|
|
||
| **Note:** The `child_process` module does not automatically set `exitCode` when | ||
| a process is terminated by a signal to avoid breaking changes in existing code | ||
| that may depend on `exitCode` being `null` in such cases. The | ||
| [`util.convertProcessSignalToExitCode()`][] utility function is provided to | ||
| allow applications to opt-in to this conversion when needed. | ||
ErickWendel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ### `subprocess.kill([signal])` | ||
|
|
||
| <!-- YAML | ||
|
|
@@ -2393,6 +2439,7 @@ or [`child_process.fork()`][]. | |
| [`subprocess.stdin`]: #subprocessstdin | ||
| [`subprocess.stdio`]: #subprocessstdio | ||
| [`subprocess.stdout`]: #subprocessstdout | ||
| [`util.convertProcessSignalToExitCode()`]: util.md#utilconvertprocesssignaltoexitcodesignalcode | ||
| [`util.promisify()`]: util.md#utilpromisifyoriginal | ||
| [synchronous counterparts]: #synchronous-process-creation | ||
| [v8.serdes]: v8.md#serialization-api | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { mustCall, mustNotCall, isWindows } from '../common/index.mjs'; | ||
| import assert from 'assert'; | ||
| import { convertProcessSignalToExitCode } from 'util'; | ||
| import { spawn } from 'child_process'; | ||
| import os from 'os'; | ||
ErickWendel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Test valid signal names | ||
| { | ||
| // SIGTERM = 15, so 128 + 15 = 143 | ||
| assert.strictEqual(convertProcessSignalToExitCode('SIGTERM'), 143); | ||
|
|
||
| // SIGKILL = 9, so 128 + 9 = 137 | ||
| assert.strictEqual(convertProcessSignalToExitCode('SIGKILL'), 137); | ||
|
|
||
| // SIGINT = 2, so 128 + 2 = 130 | ||
| assert.strictEqual(convertProcessSignalToExitCode('SIGINT'), 130); | ||
|
|
||
| // SIGHUP = 1, so 128 + 1 = 129 | ||
| assert.strictEqual(convertProcessSignalToExitCode('SIGHUP'), 129); | ||
|
|
||
| // SIGABRT = 6, so 128 + 6 = 134 | ||
| assert.strictEqual(convertProcessSignalToExitCode('SIGABRT'), 134); | ||
| } | ||
|
|
||
| // Test invalid signal names return null | ||
| { | ||
| assert.strictEqual(convertProcessSignalToExitCode('INVALID'), null); | ||
| assert.strictEqual(convertProcessSignalToExitCode(''), null); | ||
| assert.strictEqual(convertProcessSignalToExitCode('SIG'), null); | ||
| } | ||
|
|
||
| // Test invalid argument types | ||
| { | ||
| [ | ||
| undefined, | ||
| null, | ||
| 123, | ||
| true, | ||
| false, | ||
| {}, | ||
| [], | ||
| Symbol('test'), | ||
| () => {}, | ||
| ].forEach((value) => { | ||
| assert.throws( | ||
| () => convertProcessSignalToExitCode(value), | ||
| { | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| name: 'TypeError', | ||
| } | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| // Test with actual child process termination | ||
| { | ||
| const cat = spawn(isWindows ? 'cmd' : 'cat'); | ||
| cat.stdout.on('end', mustCall()); | ||
| cat.stderr.on('data', mustNotCall()); | ||
| cat.stderr.on('end', mustCall()); | ||
|
|
||
| cat.on('exit', mustCall((code, signal) => { | ||
| assert.strictEqual(code, null); | ||
| assert.strictEqual(signal, 'SIGTERM'); | ||
| assert.strictEqual(cat.signalCode, 'SIGTERM'); | ||
|
|
||
| // Verify the utility function returns correct exit code | ||
| const exitCode = convertProcessSignalToExitCode(signal); | ||
| assert.strictEqual(exitCode, 143); | ||
| })); | ||
|
|
||
| assert.strictEqual(cat.signalCode, null); | ||
| assert.strictEqual(cat.killed, false); | ||
| cat[Symbol.dispose](); | ||
| assert.strictEqual(cat.killed, true); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove the code samples and move the text to the bottom of the section – there's very little reason why somebody would want to do this, while information such as that stdio streams may still be open is much more directly relevant to what this event does
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done