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

Emit end event on streams when process fails #518

Merged
merged 2 commits into from Feb 11, 2023
Merged

Conversation

ehmicky
Copy link
Collaborator

@ehmicky ehmicky commented Feb 11, 2023

Fixes #517

When a process (or one of its standard streams) errors, we return what could have been buffered so far from stdout/stderr as error.stdout and error.stderr (which are also appended to error.message). To do this, we first destroy() those streams, then retrieve the buffered value from get-stream.

However, calling destroy() prevents the end event from being emitted on those streams. This behavior differs from how child_process.spawn() and might be unexpected.

This PR fixes this for the case where the buffer option is false. When that's the case, there is no need to destroy the streams since we do not need to do anything with them anymore. We can just let Node.js destroy them naturally as part of the child process exit.

@@ -173,6 +174,11 @@ test('buffer: false > promise rejects when process returns non-zero', async t =>
t.is(exitCode, 2);
});

test('buffer: false > emits end event when promise is rejected', async t => {
const subprocess = execa('wrong command', {buffer: false, reject: false});
await t.notThrowsAsync(Promise.all([subprocess, pEvent(subprocess.stdout, 'end')]));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the code fix above, the end event would never be emitted on subprocess.stdout in that test case.

@@ -36,7 +36,7 @@ export const makeAllStream = (spawned, {all}) => {

// On failure, `result.stdout|stderr|all` should contain the currently buffered stream
const getBufferedData = async (stream, streamPromise) => {
if (!stream) {
if (!stream || streamPromise === undefined) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A short code comment here would be great.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

@sindresorhus sindresorhus merged commit 30c7a7a into main Feb 11, 2023
@sindresorhus sindresorhus deleted the fix-end-event branch February 11, 2023 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Output streams do not emit "end" event if execution fails
2 participants