Skip to content

Commit

Permalink
Merge pull request #3 from AndreyBelym/fixes
Browse files Browse the repository at this point in the history
Unref timeout and proper disconnect
  • Loading branch information
AlexanderMoskovkin authored Sep 11, 2017
2 parents df63f4b + 5bb53d7 commit 235393b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = function (cliPath, opts) {
var cliProc = spawn(process.execPath, args, { stdio: [process.stdin, process.stdout, process.stderr, useShutdownMessage ? 'ipc' : null] });

cliProc.on('exit', function (code, signal) {
if (useShutdownMessage)
if (useShutdownMessage && process.disconnect)
process.disconnect();

process.on('exit', function () {
Expand All @@ -66,14 +66,16 @@ module.exports = function (cliPath, opts) {
});

process.on('SIGINT', function () {
function forceKill () {
cliProc.kill('SIGTERM');
}

if (useShutdownMessage)
cliProc.send('shutdown');
else
cliProc.kill('SIGINT');

setTimeout(function () {
cliProc.kill('SIGTERM');
}, forcedKillDelay);
setTimeout(forceKill, forcedKillDelay).unref();
});

if (useShutdownMessage) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bin-v8-flags-filter",
"version": "1.1.0",
"version": "1.1.1",
"description": "Filters out v8 flags for your Node.js CLIs.",
"main": "index.js",
"directories": {
Expand Down
3 changes: 2 additions & 1 deletion test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var path = require('path');
var filter = require('../');

var gracefulShutdown = process.argv.indexOf('--graceful-shutdown') > -1;
var noIPCTest = process.argv.indexOf('--no-ipc-test') > -1;

filter(path.join(__dirname, './actual-cli.js'), { useShutdownMessage: gracefulShutdown });
filter(path.join(__dirname, './actual-cli.js'), { useShutdownMessage: gracefulShutdown || noIPCTest });

26 changes: 26 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,29 @@ it('Should use shutdown message', function (done) {

cliProcess.send('shutdown');
});

it('[Regression] Should not abort if using shutdown message and no parent IPC was established', function (done) {
var args = [
path.join(__dirname, './cli.js'),
'--hey',
'--allow-natives-syntax',
'-t=yo',
'--trace-gc',
'--no-ipc-test'
];

var output = '';
var cliProcess = spawn(process.execPath, args, { stdio: 'pipe' });

cliProcess.stdout.on('data', function (data) {
output += data.toString();
});

cliProcess.on('exit', function (code) {
assert.equal(code, 0);
assert.ok(output.indexOf('$$$ARGS:["--hey","-t=yo","--no-ipc-test"]$$$') > -1);
assert.ok(output.indexOf('$$$ISSMI:true$$$') > -1);

done();
});
});

0 comments on commit 235393b

Please sign in to comment.