Skip to content

Commit 7f71f50

Browse files
committed
fixup! stream: pass ERR_STREAM_WRITE_AFTER_END to .end(cb) after end
The previous version always synthesized ERR_STREAM_WRITE_AFTER_END when kEnding was set, which broke test-stream-writable-end-cb-error block 1: a writable whose underlying _write errors with `_err`, and whose `.end(cb1)` and `.end(cb2)` are both expected to receive that underlying error via kOnFinished. Tighten the condition to only synthesize WRITE_AFTER_END when the stream has no in-flight write (kWriting), no buffered data (kBuffered), no stored error (kErrored), and an empty buffer length. In any of those cases there is real pending state that will surface a meaningful error through the existing kOnFinished cascade, so the cb should be queued and receive that real error instead. Signed-off-by: Maruthan G <[email protected]>
1 parent be655c5 commit 7f71f50

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

lib/internal/streams/writable.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -845,13 +845,15 @@ Writable.prototype.end = function(chunk, encoding, cb) {
845845
err = new ERR_STREAM_ALREADY_FINISHED('end');
846846
} else if ((state[kState] & kDestroyed) !== 0) {
847847
err = new ERR_STREAM_DESTROYED('end');
848-
} else if ((state[kState] & kEnding) !== 0 && typeof cb === 'function') {
849-
// The stream is already ending (or ended) but not yet finished or
850-
// destroyed. For consistency with `.write(chunk, cb)` after end (which
851-
// invokes cb with ERR_STREAM_WRITE_AFTER_END), surface the same error
852-
// through the user-supplied callback. Note: we do not call
853-
// errorOrDestroy here in order to preserve the pre-existing forgiving
854-
// behavior of `.end()` (no cb) called multiple times.
848+
} else if ((state[kState] & kEnding) !== 0 && typeof cb === 'function' &&
849+
(state[kState] & (kErrored | kWriting | kBuffered)) === 0 &&
850+
state.length === 0) {
851+
// The stream is already ending and has no in-flight write, buffered
852+
// data, or stored error that would surface a real error to the user.
853+
// Synthesize ERR_STREAM_WRITE_AFTER_END so the user-supplied callback
854+
// fires consistently with `.write(chunk, cb)` after end. When pending
855+
// work or a stored error exists, fall through so the cb is queued on
856+
// kOnFinished and receives that real error instead.
855857
err = new ERR_STREAM_WRITE_AFTER_END();
856858
}
857859

0 commit comments

Comments
 (0)