Skip to content

Commit 475fc7d

Browse files
authored
fix: Use to exit event instead of close (#13)
if child process has unclosed stdio, then the main process cannot resolve successfully
1 parent 09bb8cd commit 475fc7d

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ module.exports = function runScript(script, options, extraOptions) {
7676
reject(err);
7777
});
7878

79-
proc.on('close', code => {
80-
debug('proc emit close: %s', code);
79+
proc.on('exit', code => {
80+
debug('proc emit exit: %s', code);
8181
if (isEnd) return;
8282
isEnd = true;
8383
clearTimeout(timeoutTimer);
@@ -102,8 +102,8 @@ module.exports = function runScript(script, options, extraOptions) {
102102
return resolve(stdio);
103103
});
104104

105-
proc.on('exit', code => {
106-
debug('proc emit exit: %s', code);
105+
proc.on('close', code => {
106+
debug('proc emit close: %s', code);
107107
});
108108

109109
if (typeof extraOptions.timeout === 'number' && extraOptions.timeout > 0) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const runScript = require('../../');
5+
const argv = process.argv.slice(2);
6+
7+
(async () => {
8+
if (argv[0] === undefined) {
9+
runScript(`node ${path.join(__dirname, './child-process-with-unclosed-stdio.js')} child`);
10+
await new Promise(resolve => {
11+
setTimeout(resolve, 1000);
12+
});
13+
console.log('child finish');
14+
process.exit();
15+
} else if (argv[0] === 'child') {
16+
// eslint-disable-next-line no-constant-condition
17+
while (true) {
18+
console.log('grandChild running');
19+
await new Promise(resolve => {
20+
setTimeout(resolve, 1000);
21+
});
22+
}
23+
}
24+
})();

test/runscript.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ describe('runscript.test.js', () => {
170170
});
171171
});
172172

173+
it('should exit when child process has not closed stdio streams', () => {
174+
return runScript(`node ${path.join(__dirname, 'fixtures/child-process-with-unclosed-stdio.js')}`, {
175+
stdio: 'pipe',
176+
}).then(stdio => {
177+
assert(/child finish/.test(stdio.stdout.toString().trim()));
178+
});
179+
});
180+
173181
if (process.platform === 'win32') {
174182
it('should run relative path .\\node_modules\\.bin\\autod', () => {
175183
return runScript('.\\node_modules\\.bin\\autod -V', {

0 commit comments

Comments
 (0)