Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ryanrasti opened this issue Jul 27, 2023 · 3 comments
Closed

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

ryanrasti opened this issue Jul 27, 2023 · 3 comments
Labels
wrong repo Issues that should be opened in another repository.

Comments

@ryanrasti
Copy link

ryanrasti commented Jul 27, 2023

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.

@bnoordhuis
Copy link
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.

@aduh95
Copy link
Contributor

aduh95 commented Jul 29, 2023

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.

@aduh95 aduh95 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 29, 2023
@aduh95 aduh95 added the wrong repo Issues that should be opened in another repository. label Jul 29, 2023
@richardlau
Copy link
Member

Adding links.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wrong repo Issues that should be opened in another repository.
Projects
None yet
Development

No branches or pull requests

4 participants