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

stream: don't emit 'end' after 'error' #31182

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
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
2 changes: 1 addition & 1 deletion test/parallel/test-http2-client-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const Countdown = require('../common/countdown');
});

req.resume();
req.on('end', common.mustCall());
req.on('end', common.mustNotCall());
req.on('close', common.mustCall(() => server.close()));
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ server.listen(0, common.mustCall(() => {

req.on('response', common.mustNotCall());
req.resume();
req.on('end', common.mustCall());
req.on('end', common.mustNotCall());
}));
8 changes: 1 addition & 7 deletions test/parallel/test-http2-head-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const errCheck = common.expectsError({
name: 'Error',
code: 'ERR_STREAM_WRITE_AFTER_END',
message: 'write after end'
}, 2);
}, 1);

const {
HTTP2_HEADER_PATH,
Expand Down Expand Up @@ -41,12 +41,6 @@ server.listen(0, () => {
[HTTP2_HEADER_PATH]: '/'
});

// Because it is a HEAD request, the payload is meaningless. The
// option.endStream flag is set automatically making the stream
// non-writable.
req.on('error', errCheck);
req.write('data');

req.on('response', common.mustCall((headers, flags) => {
assert.strictEqual(headers[HTTP2_HEADER_STATUS], 200);
assert.strictEqual(flags, 5); // The end of stream flag is set
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'));
}
8 changes: 4 additions & 4 deletions test/parallel/test-stream-unshift-read-race.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ r._read = function(n) {
};

function pushError() {
r.unshift(Buffer.allocUnsafe(1));
w.end();

assert.throws(() => {
r.push(Buffer.allocUnsafe(1));
}, {
Expand All @@ -85,10 +88,7 @@ w._write = function(chunk, encoding, cb) {
cb();
};

r.on('end', common.mustCall(function() {
r.unshift(Buffer.allocUnsafe(1));
w.end();
}));
r.on('end', common.mustNotCall());

r.on('readable', function() {
let chunk;
Expand Down