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

Remove exit handler on exit #129

Merged
merged 5 commits into from
Mar 6, 2018
Merged
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
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,15 @@ module.exports = (cmd, args, opts) => {
let timeoutId = null;
let timedOut = false;

const cleanupTimeout = () => {
const cleanup = () => {
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
}

if (removeExitHandler) {
removeExitHandler();
}
};

if (parsed.opts.timeout > 0) {
Expand All @@ -233,18 +237,18 @@ module.exports = (cmd, args, opts) => {

const processDone = new Promise(resolve => {
spawned.on('exit', (code, signal) => {
cleanupTimeout();
cleanup();
resolve({code, signal});
});

spawned.on('error', err => {
cleanupTimeout();
cleanup();
resolve({error: err});
});

if (spawned.stdin) {
spawned.stdin.on('error', err => {
cleanupTimeout();
cleanup();
resolve({error: err});
});
}
Expand All @@ -269,10 +273,6 @@ module.exports = (cmd, args, opts) => {
result.stdout = arr[1];
result.stderr = arr[2];

if (removeExitHandler) {
removeExitHandler();
}

if (result.error || result.code !== 0 || result.signal !== null) {
const err = makeError(result, {
joinedCmd,
Expand Down
17 changes: 17 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,3 +507,20 @@ test('detach child process', async t => {

t.is(fs.readFileSync(file, 'utf8'), 'foo\n');
});

// See #128
test('removes exit handler on exit', async t => {
// FIXME: This relies on `signal-exit` internals
const ee = process.__signal_exit_emitter__;

const child = m('noop');
const listener = ee.listeners('exit').pop();

await new Promise((resolve, reject) => {
child.on('error', reject);
child.on('exit', resolve);
});

const included = ee.listeners('exit').indexOf(listener) !== -1;
t.false(included);
});