Skip to content

Commit 47dfb8b

Browse files
committed
fix: pipe stderr correctly
Fixes #1638 This also simplifies the shutdown process by sending the kill signal to the sub-process - thus ensuring we don't get the weird "User defined singal" from the shell. This commit also includes a test to ensure this doesn't happen again.
1 parent ed91703 commit 47dfb8b

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

Diff for: lib/monitor/run.js

+7-18
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ function run(options) {
3333
var stdio = ['pipe', 'pipe', 'pipe'];
3434

3535
if (config.options.stdout) {
36-
stdio = ['pipe', process.stdout, 'pipe'];
36+
stdio = ['pipe', process.stdout, process.stderr];
3737
}
3838

3939
if (config.options.stdin === false) {
40-
stdio = [process.stdin, process.stdout, 'pipe'];
40+
stdio = [process.stdin, process.stdout, process.stderr];
4141
}
4242

4343
var sh = 'sh';
@@ -99,6 +99,8 @@ function run(options) {
9999
if (shouldFork) {
100100
var forkArgs = cmd.args.slice(1);
101101
var env = utils.merge(options.execOptions.env, process.env);
102+
stdio.pop();
103+
stdio.push(process.stderr);
102104
stdio.push('ipc');
103105
child = fork(options.execOptions.script, forkArgs, {
104106
env: env,
@@ -110,7 +112,7 @@ function run(options) {
110112
} else {
111113
utils.log.detail('spawning');
112114
child = spawn.apply(null, spawnArgs);
113-
debug('spawn', sh, shFlag, args)
115+
debug('spawn', sh, shFlag, args);
114116
}
115117

116118
if (config.required) {
@@ -139,20 +141,6 @@ function run(options) {
139141
bus.emit('message', message, sendHandle);
140142
});
141143
}
142-
} else { // else if not required…
143-
144-
// this swallows a shell message that happens because we kill the sh -c
145-
// more details here: https://git.io/Je6d0
146-
child.stderr.on('data', s => {
147-
s = s.toString();
148-
149-
if (child.__nodemonRestart) { // this flag is set right before the kill
150-
utils.log.detail('stderr: ' + s);
151-
return;
152-
}
153-
154-
process.stderr.write(s);
155-
});
156144
}
157145

158146
bus.emit('start');
@@ -276,7 +264,6 @@ function run(options) {
276264
/* Now kill the entire subtree of processes belonging to nodemon */
277265
var oldPid = child.pid;
278266
if (child) {
279-
child.__nodemonRestart = true;
280267
kill(child, config.signal, function () {
281268
// this seems to fix the 0.11.x issue with the "rs" restart command,
282269
// though I'm unsure why. it seems like more data is streamed in to
@@ -380,6 +367,8 @@ function kill(child, signal, callback) {
380367
// the sub processes need to be killed from smallest to largest
381368
debug('sending kill signal to ' + pids.join(', '));
382369

370+
child.kill(signal);
371+
383372
pids.sort().forEach(pid => exec(`kill -${sig} ${pid}`, noop));
384373

385374
waitForSubProcesses(child.pid, () => {

Diff for: test/fork/run.test.js

+30-4
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,45 @@ var assert = require('assert'),
55
run = utils.run;
66

77
describe('nodemon fork', function () {
8-
it('should start a fork', function (done) {
9-
var p = run(appjs, {
8+
it('should not show user-signal', done => {
9+
var p = run({ exec: 'bin/nodemon.js',
10+
args: [ '-V', '-x', 'echo running && sleep 20' ] }, {
1011
error: function (data) {
1112
p.send('quit');
13+
if (data.trim().indexOf('signal') !== -1) {
14+
return done(new Error('Signal incorrectly shown'));
15+
}
16+
1217
done(new Error(data));
18+
},
19+
output: function (data) {
20+
if (data.trim().indexOf('signal') !== -1) {
21+
done(new Error('Signal incorrectly shown'));
22+
}
1323
}
1424
});
1525

26+
let started = false;
1627
p.on('message', function (event) {
1728
if (event.type === 'start') {
29+
if (!started) {
30+
p.send('restart');
31+
started = true;
32+
} else {
33+
p.send('quit');
34+
assert(true, 'nodemon started');
35+
done();
36+
}
37+
38+
}
39+
});
40+
});
41+
42+
it('should start a fork', function (done) {
43+
var p = run(appjs, {
44+
error: function (data) {
1845
p.send('quit');
19-
assert(true, 'nodemon started');
20-
done();
46+
done(new Error(data));
2147
}
2248
});
2349
});

0 commit comments

Comments
 (0)