Skip to content

Commit 66f4e4e

Browse files
ronagTrott
authored andcommitted
stream: do not emit 'end' after 'error'
Refs: #6083 PR-URL: #31182 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 57a1ca9 commit 66f4e4e

7 files changed

+27
-14
lines changed

lib/_stream_readable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ function endReadableNT(state, stream) {
12151215
debug('endReadableNT', state.endEmitted, state.length);
12161216

12171217
// Check that we didn't get one last unshift.
1218-
if (!state.endEmitted && state.length === 0) {
1218+
if (!state.errorEmitted && !state.endEmitted && state.length === 0) {
12191219
state.endEmitted = true;
12201220
stream.readable = false;
12211221
stream.emit('end');

lib/internal/streams/destroy.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ function destroy(err, cb) {
77
const r = this._readableState;
88
const w = this._writableState;
99

10+
// TODO(ronag): readable & writable = false?
11+
1012
if (err) {
1113
if (w) {
1214
w.errored = true;
@@ -129,6 +131,8 @@ function errorOrDestroy(stream, err, sync) {
129131
const r = stream._readableState;
130132
const w = stream._writableState;
131133

134+
// TODO(ronag): readable & writable = false?
135+
132136
if ((r && r.autoDestroy) || (w && w.autoDestroy))
133137
stream.destroy(err);
134138
else if (err) {

test/parallel/test-http2-client-destroy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const Countdown = require('../common/countdown');
9595
});
9696

9797
req.resume();
98-
req.on('end', common.mustCall());
98+
req.on('end', common.mustNotCall());
9999
req.on('close', common.mustCall(() => server.close()));
100100
}));
101101
}

test/parallel/test-http2-client-stream-destroy-before-connect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ server.listen(0, common.mustCall(() => {
5050

5151
req.on('response', common.mustNotCall());
5252
req.resume();
53-
req.on('end', common.mustCall());
53+
req.on('end', common.mustNotCall());
5454
}));

test/parallel/test-http2-head-request.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const errCheck = common.expectsError({
1010
name: 'Error',
1111
code: 'ERR_STREAM_WRITE_AFTER_END',
1212
message: 'write after end'
13-
}, 2);
13+
}, 1);
1414

1515
const {
1616
HTTP2_HEADER_PATH,
@@ -41,12 +41,6 @@ server.listen(0, () => {
4141
[HTTP2_HEADER_PATH]: '/'
4242
});
4343

44-
// Because it is a HEAD request, the payload is meaningless. The
45-
// option.endStream flag is set automatically making the stream
46-
// non-writable.
47-
req.on('error', errCheck);
48-
req.write('data');
49-
5044
req.on('response', common.mustCall((headers, flags) => {
5145
assert.strictEqual(headers[HTTP2_HEADER_STATUS], 200);
5246
assert.strictEqual(flags, 5); // The end of stream flag is set
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { Readable } = require('stream');
5+
6+
{
7+
const r = new Readable({ read() {} });
8+
9+
r.on('end', common.mustNotCall());
10+
r.on('data', common.mustCall());
11+
r.on('error', common.mustCall());
12+
r.push('asd');
13+
r.push(null);
14+
r.destroy(new Error('kaboom'));
15+
}

test/parallel/test-stream-unshift-read-race.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ r._read = function(n) {
6868
};
6969

7070
function pushError() {
71+
r.unshift(Buffer.allocUnsafe(1));
72+
w.end();
73+
7174
assert.throws(() => {
7275
r.push(Buffer.allocUnsafe(1));
7376
}, {
@@ -85,10 +88,7 @@ w._write = function(chunk, encoding, cb) {
8588
cb();
8689
};
8790

88-
r.on('end', common.mustCall(function() {
89-
r.unshift(Buffer.allocUnsafe(1));
90-
w.end();
91-
}));
91+
r.on('end', common.mustNotCall());
9292

9393
r.on('readable', function() {
9494
let chunk;

0 commit comments

Comments
 (0)