Skip to content
This repository was archived by the owner on Aug 11, 2022. It is now read-only.
This repository was archived by the owner on Aug 11, 2022. It is now read-only.

passing unix signals to child rather than dying? #4603

@evantahler

Description

@evantahler

Here's some interesting behavior I've seen when running an application which does some slower shutdown operations when receiving a termination signal.

When running npm start in a *nix terminal (tested on ubuntu and OSX), the SIGINT (ctrl+c) command is passed from the parent npm process to the child (my running application). npm instantly exits, and then my application handles the passed-down signal and eventually exits. However, because the stdout/err of the session was managed by npm, strange things can happen if my application still writes (or console.outs).

At the very least, the terminal which started the application will interpret the npm process' death as a return and draw the next line. However, the child's stdout is still bound to that session. This means you can start another application in that same terminal and still get the first app's messages intermingled. Or, more strangely, if you then close that terminal session, the underlying app which is still running and possibly trying to write to stdout is likely to crash on the next console.log.

You can emulate this pretty simply:

package.json

{
  "name": "test",
  "scripts": {
    "start": "node ./app.js"
  }
}

app.js

process.on('SIGINT',  function(){ console.log("SIGINT");  shutDown() });
process.on('SIGTERM', function(){ console.log("SIGTERM"); shutDown() });

var string = ".";

var shutDown = function(){
  console.log("off-ing...");
  string = "x";

  setTimeout(function(){
    console.log("bye!");
    process.exit();
  }, 1000 * 60);
}

setInterval(function(){
  console.log(string);
}, 500)
  1. start app.js with npm start
  2. ctrl+c in the window you started npm with. You will see npm exit, but still receive messages from app.js.
  3. have fun opening vi or some other application in the same window.

I think a possible solution to this would to have npm catch (process.on('x')) and not actually exit until the child process exits and pass those commands along to the child... but I'm sure that will cause all sorts of other problems.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions