[Pkg-javascript-commits] [node-foreground-child] 05/69: Use signal-exit, fix kill(process.pid) race

Bastien Roucariès rouca at moszumanska.debian.org
Fri Aug 25 11:43:01 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-foreground-child.

commit 14b154835b3413b0c48484f6846e505946a3dcf6
Author: isaacs <i at izs.me>
Date:   Sat May 23 13:15:05 2015 -0700

    Use signal-exit, fix kill(process.pid) race
---
 index.js      | 80 +++++++++++------------------------------------------------
 package.json  |  4 ++-
 test/basic.js |  3 ++-
 3 files changed, 20 insertions(+), 67 deletions(-)

diff --git a/index.js b/index.js
index 37114f1..e7e5d0a 100644
--- a/index.js
+++ b/index.js
@@ -1,3 +1,6 @@
+var signalExit = require('signal-exit')
+var spawn = require('child_process').spawn
+
 module.exports = function (program, args) {
   if (Array.isArray(program)) {
     args = program.slice(1)
@@ -6,79 +9,26 @@ module.exports = function (program, args) {
     args = [].slice.call(arguments, 1)
   }
 
-  var child = require('child_process').spawn(
-    program,
-    args,
-    { stdio: 'inherit' }
-  )
-
-  signals.forEach(function (sig) {
-    try {
-      process.on(sig, function () {
-        child.kill(sig)
-      })
-    } catch (er) {}
-  })
+  var child = spawn(program, args, { stdio: 'inherit' })
 
-  process.once('exit', function (code) {
-    emittedExit = true
-    child.kill('SIGHUP')
+  var childExited = false
+  signalExit(function (code, signal) {
+    child.kill(signal || 'SIGHUP')
   })
 
-  var emittedExit = false
-
   child.on('close', function (code, signal) {
+    childExited = true
     if (signal) {
-      process.removeAllListeners(signal)
-      if (!emittedExit) {
-        process.emit('exit', code)
-      }
+      // If there is nothing else keeping the event loop alive,
+      // then there's a race between a graceful exit and getting
+      // the signal to this process.  Put this timeout here to
+      // make sure we're still alive to get the signal, and thus
+      // exit with the intended signal code.
+      setTimeout(function () {}, 200)
       process.kill(process.pid, signal)
-    } else {
+    } else
       process.exit(code)
-    }
   })
 
   return child
 }
-
-var signals = [
-  'SIGABRT',
-  'SIGALRM',
-  'SIGBUS',
-  'SIGCHLD',
-  'SIGCLD',
-  'SIGCONT',
-  'SIGEMT',
-  'SIGFPE',
-  'SIGHUP',
-  'SIGILL',
-  'SIGINFO',
-  'SIGINT',
-  'SIGIO',
-  'SIGIOT',
-  'SIGKILL',
-  'SIGLOST',
-  'SIGPIPE',
-  'SIGPOLL',
-  'SIGPROF',
-  'SIGPWR',
-  'SIGQUIT',
-  'SIGSEGV',
-  'SIGSTKFLT',
-  'SIGSTOP',
-  'SIGSYS',
-  'SIGTERM',
-  'SIGTRAP',
-  'SIGTSTP',
-  'SIGTTIN',
-  'SIGTTOU',
-  'SIGUNUSED',
-  'SIGURG',
-  'SIGUSR1',
-  'SIGUSR2',
-  'SIGVTALRM',
-  'SIGWINCH',
-  'SIGXCPU',
-  'SIGXFSZ'
-]
diff --git a/package.json b/package.json
index 36f0f2f..9f3a7d9 100644
--- a/package.json
+++ b/package.json
@@ -6,7 +6,9 @@
   "directories": {
     "test": "test"
   },
-  "dependencies": {},
+  "dependencies": {
+    "signal-exit": "^2.0.0"
+  },
   "devDependencies": {
     "tap": "^1.0.4"
   },
diff --git a/test/basic.js b/test/basic.js
index 6713aff..40a68cc 100644
--- a/test/basic.js
+++ b/test/basic.js
@@ -1,5 +1,6 @@
 var fg = require('../index.js')
 var spawn = require('child_process').spawn
+var signalExit = require('signal-exit')
 
 if (process.argv[2] === 'child') {
   console.log('stdout')
@@ -26,7 +27,7 @@ if (process.argv[2] === 'parent') {
   var child = fg(program, args)
 
   if (process.argv[3] === 'signalexit') {
-    process.on('exit', function () {
+    signalExit(function (code, signal) {
       console.log('parent exit')
     })
     switch (process.argv[4]) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-foreground-child.git



More information about the Pkg-javascript-commits mailing list