Skip to content

Commit

Permalink
stream: don't emit 'end' after 'error'
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jan 3, 2020
1 parent 4bec6d1 commit 8ffa2e9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ function endReadableNT(state, stream) {
debug('endReadableNT', state.endEmitted, state.length);

// Check that we didn't get one last unshift.
if (!state.endEmitted && state.length === 0) {
if (!state.errorEmitted && !state.endEmitted && state.length === 0) {
state.endEmitted = true;
stream.readable = false;
stream.emit('end');
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ function destroy(err, cb) {
}
}

// TODO(ronag): readable & writable = false?

if ((w && w.destroyed) || (r && r.destroyed)) {
if (cb) {
cb(err);
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-stream-readable-error-end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const common = require('../common');
const { Readable } = require('stream');
const assert = require('assert');

{
// Ensure 'end' is not emitted after 'error'.
// This test is slightly more complicated than
// needed in order to better illustrate the invariant.

const r = new Readable({ read() {} });

let errorEmitted = false;

r.on('data', common.mustCall());
r.on('end', () => {
assert.strictEqual(!errorEmitted);
});
r.on('error', () => {
errorEmitted = true;
});
r.push('asd');
r.push(null);
r.destroy(new Error('kaboom'));
}

0 comments on commit 8ffa2e9

Please sign in to comment.