Sitelet https://github.com/nodejs/node/issues/48946
Skip to content

npm ignores process signals when bundled with node >=20.3.0 #48946

Description

@ryanrasti

Version

v20.5.0

Platform

Linux nixos 5.15.93 #1-NixOS SMP Thu Feb 9 10:26:48 UTC 2023 x86_64 GNU/Linux

Subsystem

npm

What steps will reproduce the bug?

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);

How often does it reproduce? Is there a required condition?

release >= 20.3.0: Always reproduces.
release <= 20.2.0: Can't reproduce.

What is the expected behavior? Why is that the expected behavior?

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

What do you see instead?

Program exits with code 1 (npm doesn't appear to receive the signal). npm itself also doesn't abort.

Additional information

All-in-one command to reproduce. Run nix-build repro.nix where repro.nix has content:

{ pkgs ? import
    (builtins.fetchTarball
      {
        name = "nixpkgs-23.11pre506668.af8cd5ded77";
        url = "https://releases.nixos.org/nixpkgs/nixpkgs-23.11pre506668.af8cd5ded77/nixexprs.tar.xz";
        sha256 = "sha256:0in8bgah6hz47lsa3ka2fslwks174maqdzy8mcmsj0q4wrv8h2s9";
      })
    { }
}:
let
  package = pkgs.writeText "package.json" ''
    {
      "name": "test",
      "version": "0.0.0",
      "type": "module",
      "scripts": {
        "test": "node test.js"
      }
    }
  '';
  script = pkgs.writeText "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)
    }, 1000);

  '';
in
pkgs.runCommand "test"
{
  buildInputs = [ pkgs.nodejs_20 pkgs.ps ];
} ''
  node --version
  cp ${package} package.json
  cp ${script} test.js
  npm run test | tee $out
''

To get the working version (node v20.2.0), replace the import statement above with:

import
    (builtins.fetchTarball
      {
        name = "nixpkgs-23.05pre476152.2362848adf8";
        url = "https://releases.nixos.org/nixpkgs/nixpkgs-23.11pre491680.31cd1b4afba/nixexprs.tar.xz";
        sha256 = "sha256:109h43svy4gzl9bnwsgdwiyghv23bf2bwya396khqscjqakd14md";
      })
    { }

This is the breaking nix commit.

Activity

  1. added a commit that references this issue on Jul 27, 2023
  2. bnoordhuis commented on Jul 28, 2023

    @bnoordhuis
    Member

    v20.3.0 contains an upgrade from npm 9.6.7 to 9.8.1 so chances are good it's a change in behavior in npm, not node. Further evidence: sending signals directly to the node process works as expected.

    Can you report this to upstream npm? It's not actionable for us.

  3. aduh95 commented on Jul 29, 2023

    @aduh95
    Contributor

    I'm going to close this as it appears to be a npm bug, not a node one, let me know if I'm missing something and should re-open this.

  4. added
    wrong repoIssues that should be opened in another repository.
    on Jul 29, 2023
  5. richardlau commented on Jan 23, 2024

    @richardlau
    Member

    Adding links.

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

    wrong repoIssues that should be opened in another repository.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions