From 6cecf140e7cd4e57badd38f1a866533b807cf9d7 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 4 Oct 2026 01:04:01 +0000 Subject: [PATCH] stream: do not fail the writer when pipeToSync() cannot close After writing every chunk, pipeToSync() treated endSync() returning -1 like any other error: it threw ERR_INVALID_STATE and, unless preventFail was set, called writer.fail() with it. -1 only means that the writer cannot close synchronously, e.g. a push() writer whose consumer has not drained it yet. All of the data had been accepted, but failing the writer discarded it, so the consumer saw an error instead of the end of the stream, and the caller could not recover. pipeToSync() still throws ERR_INVALID_STATE in that case, since it never falls back to the async end(), but it no longer fails the writer. The caller can still close it, e.g. with `await writer.end()`. Assisted-by: OpenCode Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/66483 Reviewed-By: Trivikram Kamat --- doc/api/stream_iter.md | 7 +++++ lib/internal/streams/iter/pull.js | 15 +++++++--- test/parallel/test-stream-iter-pipeto-edge.js | 28 ++++++++++++++++++- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/doc/api/stream_iter.md b/doc/api/stream_iter.md index 421a9e2f190..bcc610045a0 100644 --- a/doc/api/stream_iter.md +++ b/doc/api/stream_iter.md @@ -718,6 +718,13 @@ Synchronous version of [`pipeTo()`][]. The `source`, all transforms, and the The `writer` must have the `*Sync` methods (`writeSync`, `writevSync`, `endSync`) and `fail()` for this to work. +`pipeToSync()` never falls back to the asynchronous writer methods. If +`writer.endSync()` returns `-1` because the writer cannot close synchronously +(for example, a `push()` writer whose consumer has not read all of the data +yet), `pipeToSync()` throws `ERR_INVALID_STATE`. All of the data was accepted +by then, so the writer is not failed: it can still be closed, for example with +`await writer.end()`. + ### `pull(source[, ...transforms][, options])`