Skip to content

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

@ryanrasti

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.

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