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 close #33076

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ function ReadableState(options, stream, isDuplex) {
// Indicates whether the stream has finished destroying.
this.closed = false;

// True if close has been emitted or would have been emitted
// depending on emitClose.
this.closeEmitted = false;

// Crypto is kind of old and crusty. Historically, its default string
// encoding is 'binary' so we have to make this configurable.
// Everything else in the universe uses 'utf8', though.
Expand Down Expand Up @@ -1216,7 +1220,8 @@ function endReadableNT(state, stream) {
debug('endReadableNT', state.endEmitted, state.length);

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

Expand Down
6 changes: 5 additions & 1 deletion lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ function emitCloseNT(self) {
if (w) {
w.closeEmitted = true;
}
if (r) {
r.closeEmitted = true;
}

if ((w && w.emitClose) || (r && r.emitClose)) {
self.emit('close');
Expand Down Expand Up @@ -106,12 +109,13 @@ function undestroy() {

if (r) {
r.closed = false;
r.closeEmitted = false;
r.destroyed = false;
r.errored = false;
r.errorEmitted = false;
r.reading = false;
r.ended = false;
r.endEmitted = false;
r.errorEmitted = false;
}

if (w) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-stream-duplex-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const assert = require('assert');

duplex.removeListener('end', fail);
duplex.removeListener('finish', fail);
duplex.on('end', common.mustCall());
duplex.on('end', common.mustNotCall());
Copy link
Member Author

Choose a reason for hiding this comment

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

NOTE, possible breaking change

duplex.on('finish', common.mustCall());
assert.strictEqual(duplex.destroyed, true);
}
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-stream-readable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const assert = require('assert');
read.destroy();

read.removeListener('end', fail);
read.on('end', common.mustCall());
read.on('end', common.mustNotCall());
Copy link
Member Author

Choose a reason for hiding this comment

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

NOTE, possible breaking change

assert.strictEqual(read.destroyed, true);
}

Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-stream-readable-end-destroyed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

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

{
// Don't emit 'end' after 'close'.

const r = new Readable();

r.on('end', common.mustNotCall());
r.resume();
r.destroy();
r.on('close', common.mustCall(() => {
r.push(null);
}));
}