From d6ad480c406fac17d00b6a12a1eed21ddd072b1a Mon Sep 17 00:00:00 2001 From: jbenet Date: Fri, 16 Sep 2016 00:04:08 -0400 Subject: [PATCH] fix(shutdown): fixed bugs in stopDaemon - bug1: local var for the closure (this.subprocess set to null) - bug2: order problem: should set .on('close') handler before .kill() --- src/node.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/node.js b/src/node.js index 499acb7d..2396a5f4 100644 --- a/src/node.js +++ b/src/node.js @@ -151,18 +151,19 @@ module.exports = class Node { if (!done) done = () => {} if (!this.subprocess) return done(null) - this.subprocess.kill('SIGTERM') - + // need a local var for the closure, as we clear the var. + const subprocess = this.subprocess const timeout = setTimeout(() => { - this.subprocess.kill('SIGKILL') + subprocess.kill('SIGKILL') done(null) }, GRACE_PERIOD) - this.subprocess.on('close', () => { + subprocess.on('close', () => { clearTimeout(timeout) done(null) }) + subprocess.kill('SIGTERM') this.subprocess = null }