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: prevent 'end' to be emitted after 'error' #20104

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 11 additions & 5 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ function ReadableState(options, stream, isDuplex) {
this.endEmitted = false;
this.reading = false;

// Flipped if an 'error' is emitted.
this.errorEmitted = false;

// a flag to be able to tell if the event 'readable'/'data' is emitted
// immediately, or on a later tick. We set this to true at first, because
// any actions that shouldn't happen until "later" should generally also
Expand Down Expand Up @@ -1069,20 +1072,23 @@ function fromList(n, state) {
function endReadable(stream) {
var state = stream._readableState;

debug('endReadable', state.endEmitted);
if (!state.endEmitted) {
debug('endReadable', state.endEmitted, state.errorEmitted);
if (!state.endEmitted && !state.errorEmitted) {
state.ended = true;
process.nextTick(endReadableNT, state, stream);
}
}

function endReadableNT(state, stream) {
debug('endReadableNT', state.endEmitted, state.length);
debug('endReadableNT', state.endEmitted, state.length, state.errorEmitted);

// Check that we didn't get one last unshift.
if (!state.endEmitted && state.length === 0) {
state.endEmitted = true;
stream.readable = false;
stream.emit('end');

if (!state.errorEmitted) {
state.endEmitted = true;
stream.emit('end');
}
}
}
10 changes: 10 additions & 0 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,22 @@ function onwriteError(stream, state, sync, er, cb) {
// this can emit finish, and it will always happen
// after error
process.nextTick(finishMaybe, stream, state);

// needed for duplex, fixes https://github.com/nodejs/node/issues/6083
if (stream._readableState) {
Copy link
Member

Choose a reason for hiding this comment

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

I haven't looked in enough detail but I guess there's no way to do this in _stream_duplex.js instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

My goal would be to change this into stream.destroy(err). But that's a more massive change, see #20096.

Copy link
Member

@lpinca lpinca Apr 17, 2018

Choose a reason for hiding this comment

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

Nit: can't we only use one of these and put it outside the wrapper if instead of duplicating it?

Copy link
Member Author

Choose a reason for hiding this comment

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

we can't as the two branches are significantly different. However we can move it in its own function. Should I do that?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm why, in this branch there are only two next tick calls so it's not a problem, in the else below there is only cb(er); before but I think it's not necessary to call the callback before setting the flag. I mean something like this:

diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js
index d21daf0541..04b344bc26 100644
--- a/lib/_stream_writable.js
+++ b/lib/_stream_writable.js
@@ -417,6 +417,13 @@ function doWrite(stream, state, writev, len, chunk, encoding, cb) {
 function onwriteError(stream, state, sync, er, cb) {
   --state.pendingcb;
 
+  // needed for duplex, fixes https://github.com/nodejs/node/issues/6083
+  if (stream._readableState) {
+    stream._readableState.errorEmitted = true;
+  }
+
+  stream._writableState.errorEmitted = true;
+
   if (sync) {
     // defer the callback if we are being called synchronously
     // to avoid piling up things on the stack
@@ -424,13 +431,11 @@ function onwriteError(stream, state, sync, er, cb) {
     // this can emit finish, and it will always happen
     // after error
     process.nextTick(finishMaybe, stream, state);
-    stream._writableState.errorEmitted = true;
     stream.emit('error', er);
   } else {
     // the caller expect this to happen before if
     // it is async
     cb(er);
-    stream._writableState.errorEmitted = true;
     stream.emit('error', er);
     // this can emit finish, but finish must
     // always follow error

but I didn't test it. If it doesn't work or if I am missing something, ignore me.

Copy link
Member Author

Choose a reason for hiding this comment

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

In the second block, you are setting errorEmitted before calling the callback. I remember spending a significant amount of time on this block of code, and these two paths should remain separated. It's something I'd prefer not to refactor in this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Yes stream._writableState.errorEmitted = true can stay as is if the user provided callback needs to find it set to false (why?) but I see no reason to not move the other part. There can't be existing code that relies on new code.

Anyway it's a just a nit and it can be ignored.

stream._readableState.errorEmitted = true;
}
stream._writableState.errorEmitted = true;
stream.emit('error', er);
} else {
// the caller expect this to happen before if
// it is async
cb(er);

// needed for duplex, fixes https://github.com/nodejs/node/issues/6083
if (stream._readableState) {
stream._readableState.errorEmitted = true;
}
stream._writableState.errorEmitted = true;
stream.emit('error', er);
// this can emit finish, but finish must
Expand Down
14 changes: 12 additions & 2 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ function destroy(err, cb) {
this._writableState.destroyed;

if (readableDestroyed || writableDestroyed) {
const readableErrored = this._readableState &&
this._readableState.errorEmitted;
const writableErrored = this._writableState &&
this._writableState.errorEmitted;

if (cb) {
cb(err);
} else if (err &&
(!this._writableState || !this._writableState.errorEmitted)) {
} else if (err && !(readableErrored || writableErrored)) {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: maybe it's just me but I find it a lot easier to read if De Morgan is applied.

process.nextTick(emitErrorNT, this, err);
}
return this;
Expand All @@ -32,6 +36,11 @@ function destroy(err, cb) {
this._destroy(err || null, (err) => {
if (!cb && err) {
process.nextTick(emitErrorAndCloseNT, this, err);

if (this._readableState) {
this._readableState.errorEmitted = true;
}

if (this._writableState) {
this._writableState.errorEmitted = true;
}
Expand Down Expand Up @@ -65,6 +74,7 @@ function undestroy() {
this._readableState.reading = false;
this._readableState.ended = false;
this._readableState.endEmitted = false;
this._readableState.errorEmitted = false;
}

if (this._writableState) {
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-http2-client-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ const Countdown = require('../common/countdown');
});

req.resume();
req.on('end', common.mustCall());
req.on('close', common.mustCall(() => server.close()));
}));
}
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-http2-client-onconnect-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ function runTest(test) {
});
}

req.on('end', common.mustCall());
req.on('close', common.mustCall(() => {
client.destroy();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,4 @@ server.listen(0, common.mustCall(() => {

req.on('response', common.mustNotCall());
req.resume();
req.on('end', common.mustCall());
}));
2 changes: 0 additions & 2 deletions test/parallel/test-http2-compat-serverresponse-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ server.listen(0, common.mustCall(() => {
req.on('close', common.mustCall(() => countdown.dec()));

req.resume();
req.on('end', common.mustCall());
}

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

req.resume();
req.on('end', common.mustCall());
}
}));
1 change: 0 additions & 1 deletion test/parallel/test-http2-max-concurrent-streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ server.listen(0, common.mustCall(() => {
req.on('aborted', common.mustCall());
req.on('response', common.mustNotCall());
req.resume();
req.on('end', common.mustCall());
req.on('close', common.mustCall(() => countdown.dec()));
req.on('error', common.expectsError({
code: 'ERR_HTTP2_STREAM_ERROR',
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-http2-misused-pseudoheaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ server.listen(0, common.mustCall(() => {

req.on('response', common.mustCall());
req.resume();
req.on('end', common.mustCall());
req.on('close', common.mustCall(() => {
server.close();
client.close();
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-http2-multi-content-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ server.listen(0, common.mustCall(() => {
// header to be set for non-payload bearing requests...
const req = client.request({ 'content-length': 1 });
req.resume();
req.on('end', common.mustCall());
req.on('close', common.mustCall(() => countdown.dec()));
req.on('error', common.expectsError({
code: 'ERR_HTTP2_STREAM_ERROR',
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http2-respond-file-fd-invalid.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ server.listen(0, () => {
req.on('response', common.mustCall());
req.on('error', common.mustCall(errorCheck));
req.on('data', common.mustNotCall());
req.on('end', common.mustCall(() => {
req.on('close', common.mustCall(() => {
assert.strictEqual(req.rstCode, NGHTTP2_INTERNAL_ERROR);
client.close();
server.close();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http2-respond-nghttperrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function runTest(test) {
req.resume();
req.end();

req.on('end', common.mustCall(() => {
req.on('close', common.mustCall(() => {
client.close();

if (!tests.length) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http2-respond-with-fd-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function runTest(test) {
req.resume();
req.end();

req.on('end', common.mustCall(() => {
req.on('close', common.mustCall(() => {
client.close();

if (!tests.length) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http2-server-shutdown-before-respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ server.on('listening', common.mustCall(() => {
}));
req.resume();
req.on('data', common.mustNotCall());
req.on('end', common.mustCall(() => server.close()));
req.on('close', common.mustCall(() => server.close()));
}));
1 change: 0 additions & 1 deletion test/parallel/test-http2-server-socket-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,4 @@ server.on('listening', common.mustCall(() => {

req.on('aborted', common.mustCall());
req.resume();
req.on('end', common.mustCall());
}));
1 change: 1 addition & 0 deletions test/parallel/test-process-external-stdio-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ if (process.argv[2] === 'child') {
assert.strictEqual(signal, null);
}));

child.stderr.pipe(process.stderr);
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed?

child.stdout.destroy();
child.send('go');
}
24 changes: 24 additions & 0 deletions test/parallel/test-stream-duplex-error-write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

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

const duplex = new Duplex({
write(chunk, enc, cb) {
cb(new Error('kaboom'));
},
read() {
this.push(null);
}
});

duplex.on('error', common.mustCall(function() {
strictEqual(this._readableState.errorEmitted, true);
strictEqual(this._writableState.errorEmitted, true);
}));

duplex.on('end', common.mustNotCall());

duplex.end('hello');
duplex.on('data', function() {});
Copy link
Member

Choose a reason for hiding this comment

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

Nit: duplex.resume() is cleaner.

15 changes: 15 additions & 0 deletions test/parallel/test-stream-readable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,18 @@ const { inherits } = require('util');
read.push('hi');
read.on('data', common.mustNotCall());
}

{
// double error case
const read = new Readable({
read() {}
});

read.on('close', common.mustCall());
read.on('error', common.mustCall());

read.destroy(new Error('kaboom 1'));
read.destroy(new Error('kaboom 2'));
assert.strictEqual(read._readableState.errorEmitted, true);
assert.strictEqual(read.destroyed, true);
}