From f599463f7d34f78502ad9d949bec5b5407bc1d2f Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 13 Feb 2026 10:14:36 +0100 Subject: [PATCH] stream: fix pipeTo to defer writes per WHATWG spec The WHATWG Streams spec requires that pipeTo's chunk handling must queue a microtask before calling the write algorithm. This ensures that enqueue() does not synchronously trigger writes. Previously, PipeToReadableStreamReadRequest[kChunk] would synchronously call writableStreamDefaultWriterWrite(), which violated the spec and caused the WPT test "enqueue() must not synchronously call write algorithm" to fail. Fix by wrapping the write operation in queueMicrotask(), which defers it to the next microtask as required by the spec. Refs: https://github.com/whatwg/streams/issues/1243 --- lib/internal/webstreams/readablestream.js | 11 ++++++++--- test/wpt/status/streams.json | 8 -------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index f9b9e6b4fb2c3e..1db659aa560ce9 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -1584,9 +1584,14 @@ class PipeToReadableStreamReadRequest { } [kChunk](chunk) { - this.state.currentWrite = writableStreamDefaultWriterWrite(this.writer, chunk); - setPromiseHandled(this.state.currentWrite); - this.promise.resolve(false); + // Per spec, pipeTo must queue a microtask for the write to avoid + // synchronous write during enqueue(). See WHATWG Streams spec + // "ReadableStreamPipeTo" step 15's "chunk steps". + queueMicrotask(() => { + this.state.currentWrite = writableStreamDefaultWriterWrite(this.writer, chunk); + setPromiseHandled(this.state.currentWrite); + this.promise.resolve(false); + }); } [kClose]() { diff --git a/test/wpt/status/streams.json b/test/wpt/status/streams.json index af3646c65ea660..222a68014f4af6 100644 --- a/test/wpt/status/streams.json +++ b/test/wpt/status/streams.json @@ -2,14 +2,6 @@ "idlharness-shadowrealm.window.js": { "skip": "ShadowRealm support is not enabled" }, - "piping/general-addition.any.js": { - "fail": { - "note": "Blocked on https://github.com/whatwg/streams/issues/1243", - "expected": [ - "enqueue() must not synchronously call write algorithm" - ] - } - }, "queuing-strategies-size-function-per-global.window.js": { "skip": "Browser-specific test" },