You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{execSync}from'child_process';process.on('SIGTERM',()=>{console.log('Received SIGTERM. Exiting...');process.exit(0);});constoutput=execSync('ps -e | grep npm').toString().split(/\s+/).filter(Boolean);constpid=Number(output[0]);process.kill(pid)setTimeout(()=>{console.log('Fatal: Signal was not received. Exiting... ');process.exit(1)},2000);
Environment
npm: 9.8.0
Node.js: v20.5.0
OS Name: Linux nixos 5.15.93 x86_64 GNU/Linux
System Model Name:
npm config:
; node bin location = /nix/store/1gnvy2dhh311c900hzyw6ppjdhnir2s5-nodejs-20.5.0/bin/node; node version = v20.5.0; npm local prefix = /build; npm version = 9.8.0; cwd = /build; HOME = /homeless-shelter; Run `npm config ls -l` to show all defaults.
@ryanrasti I'm getting this same behavior in npm v9.6.7. Can you verify what npm version was working?
In my case package.json scripts that were receiving the signals fine in npm v8.19.4 (node v16.20.1) stopped working when upgrading directly to npm v9.6.7 (node v18.17.0).
There is a possibly identical issue that was opened at #6547, but no triage occurred in the month since it was opened.
This may be caused by a change made in npm/run-script#142. There were no tests associated with the changed behavior. Maybe an issue needs opened in that repository?
I have created a demo repo to showcase the bug. The repo verifies that this only happens in versions of NPM >=9.6.7 <10.3.0. If you experience this in another version of NPM, please create a repo which shows the error happening.
The reason for the inconsistency, where some report the signals are forwarded successfully while others do not, is: The inconsistent part is because of the use of sh in @npmcli/promise-spawn.
The signal is forwarded correctly in npm, but may be ignored by sh. I don't test very much, but at least in node:20-alpine the signal is forwarded to the underlying script but in node:20-slim (debian) not. And also, using Ctrl + C in the terminal will always work as it doesn't rely on signal forwarding only but also uses the job control to send signals to the child process)
The reason for the inconsistency, where some report the signals are forwarded successfully while others do not, is: The inconsistent part is because of the use of sh in @npmcli/promise-spawn.
The signal is forwarded correctly in npm, but may be ignored by sh. I don't test very much, but at least in node:20-alpine the signal is forwarded to the underlying script but in node:20-slim (debian) not. And also, using Ctrl + C in the terminal will always work as it doesn't rely on signal forwarding only but also uses the job control to send signals to the child process)
Seeing the same behavior, the bug persists in node:22, but node:22-alpine forwards the signal correctly.
It has something to do with the default shell in your image. node:22 image defaults to using dash whereas bash or busybox with node:22-alpine does an implicit exec-like optimization that replaces the shell. This screenshot shows it pretty clearly, explaining why the signal might not be getting to our node apps.
The scripts with npm in node:22 have 2 subprocess "hops", one from npm itself and another from the shell. Whereas with bash as default (/bin/sh) shell it will only create one subprocess which npm will relay the signal to.
Activity
ngbrown commentedon Jul 28, 2023
@ryanrasti I'm getting this same behavior in npm v9.6.7. Can you verify what npm version was working?
In my case
package.jsonscripts that were receiving the signals fine in npm v8.19.4 (node v16.20.1) stopped working when upgrading directly to npm v9.6.7 (node v18.17.0).There is a possibly identical issue that was opened at #6547, but no triage occurred in the month since it was opened.
This may be caused by a change made in npm/run-script#142. There were no tests associated with the changed behavior. Maybe an issue needs opened in that repository?
cc: @nlf, @wraithgar
ryanrasti commentedon Jul 29, 2023
Indeed, good catch @ngbrown , the version that worked for me was 9.6.6 (not 9.6.7).
Indeed, this looks like a duplicate of #6547 but that bug is incorrectly tagged with
Release 8.xthejohnfreeman commentedon Aug 21, 2023
How does this issue still have zero response? Very disappointing. @nlf @wraithgar
For those looking for a workaround on Linux, you can kill the entire process tree like this:
As a bonus, you can inspect the process tree with either the root process ID or the process group ID like this:
dmaizel commentedon Oct 18, 2023
Any news about this?
Revert Signal Handling Regression (#188)
fix: Revert Signal Handling Regression (#188)
tdislay commentedon Feb 7, 2024
Since npm@v10.3.0 which depends of @npm/run-script@7.0.3 (in which the regression was fixed), everything works fine for me :).
Thanks 🎉
mahnunchik commentedon Feb 19, 2024
I've faced the same issue...
DesignByOnyx commentedon Mar 6, 2024
I can confirm that npm version 10.3.0 does forward the signals as expected.
Update NPM to fix npm/cli#6684
grcode123 commentedon Mar 28, 2024
I think this is still happening on Ubuntu 20.04.6 LTS with node 18.20.0 and npm 10.5.0
Dzieni commentedon Apr 24, 2024
We have tested 10.3.0 and 10.5.0 on Node 21.7.3 & MacOS Sonoma and unfortunately the problem is still there.
ekeren commentedon Apr 24, 2024
I am able to reproduce on npm 10.3.0 and node v21.6.2, on macos 13.4
skyrpex commentedon Apr 24, 2024
Here a small repository with another example that reproduces the problem: https://github.com/skyrpex/npm-scripts-sigint-problem.
The issue happens with npm
10.5.2.DesignByOnyx commentedon Apr 24, 2024
I have created a demo repo to showcase the bug. The repo verifies that this only happens in versions of NPM
>=9.6.7 <10.3.0. If you experience this in another version of NPM, please create a repo which shows the error happening.https://github.com/DesignByOnyx/npm-signals-bug
mahnunchik commentedon Dec 7, 2024
Any news?
ImSingee commentedon Jan 2, 2025
The reason for the inconsistency, where some report the signals are forwarded successfully while others do not, is: The inconsistent part is because of the use of
shin @npmcli/promise-spawn.The signal is forwarded correctly in npm, but may be ignored by sh. I don't test very much, but at least in
node:20-alpinethe signal is forwarded to the underlying script but innode:20-slim(debian) not. And also, usingCtrl + Cin the terminal will always work as it doesn't rely on signal forwarding only but also uses the job control to send signals to the child process)ImSingee commentedon Jan 2, 2025
And before waiting for a fix, using something like
"start": "exec node index.js"can workfilip-jaksic commentedon May 14, 2025
Seeing the same behavior, the bug persists in
node:22, butnode:22-alpineforwards the signal correctly.jtomlin12 commentedon Nov 25, 2025
It has something to do with the default shell in your image.
node:22image defaults to usingdashwhereasbashorbusyboxwithnode:22-alpinedoes an implicitexec-like optimization that replaces the shell. This screenshot shows it pretty clearly, explaining why the signal might not be getting to our node apps.The scripts with
npminnode:22have 2 subprocess "hops", one fromnpmitself and another from the shell. Whereas withbashas default (/bin/sh) shell it will only create one subprocess which npm will relay the signal to.fix(ci): isolate chaos and demo into separate jobs; assert mock fresh…
fix(ci): isolate chaos and demo into separate jobs; assert mock fresh…
fix(scripts): the cleanup traps stop killing the messenger and start …