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 finish after close #32933

Closed
wants to merge 6 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ function WritableState(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;
}

function resetBuffer(state) {
Expand Down Expand Up @@ -653,11 +657,10 @@ function finishMaybe(stream, state, sync) {

function finish(stream, state) {
state.pendingcb--;
if (state.errorEmitted)
// TODO (ronag): Unify with needFinish.
if (state.errorEmitted || state.closeEmitted)
return;

// TODO(ronag): This could occur after 'close' is emitted.

state.finished = true;
stream.emit('finish');

Expand Down
9 changes: 7 additions & 2 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ function emitCloseNT(self) {
const r = self._readableState;
const w = self._writableState;

if (w) {
w.closeEmitted = true;
}

if ((w && w.emitClose) || (r && r.emitClose)) {
self.emit('close');
}
Expand Down Expand Up @@ -111,15 +115,16 @@ function undestroy() {
}

if (w) {
w.closed = false;
w.destroyed = false;
w.closed = false;
w.closeEmitted = false;
w.errored = false;
w.errorEmitted = false;
w.ended = false;
w.ending = false;
w.finalCalled = false;
w.prefinished = false;
w.finished = false;
w.errorEmitted = false;
}
}

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

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

{
const w = new Writable({
write: common.mustCall((chunk, encoding, cb) => {
w.on('close', common.mustCall(() => {
cb();
}));
})
});

w.on('finish', common.mustNotCall());
w.end('asd');
w.destroy();
}

{
const w = new Writable({
write: common.mustCall((chunk, encoding, cb) => {
w.on('close', common.mustCall(() => {
cb();
w.end();
}));
})
});

w.on('finish', common.mustNotCall());
w.write('asd');
w.destroy();
}