cli icon indicating copy to clipboard operation
cli copied to clipboard

[BUG] npm not handling process signals

Open ryanrasti opened this issue 1 year ago • 12 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

NOTE: I originally posted this issue on nodejs/node but was redirected here.

This issue exists in the latest npm version

  • [X] I am using the latest npm

Current Behavior

npm doesn't handle SIGTERM like it used to. I noticed this issue specifically when updating node from v20.2.0 to v20.3.0 (npm 9.6.7 to 9.8.1).

Expected Behavior

Expected that the below program exits with code 0 (i.e., npm is aborted by the signal and the script handles the signal as well)

Steps To Reproduce

The below script sends a SIGTERM to the npm process that spawned it. Run npm run test with:

In package.json

    {
      "name": "test",
      "version": "0.0.0",
      "type": "module",
      "scripts": {
        "test": "node test.js"
      }
    }

In test.js

    import { execSync } from 'child_process';

    process.on('SIGTERM', () => {
        console.log('Received SIGTERM. Exiting...');
        process.exit(0);
    });

    const output = execSync('ps -e | grep npm').toString().split(/\s+/).filter(Boolean);
    const pid = 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 avatar Jul 28 '23 21:07 ryanrasti

@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 https://github.com/npm/cli/issues/6547, but no triage occurred in the month since it was opened.

This may be caused by a change made in https://github.com/npm/run-script/pull/142. There were no tests associated with the changed behavior. Maybe an issue needs opened in that repository?

cc: @nlf, @wraithgar

ngbrown avatar Jul 28 '23 22:07 ngbrown

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 https://github.com/npm/cli/issues/6547 but that bug is incorrectly tagged with Release 8.x

ryanrasti avatar Jul 29 '23 03:07 ryanrasti

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:

npm start &
pid=$! # root process ID
gid=$(ps -o pgid= $pid) # process group ID
kill -SIGTERM -$gid

As a bonus, you can inspect the process tree with either the root process ID or the process group ID like this:

pstree -pT $pid
ps -jH -$gid

thejohnfreeman avatar Aug 21 '23 21:08 thejohnfreeman

Any news about this?

dmaizel avatar Oct 18 '23 18:10 dmaizel

Since [email protected] which depends of @npm/[email protected] (in which the regression was fixed), everything works fine for me :). Thanks :tada:

tdislay avatar Feb 07 '24 16:02 tdislay

I've faced the same issue...

mahnunchik avatar Feb 19 '24 02:02 mahnunchik

I can confirm that npm version 10.3.0 does forward the signals as expected.

DesignByOnyx avatar Mar 06 '24 08:03 DesignByOnyx

I think this is still happening on Ubuntu 20.04.6 LTS with node 18.20.0 and npm 10.5.0

alexrus123 avatar Mar 28 '24 16:03 alexrus123

We have tested 10.3.0 and 10.5.0 on Node 21.7.3 & MacOS Sonoma and unfortunately the problem is still there.

Dzieni avatar Apr 24 '24 09:04 Dzieni

I am able to reproduce on npm 10.3.0 and node v21.6.2, on macos 13.4

ekeren avatar Apr 24 '24 14:04 ekeren

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.

skyrpex avatar Apr 24 '24 15:04 skyrpex

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

DesignByOnyx avatar Apr 24 '24 15:04 DesignByOnyx