Skip to content
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

src: fix kill signal 0 on Windows #57695

Merged
merged 1 commit into from
Apr 4, 2025

Conversation

StefanStojanovic
Copy link
Contributor

The previous changes to this file missed the special case of process.kill(0), thus breaking it on Windows. This PR fixes that.

Refs: #55514
Refs: #42923
Fixes: #57669

@StefanStojanovic StefanStojanovic added windows Issues and PRs related to the Windows platform. process Issues and PRs related to the process subsystem. labels Mar 31, 2025
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. child_process Issues and PRs related to the child_process subsystem. needs-ci PRs that need a full CI run. labels Mar 31, 2025
Copy link

codecov bot commented Mar 31, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.24%. Comparing base (657f818) to head (cd81beb).
Report is 23 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #57695   +/-   ##
=======================================
  Coverage   90.23%   90.24%           
=======================================
  Files         630      630           
  Lines      185017   185017           
  Branches    36207    36216    +9     
=======================================
+ Hits       166948   166966   +18     
- Misses      11020    11025    +5     
+ Partials     7049     7026   -23     
Files with missing lines Coverage Δ
src/process_wrap.cc 66.66% <ø> (ø)

... and 27 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@targos
Copy link
Member

targos commented Mar 31, 2025

Is it possible to add a test?

@StefanStojanovic
Copy link
Contributor Author

Is it possible to add a test?

Good idea. I've added it. Hopefully, the 1 second I gave it will be enough for the CI machines.

Comment on lines 66 to 71
const checkProcess = spawn(process.execPath, ['-e', 'setTimeout(() => {}, 1000)']);
checkProcess.on('exit', (code, signal) => {
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});
checkProcess.kill(0);
Copy link
Member

@lpinca lpinca Apr 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion (that can be ignored) to not rely on timers and not wait a second for the test to end.

Suggested change
const checkProcess = spawn(process.execPath, ['-e', 'setTimeout(() => {}, 1000)']);
checkProcess.on('exit', (code, signal) => {
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});
checkProcess.kill(0);
const checkProcess = spawn(process.execPath, [
'-e',
'setInterval(() => {}, 1000)'
]);
checkProcess.on('exit', (code, signal) => {
assert.strictEqual(code, null);
assert.strictEqual(signal, 'SIGTERM');
});
checkProcess.kill(0); // 'SIGKILL' is not sent to the child process.
checkProcess.kill('SIGTERM');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like changing timeout to interval and then killing it manually to have complete control. However, the new asserts change the nature of the test itself. We want to test checkProcess.kill(0); and see if the code and signal will be as expected. This change checks if sending 0 will kill the process, but not if it'll return the expected values. Correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, correct, the assumption is that checkProcess.kill(0) is a no-op because the received signal is 'SIGTERM'. If it wasn't the received signal would be 'SIGKILL'. I think this is sufficient to assert that it doesn't change the signal. However, as you said, there is no check on the exit code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option can be something like this:

const code = `const interval = setInterval(() => {}, 1000);
process.stdin.on('data', () => { clearInterval(interval); });
process.stdout.write('x');
`;

const checkProcess = spawn(process.execPath, ['-e', code]);

checkProcess.on('exit', (code, signal) => {
  assert.strictEqual(code, 0);
  assert.strictEqual(signal, null);
});

checkProcess.stdout.on('data', common.mustCall((chunk) => {
  assert.strictEqual(chunk.toString(), 'x');
  checkProcess.kill(0);
  checkProcess.stdin.write('x');
  checkProcess.stdin.end();
}));

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that one. It's more elegant IMHO. You can submit it as a new suggestion if you want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to apply it yourself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, please reapprove when you get a chance. Thanks.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 1, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 1, 2025
@nodejs-github-bot
Copy link
Collaborator

This special case was missed in the previous changes to this file.

Refs: nodejs#55514
Refs: nodejs#42923
Fixes: nodejs#57669
@StefanStojanovic StefanStojanovic added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 3, 2025
@github-actions github-actions bot added request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Apr 3, 2025
Copy link
Contributor

github-actions bot commented Apr 3, 2025

Failed to start CI
   ⚠  Commits were pushed since the last approving review:
   ⚠  - src: fix kill signal 0 on Windows
   ✘  Refusing to run CI on potentially unsafe PR
https://github.com/nodejs/node/actions/runs/14239782978

@StefanStojanovic StefanStojanovic removed the request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. label Apr 3, 2025
@richardlau richardlau added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 3, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 3, 2025
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@mcollina mcollina added commit-queue Add this label to land a pull request using GitHub Actions. and removed needs-ci PRs that need a full CI run. labels Apr 4, 2025
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Apr 4, 2025
@nodejs-github-bot nodejs-github-bot merged commit 32e5e81 into nodejs:main Apr 4, 2025
63 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 32e5e81

@richardlau richardlau added lts-watch-v20.x PRs that may need to be released in v20.x lts-watch-v22.x PRs that may need to be released in v22.x labels Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. child_process Issues and PRs related to the child_process subsystem. lts-watch-v20.x PRs that may need to be released in v20.x lts-watch-v22.x PRs that may need to be released in v22.x process Issues and PRs related to the process subsystem. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

subprocess.kill(0) terminates the subprocess on Windows since v23.4.0
7 participants