Skip to content

[BUG] npm not handling process signals #6684

Open

Description

@ryanrasti

Is there an existing issue for this?

  • 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

  • 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.

Activity

  1. added
    Bugthing that needs fixing
    Needs Triageneeds review for next steps
    Release 9.xwork is associated with a specific npm 9 release
    on Jul 28, 2023
  2. ngbrown commented on Jul 28, 2023

    @ngbrown

    @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?

    cc: @nlf, @wraithgar

  3. ryanrasti commented on Jul 29, 2023

    @ryanrasti
    Author

    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.x

  4. thejohnfreeman commented on Aug 21, 2023

    @thejohnfreeman

    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
    
  5. dmaizel commented on Oct 18, 2023

    @dmaizel

    Any news about this?

  6. tdislay commented on Feb 7, 2024

    @tdislay

    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 🎉

  7. mahnunchik commented on Feb 19, 2024

    @mahnunchik

    I've faced the same issue...

  8. DesignByOnyx commented on Mar 6, 2024

    @DesignByOnyx

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

  9. added a commit that references this issue on Mar 6, 2024
  10. grcode123 commented on Mar 28, 2024

    @grcode123

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

  11. Dzieni commented on Apr 24, 2024

    @Dzieni

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

  12. ekeren commented on Apr 24, 2024

    @ekeren

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

  13. skyrpex commented on Apr 24, 2024

    @skyrpex

    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.

  14. DesignByOnyx commented on Apr 24, 2024

    @DesignByOnyx

    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

  15. mahnunchik commented on Dec 7, 2024

    @mahnunchik

    Any news?

  16. ImSingee commented on Jan 2, 2025

    @ImSingee

    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)

  17. ImSingee commented on Jan 2, 2025

    @ImSingee

    And before waiting for a fix, using something like "start": "exec node index.js" can work

  18. filip-jaksic commented on May 14, 2025

    @filip-jaksic

    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.

  19. jtomlin12 commented on Nov 25, 2025

    @jtomlin12

    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.

    Image

    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.

  20. added
    Priority 2secondary priority issue
    and removed
    Needs Triageneeds review for next steps
    on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixingPriority 2secondary priority issueRelease 9.xwork is associated with a specific npm 9 release

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions