Skip to content

Commit 931408e

Browse files
mscdexMylesBorins
authored andcommitted
Revert "stream: prevent 'end' to be emitted after 'error'"
This reverts commit 0857790. PR-URL: #20449 Fixes: #20334 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 38323bc commit 931408e

17 files changed

+20
-76
lines changed

lib/_stream_readable.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ function ReadableState(options, stream, isDuplex) {
9999
this.endEmitted = false;
100100
this.reading = false;
101101

102-
// Flipped if an 'error' is emitted.
103-
this.errorEmitted = false;
104-
105102
// a flag to be able to tell if the event 'readable'/'data' is emitted
106103
// immediately, or on a later tick. We set this to true at first, because
107104
// any actions that shouldn't happen until "later" should generally also
@@ -1072,23 +1069,20 @@ function fromList(n, state) {
10721069
function endReadable(stream) {
10731070
var state = stream._readableState;
10741071

1075-
debug('endReadable', state.endEmitted, state.errorEmitted);
1076-
if (!state.endEmitted && !state.errorEmitted) {
1072+
debug('endReadable', state.endEmitted);
1073+
if (!state.endEmitted) {
10771074
state.ended = true;
10781075
process.nextTick(endReadableNT, state, stream);
10791076
}
10801077
}
10811078

10821079
function endReadableNT(state, stream) {
1083-
debug('endReadableNT', state.endEmitted, state.length, state.errorEmitted);
1080+
debug('endReadableNT', state.endEmitted, state.length);
10841081

10851082
// Check that we didn't get one last unshift.
10861083
if (!state.endEmitted && state.length === 0) {
1084+
state.endEmitted = true;
10871085
stream.readable = false;
1088-
1089-
if (!state.errorEmitted) {
1090-
state.endEmitted = true;
1091-
stream.emit('end');
1092-
}
1086+
stream.emit('end');
10931087
}
10941088
}

lib/_stream_writable.js

-10
Original file line numberDiff line numberDiff line change
@@ -424,22 +424,12 @@ function onwriteError(stream, state, sync, er, cb) {
424424
// this can emit finish, and it will always happen
425425
// after error
426426
process.nextTick(finishMaybe, stream, state);
427-
428-
// needed for duplex, fixes https://github.com/nodejs/node/issues/6083
429-
if (stream._readableState) {
430-
stream._readableState.errorEmitted = true;
431-
}
432427
stream._writableState.errorEmitted = true;
433428
stream.emit('error', er);
434429
} else {
435430
// the caller expect this to happen before if
436431
// it is async
437432
cb(er);
438-
439-
// needed for duplex, fixes https://github.com/nodejs/node/issues/6083
440-
if (stream._readableState) {
441-
stream._readableState.errorEmitted = true;
442-
}
443433
stream._writableState.errorEmitted = true;
444434
stream.emit('error', er);
445435
// this can emit finish, but finish must

lib/internal/streams/destroy.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@ function destroy(err, cb) {
88
this._writableState.destroyed;
99

1010
if (readableDestroyed || writableDestroyed) {
11-
const readableErrored = this._readableState &&
12-
this._readableState.errorEmitted;
13-
const writableErrored = this._writableState &&
14-
this._writableState.errorEmitted;
15-
1611
if (cb) {
1712
cb(err);
18-
} else if (err && !readableErrored && !writableErrored) {
13+
} else if (err &&
14+
(!this._writableState || !this._writableState.errorEmitted)) {
1915
process.nextTick(emitErrorNT, this, err);
2016
}
2117
return this;
@@ -36,11 +32,6 @@ function destroy(err, cb) {
3632
this._destroy(err || null, (err) => {
3733
if (!cb && err) {
3834
process.nextTick(emitErrorAndCloseNT, this, err);
39-
40-
if (this._readableState) {
41-
this._readableState.errorEmitted = true;
42-
}
43-
4435
if (this._writableState) {
4536
this._writableState.errorEmitted = true;
4637
}
@@ -74,7 +65,6 @@ function undestroy() {
7465
this._readableState.reading = false;
7566
this._readableState.ended = false;
7667
this._readableState.endEmitted = false;
77-
this._readableState.errorEmitted = false;
7868
}
7969

8070
if (this._writableState) {

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

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const Countdown = require('../common/countdown');
9595
});
9696

9797
req.resume();
98+
req.on('end', common.mustCall());
9899
req.on('close', common.mustCall(() => server.close()));
99100
}));
100101
}

test/parallel/test-http2-client-onconnect-errors.js

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ function runTest(test) {
101101
});
102102
}
103103

104+
req.on('end', common.mustCall());
104105
req.on('close', common.mustCall(() => {
105106
client.destroy();
106107

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

+1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ server.listen(0, common.mustCall(() => {
4545

4646
req.on('response', common.mustNotCall());
4747
req.resume();
48+
req.on('end', common.mustCall());
4849
}));

test/parallel/test-http2-compat-serverresponse-destroy.js

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ server.listen(0, common.mustCall(() => {
6363
req.on('close', common.mustCall(() => countdown.dec()));
6464

6565
req.resume();
66+
req.on('end', common.mustCall());
6667
}
6768

6869
{
@@ -77,5 +78,6 @@ server.listen(0, common.mustCall(() => {
7778
req.on('close', common.mustCall(() => countdown.dec()));
7879

7980
req.resume();
81+
req.on('end', common.mustCall());
8082
}
8183
}));

test/parallel/test-http2-max-concurrent-streams.js

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ server.listen(0, common.mustCall(() => {
4545
req.on('aborted', common.mustCall());
4646
req.on('response', common.mustNotCall());
4747
req.resume();
48+
req.on('end', common.mustCall());
4849
req.on('close', common.mustCall(() => countdown.dec()));
4950
req.on('error', common.expectsError({
5051
code: 'ERR_HTTP2_STREAM_ERROR',

test/parallel/test-http2-misused-pseudoheaders.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ server.listen(0, common.mustCall(() => {
4141

4242
req.on('response', common.mustCall());
4343
req.resume();
44+
req.on('end', common.mustCall());
4445
req.on('close', common.mustCall(() => {
4546
server.close();
4647
client.close();

test/parallel/test-http2-multi-content-length.js

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ server.listen(0, common.mustCall(() => {
5353
// header to be set for non-payload bearing requests...
5454
const req = client.request({ 'content-length': 1 });
5555
req.resume();
56+
req.on('end', common.mustCall());
5657
req.on('close', common.mustCall(() => countdown.dec()));
5758
req.on('error', common.expectsError({
5859
code: 'ERR_HTTP2_STREAM_ERROR',

test/parallel/test-http2-respond-file-fd-invalid.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ server.listen(0, () => {
4040
req.on('response', common.mustCall());
4141
req.on('error', common.mustCall(errorCheck));
4242
req.on('data', common.mustNotCall());
43-
req.on('close', common.mustCall(() => {
43+
req.on('end', common.mustCall(() => {
4444
assert.strictEqual(req.rstCode, NGHTTP2_INTERNAL_ERROR);
4545
client.close();
4646
server.close();

test/parallel/test-http2-respond-nghttperrors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function runTest(test) {
8787
req.resume();
8888
req.end();
8989

90-
req.on('close', common.mustCall(() => {
90+
req.on('end', common.mustCall(() => {
9191
client.close();
9292

9393
if (!tests.length) {

test/parallel/test-http2-respond-with-fd-errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function runTest(test) {
9595
req.resume();
9696
req.end();
9797

98-
req.on('close', common.mustCall(() => {
98+
req.on('end', common.mustCall(() => {
9999
client.close();
100100

101101
if (!tests.length) {

test/parallel/test-http2-server-shutdown-before-respond.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ server.on('listening', common.mustCall(() => {
3232
}));
3333
req.resume();
3434
req.on('data', common.mustNotCall());
35-
req.on('close', common.mustCall(() => server.close()));
35+
req.on('end', common.mustCall(() => server.close()));
3636
}));

test/parallel/test-http2-server-socket-destroy.js

+1
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ server.on('listening', common.mustCall(() => {
5252

5353
req.on('aborted', common.mustCall());
5454
req.resume();
55+
req.on('end', common.mustCall());
5556
}));

test/parallel/test-stream-duplex-error-write.js

-24
This file was deleted.

test/parallel/test-stream-readable-destroy.js

-15
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,3 @@ const { inherits } = require('util');
189189
read.push('hi');
190190
read.on('data', common.mustNotCall());
191191
}
192-
193-
{
194-
// double error case
195-
const read = new Readable({
196-
read() {}
197-
});
198-
199-
read.on('close', common.mustCall());
200-
read.on('error', common.mustCall());
201-
202-
read.destroy(new Error('kaboom 1'));
203-
read.destroy(new Error('kaboom 2'));
204-
assert.strictEqual(read._readableState.errorEmitted, true);
205-
assert.strictEqual(read.destroyed, true);
206-
}

0 commit comments

Comments
 (0)