From 68a292188fd7b4bf3fe7a958356718591d3744e2 Mon Sep 17 00:00:00 2001 From: Ben Weintraub Date: Sun, 3 Dec 2023 21:45:43 -0800 Subject: [PATCH] Create process group for each child + subprocesses (fixes #176) Signed-off-by: Ben Weintraub --- lib/proc.js | 12 ++++++++++-- test/process-groups.test.sh | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100755 test/process-groups.test.sh diff --git a/lib/proc.js b/lib/proc.js index 846887c..20a2782 100644 --- a/lib/proc.js +++ b/lib/proc.js @@ -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; @@ -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) { @@ -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') { diff --git a/test/process-groups.test.sh b/test/process-groups.test.sh new file mode 100755 index 0000000..f25a778 --- /dev/null +++ b/test/process-groups.test.sh @@ -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