-
-
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
base: main
Are you sure you want to change the base?
util: process signal to exit code utility #60963
Conversation
Add convertProcessSignalToExitCode() to convert signal names to POSIX exit codes (128 + signal number). Exposed in public util API. Refs: nodejs#60720
Document util.convertProcessSignalToExitCode() in child_process module to help users convert signal names to POSIX exit codes when a child process is terminated by a signal. Refs: nodejs#60285
| validateString(signalCode, 'signalCode'); | ||
|
|
||
| const signalNumber = signals[signalCode]; | ||
| if (signalNumber === undefined) { |
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.
We can use validateOneOf here. e.g: https://github.com/nodejs/node/blob/main/lib/util.js#L149
| console.log(`signal ${signal}, POSIX exit code: ${exitCode}`); | ||
|
|
||
| // signal SIGTERM, POSIX exit code: 143 | ||
| ``` |
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
| **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. |
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'd drop this paragraph. Saying "we don't change this API because changing it would be a breaking change" applies to virtually any API we have. Providing a single link to util.convertProcessSignalToExitCode() should be sufficient (it's not something that people are going to need a lot). (EDIT: and maybe it makes more sense to link to that from the .signalCode docs than from the .exitCode docs?)
| 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 |
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 think the really important thing we should mention is that subprocess.signalCode will be set, and we should link to that.
| * Type: {string|null} | ||
| The `subprocess.signalCode` property indicates the signal received by | ||
| the child process if any, else `null`. |
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.
Might also be worth linking back to .exitCode from here
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #60963 +/- ##
=======================================
Coverage 88.53% 88.53%
=======================================
Files 703 703
Lines 208413 208428 +15
Branches 40191 40198 +7
=======================================
+ Hits 184521 184536 +15
- Misses 15902 15909 +7
+ Partials 7990 7983 -7
🚀 New features to boost your workflow:
|
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
codeparameter in the'exit'and'close'events isnull. This utility allows users to convert thesignalparameter to the POSIX standard exit code.Example
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