Skip to content

Commit

Permalink
stream: 'error' should be emitted asynchronously
Browse files Browse the repository at this point in the history
errorOrDestroy emits 'error' synchronously due to
compat reasons. However, it should be possible to
use correct async behaviour for new code.
  • Loading branch information
ronag committed Sep 28, 2019
1 parent 35bfe0e commit 4557b48
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ Writable.prototype.write = function(chunk, encoding, cb) {
} else if (state.destroyed) {
const err = new ERR_STREAM_DESTROYED('write');
process.nextTick(cb, err);
errorOrDestroy(this, err);
errorOrDestroy(this, err, true);
} else if (isBuf || validChunk(this, state, chunk, cb)) {
state.pendingcb++;
ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb);
Expand Down
11 changes: 8 additions & 3 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function undestroy() {
}
}

function errorOrDestroy(stream, err) {
function errorOrDestroy(stream, err, tick) {
// We have tests that rely on errors being emitted
// in the same tick, so changing this is semver major.
// For now when you opt-in to autoDestroy we allow
Expand All @@ -123,8 +123,13 @@ function errorOrDestroy(stream, err) {

if ((r && r.autoDestroy) || (w && w.autoDestroy))
stream.destroy(err);
else if (needError(stream, err))
stream.emit('error', err);
else if (needError(stream, err)) {
if (tick) {
process.nextTick(emitErrorNT, stream, err);
} else {
stream.emit('error', err);
}
}
}


Expand Down

0 comments on commit 4557b48

Please sign in to comment.