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: throw error if stream has been destroyed on _final and _write #41164

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 12 additions & 5 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,13 +493,17 @@ function afterWrite(stream, state, count, cb) {
stream.emit('drain');
}

while (count-- > 0) {
state.pendingcb--;
cb();
}

if (state.destroyed) {
while (count-- > 0) {
state.pendingcb--;
cb(new ERR_STREAM_DESTROYED('write'));
}
errorBuffer(state);
} else {
while (count-- > 0) {
state.pendingcb--;
cb();
}
}

finishMaybe(stream, state);
Expand Down Expand Up @@ -671,6 +675,9 @@ function callFinal(stream, state) {
called = true;

state.pendingcb--;
if (!err && state.destroyed) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We could move this into the if-else block below to make it a bit cleaner:

if (err) {
  // ...
} else if (state.destroyed) {
  // ...
} else if (needFinish(state)) {
  // ...
}

err = new ERR_STREAM_DESTROYED('final');
}
if (err) {
const onfinishCallbacks = state[kOnFinished].splice(0);
for (let i = 0; i < onfinishCallbacks.length; i++) {
Expand Down