Skip to content

Commit

Permalink
Create process group for each child + subprocesses (fixes strongloop#176
Browse files Browse the repository at this point in the history
)

Signed-off-by: Ben Weintraub <[email protected]>
  • Loading branch information
benweint committed Dec 17, 2023
1 parent 782cf09 commit 68a2921
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
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
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

0 comments on commit 68a2921

Please sign in to comment.