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

Spawn children in their own process groups, and deliver SIGINT to each group #177

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/proc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

var process = require('process');
var prog = require('child_process');

var cons = require('./console').Console;
Expand All @@ -28,7 +29,10 @@ function run(key, proc, emitter) {
file = '/bin/sh';
args = ['-c', proc.command];
}
var child = prog.spawn(file, args, { env: proc.env });
var child = prog.spawn(file, args, {
env: proc.env,
detached: true,
});
var killallReceived = false;

child.stdout.on('data', function(data) {
Expand Down Expand Up @@ -59,7 +63,11 @@ function run(key, proc, emitter) {
killallReceived = true;

try {
child.kill(signal);
if (platform === 'win32') {
child.kill(signal);
} else {
process.kill(-child.pid, signal);
}
}
catch (err) {
if (err.code === 'EPERM') {
Expand Down
6 changes: 5 additions & 1 deletion nf.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ var calculatePadding = _requirements.calculatePadding;
var startProxies = require('./lib/proxy').startProxies;
var startForward = require('./lib/forward').startForward;

var interrupted = false;

// Kill All Child Processes on SIGINT
process.once('SIGINT', function() {
process.on('SIGINT', function() {
if (interrupted) return;
interrupted = true;
display.Warn('Interrupted by User');
emitter.emit('killall', 'SIGINT');
});
Expand Down
16 changes: 16 additions & 0 deletions test/process-groups.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

NF="node ../nf.js"

rm -rf sandbox
mkdir -p sandbox

cat << EOF > sandbox/Procfile
a: sleep 1
b: /bin/sh -c "sleep 10000 && exit 1"
EOF

# We'll exit with a code that cannot be used as a signal. This exposes a
# potential bug in termination handling.
$NF --procfile sandbox/Procfile start >sandbox/groups.txt 2>&1 && exit 0
exit 1