From 3b2d8e6d971992e9de983d5506420f5a7ae26184 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Mon, 21 Sep 2026 16:45:14 +0000 Subject: [PATCH 1/5] streams: settle a direct stream's read() promise itself instead of adopting the pump's reader.read() on a type: "direct" stream resolved its promise with the promise that the direct pump allocated. Resolving a promise with a promise costs two extra microtasks, so the read settled after a reader.closed that the source rejected right after the chunk. Duplex.fromWeb destroys on that rejection and dropped the chunk. onPull() now takes the caller's promise and settles it on every path. --- .../streams/JSDirectStreamController.cpp | 84 ++++++++++--------- .../streams/JSDirectStreamController.h | 16 ++-- .../streams/JSReadableStreamDefaultReader.cpp | 18 ++-- test/js/web/streams/streams.test.js | 66 ++++++++++++++- 4 files changed, 126 insertions(+), 58 deletions(-) diff --git a/src/jsc/bindings/webcore/streams/JSDirectStreamController.cpp b/src/jsc/bindings/webcore/streams/JSDirectStreamController.cpp index 5a6506948a2d..a65c7276c767 100644 --- a/src/jsc/bindings/webcore/streams/JSDirectStreamController.cpp +++ b/src/jsc/bindings/webcore/streams/JSDirectStreamController.cpp @@ -727,7 +727,7 @@ static JSValue callDirectPull(JSC::VM& vm, JSGlobalObject* globalObject, JSDirec return {}; } -JSValue JSDirectStreamController::onPull(JSGlobalObject* globalObject, bool readRequestQueued) +bool JSDirectStreamController::onPull(JSGlobalObject* globalObject, JSPromise* readPromise) { auto& vm = getVM(globalObject); auto scope = DECLARE_THROW_SCOPE(vm); @@ -740,36 +740,37 @@ JSValue JSDirectStreamController::onPull(JSGlobalObject* globalObject, bool read m_finalChunk.clear(); auto* stream = m_stream.get(); // A queued read request (for-await/tee/pipeTo) takes the chunk through its own - // chunkSteps; a promise-backed read gets it wrapped in the returned promise. + // chunkSteps; a promise-backed read is fulfilled with it. if (stream && readableStreamHasDefaultReader(stream) && readableStreamGetNumReadRequests(stream) > 0) { readableStreamFulfillReadRequest(globalObject, stream, chunk, false); - RETURN_IF_EXCEPTION(scope, {}); + RETURN_IF_EXCEPTION(scope, false); readableStreamCloseIfPossible(globalObject, stream); - RETURN_IF_EXCEPTION(scope, {}); - return jsUndefined(); + RETURN_IF_EXCEPTION(scope, false); + return false; + } + if (readPromise) { + JSObject* result = createIteratorResultObject(globalObject, chunk, false); + RETURN_IF_EXCEPTION(scope, false); + readPromise->fulfill(vm, result); + RETURN_IF_EXCEPTION(scope, false); } - JSObject* result = createIteratorResultObject(globalObject, chunk, false); - RETURN_IF_EXCEPTION(scope, {}); - auto* promise = JSPromise::create(vm, globalObject->promiseStructure()); - promise->fulfill(vm, result); - RETURN_IF_EXCEPTION(scope, {}); if (stream) { readableStreamCloseIfPossible(globalObject, stream); - RETURN_IF_EXCEPTION(scope, {}); + RETURN_IF_EXCEPTION(scope, false); } - return promise; + return true; } auto* stream = m_stream.get(); if (!stream || stream->m_state != ReadableStreamState::Readable || m_closed) - return jsUndefined(); + return false; // Re-entrant pull while a pull is already running. if (m_deferClose == -1) - return jsUndefined(); + return false; // Refuse before pull() runs, or a close() that pull() defers is lost with the read. - if (!readRequestQueued && m_pendingRead && readableStreamReadRequestsFull(stream)) [[unlikely]] { + if (readPromise && m_pendingRead && readableStreamReadRequestsFull(stream)) [[unlikely]] { throwOutOfMemoryError(globalObject, scope); - return {}; + return false; } int8_t deferredClose = 0; @@ -789,16 +790,18 @@ JSValue JSDirectStreamController::onPull(JSGlobalObject* globalObject, bool read m_deferClose = 0; m_deferFlush = 0; // A VM termination from the pull, or a failure while registering the reaction. - RETURN_IF_EXCEPTION(scope, {}); + RETURN_IF_EXCEPTION(scope, false); if (!abrupt.isEmpty()) { // A synchronous throw from pull errors the stream, which settles a queued read // request through its errorSteps; a promise-backed read gets the rejection here. handleError(globalObject, abrupt); - RETURN_IF_EXCEPTION(scope, {}); - if (readRequestQueued) - return jsUndefined(); - RELEASE_AND_RETURN(scope, promiseRejectedWith(globalObject, abrupt)); + RETURN_IF_EXCEPTION(scope, false); + if (readPromise) { + rejectPromise(globalObject, readPromise, abrupt); + RETURN_IF_EXCEPTION(scope, false); + } + return true; } } else { // A new read arrived while an async pull is pending: the fulfillment reaction will @@ -812,31 +815,30 @@ JSValue JSDirectStreamController::onPull(JSGlobalObject* globalObject, bool read // A queued read request was already settled by the stream's error/close steps. stream = m_stream.get(); if (!stream || stream->m_state != ReadableStreamState::Readable) { - if (readRequestQueued) - return jsUndefined(); - if (auto* pendingRead = m_pendingRead.get()) - return pendingRead; - if (stream && stream->m_state == ReadableStreamState::Errored) - RELEASE_AND_RETURN(scope, promiseRejectedWith(globalObject, stream->m_storedError.get())); + if (!readPromise) + return true; + if (stream && stream->m_state == ReadableStreamState::Errored) { + rejectPromise(globalObject, readPromise, stream->m_storedError.get()); + RETURN_IF_EXCEPTION(scope, false); + return true; + } JSObject* doneResult = createIteratorResultObject(globalObject, jsUndefined(), true); - RETURN_IF_EXCEPTION(scope, {}); - auto* doneP = JSPromise::create(vm, globalObject->promiseStructure()); - doneP->fulfill(vm, doneResult); - return doneP; + RETURN_IF_EXCEPTION(scope, false); + readPromise->fulfill(vm, doneResult); + RETURN_IF_EXCEPTION(scope, false); + return true; } // Register the consumer before replaying what pull() deferred. A queued read request is - // already registered; a promise made for it here would swallow that delivery unobserved. - JSPromise* promiseToReturn = nullptr; - if (!readRequestQueued) { - promiseToReturn = JSPromise::create(vm, globalObject->promiseStructure()); + // already registered. + if (readPromise) { if (!m_pendingRead) - m_pendingRead.set(vm, this, promiseToReturn); + m_pendingRead.set(vm, this, readPromise); else { auto* runtime = JSStreamsRuntime::from(globalObject); - auto* readRequest = JSReadRequest::create(vm, runtime->readRequestStructure(defaultGlobalObject(globalObject)), ReadRequestKind::Promise, promiseToReturn); + auto* readRequest = JSReadRequest::create(vm, runtime->readRequestStructure(defaultGlobalObject(globalObject)), ReadRequestKind::Promise, readPromise); readableStreamAddReadRequest(globalObject, stream, readRequest); - RETURN_IF_EXCEPTION(scope, {}); + RETURN_IF_EXCEPTION(scope, false); } } @@ -844,14 +846,14 @@ JSValue JSDirectStreamController::onPull(JSGlobalObject* globalObject, bool read JSValue reason = m_deferCloseReason.get(); m_deferCloseReason.clear(); onClose(globalObject, reason); - RETURN_IF_EXCEPTION(scope, {}); + RETURN_IF_EXCEPTION(scope, false); } else if (deferredFlush == 1 || !m_buffer.isEmpty()) { // Bytes written before this read (outside pull, or by a producer now parked on // backpressure) go to the consumer just registered. onFlush(globalObject); - RETURN_IF_EXCEPTION(scope, {}); + RETURN_IF_EXCEPTION(scope, false); } - return promiseToReturn ? JSValue(promiseToReturn) : jsUndefined(); + return true; } void JSDirectStreamController::onClose(JSGlobalObject* globalObject, JSValue reason) diff --git a/src/jsc/bindings/webcore/streams/JSDirectStreamController.h b/src/jsc/bindings/webcore/streams/JSDirectStreamController.h index e1eeeeb8455e..90cc3de9ef4b 100644 --- a/src/jsc/bindings/webcore/streams/JSDirectStreamController.h +++ b/src/jsc/bindings/webcore/streams/JSDirectStreamController.h @@ -90,9 +90,9 @@ class JSDirectStreamController final : public JSC::JSDestructibleObject { JSC::WriteBarrier m_stream; // The converted user source (this + pull/cancel/close). Cleared once no hook can run. JSC::WriteBarrier m_source; - // _pendingRead — the promise the in-flight read()/readMany() is waiting on. Only + // _pendingRead — the in-flight read()/readMany()'s own promise (see onPull). Only // promise-backed reads register here; pipeTo / tee / for-await reads wait in the - // reader's [[readRequests]] instead (see onPull). handleError rejects AND CLEARS it. + // reader's [[readRequests]] instead. handleError rejects AND CLEARS it. JSC::WriteBarrier m_pendingRead; // _deferCloseReason JSC::WriteBarrier m_deferCloseReason; @@ -166,11 +166,13 @@ class JSDirectStreamController final : public JSC::JSDestructibleObject { // The state machine. All userJS: YES. // The READ pump: every default-reader read on a Direct stream lands here. A promise-backed - // read()/readMany() passes readRequestQueued=false and adopts the returned promise - // (undefined when the pump refused). A pipeTo / tee / for-await read adds its - // JSReadRequest to [[readRequests]] first and passes readRequestQueued=true: that - // request is the consumer, the pump registers no promise for it and returns undefined. - JSC::JSValue onPull(JSC::JSGlobalObject*, bool readRequestQueued); + // read()/readMany() passes its own promise, and the pump settles that promise itself. A + // promise the caller adopted would settle two microtasks after the delivery, behind a + // reader.closed that the source settles right after the chunk. false = the pump refused + // (the source already ended, or a re-entrant pull): readPromise is untouched and the + // caller reports done. A pipeTo / tee / for-await read adds its JSReadRequest to + // [[readRequests]] first and passes nullptr: that request is the consumer. + bool onPull(JSC::JSGlobalObject*, JSC::JSPromise* readPromise); // `end()` / `close(reason)` — reason may be the empty JSValue (absent). void onClose(JSC::JSGlobalObject*, JSC::JSValue reason); // `flush()` — BRANCH ORDER IS LOAD-BEARING. diff --git a/src/jsc/bindings/webcore/streams/JSReadableStreamDefaultReader.cpp b/src/jsc/bindings/webcore/streams/JSReadableStreamDefaultReader.cpp index 6e7e4ef1172d..033ecf0cdffb 100644 --- a/src/jsc/bindings/webcore/streams/JSReadableStreamDefaultReader.cpp +++ b/src/jsc/bindings/webcore/streams/JSReadableStreamDefaultReader.cpp @@ -124,26 +124,26 @@ void readableStreamDefaultReaderRead(JSGlobalObject* globalObject, JSReadableStr RELEASE_AND_RETURN(scope, readableStreamAddReadRequest(globalObject, stream, readRequest)); case ControllerKind::Direct: { auto* controller = uncheckedDowncast(stream->m_controller.get()); - // The direct pump allocates and settles its own head-of-line promise; a - // promise-backed read adopts it instead of waiting in [[readRequests]]. + // The direct pump settles a promise-backed read's promise itself, as its head-of-line + // read, instead of the read waiting in [[readRequests]]. if (readRequest->kind() == ReadRequestKind::Promise) { auto* readPromise = uncheckedDowncast(readRequest->context()); - JSValue pulled = controller->onPull(globalObject, /* readRequestQueued */ false); + bool pulled = controller->onPull(globalObject, readPromise); RETURN_IF_EXCEPTION(scope, void()); - if (!pulled.isObject()) { + if (!pulled) { // The pump refused (already closed / re-entrant pull): report done. JSObject* doneResult = createIteratorResultObject(globalObject, jsUndefined(), true); RETURN_IF_EXCEPTION(scope, void()); RELEASE_AND_RETURN(scope, resolvePromise(globalObject, readPromise, doneResult)); } - RELEASE_AND_RETURN(scope, resolvePromise(globalObject, readPromise, pulled)); + return; } // Other read-request kinds wait in [[readRequests]] and are delivered through their // own chunk/close/error steps. readableStreamAddReadRequest(globalObject, stream, readRequest); RETURN_IF_EXCEPTION(scope, void()); scope.release(); - controller->onPull(globalObject, /* readRequestQueued */ true); + controller->onPull(globalObject, nullptr); return; } case ControllerKind::NativeSink: { @@ -387,10 +387,10 @@ JSValue readableStreamDefaultReaderReadMany(JSGlobalObject* globalObject, JSRead if (state == ReadableStreamState::Closed) break; auto* controller = uncheckedDowncast(stream->m_controller.get()); - JSValue pulled = controller->onPull(globalObject, /* readRequestQueued */ false); + auto* pulledPromise = JSPromise::create(vm, globalObject->promiseStructure()); + bool pulled = controller->onPull(globalObject, pulledPromise); RETURN_IF_EXCEPTION(scope, {}); - auto* pulledPromise = dynamicDowncast(pulled); - if (!pulledPromise) + if (!pulled) break; auto* result = JSPromise::create(vm, globalObject->promiseStructure()); pulledPromise->performPromiseThenWithContext(vm, globalObject, runtime->onReadManyDirectPullFulfilled(), jsUndefined(), result, reader); diff --git a/test/js/web/streams/streams.test.js b/test/js/web/streams/streams.test.js index 8a631d48b156..ecb70396a2c4 100644 --- a/test/js/web/streams/streams.test.js +++ b/test/js/web/streams/streams.test.js @@ -11,7 +11,7 @@ import { bunEnv, bunExe, isASAN, isDebug, isLinux, isMacOS, isWindows, tempDir, import { mkfifo } from "mkfifo"; import { closeSync, createReadStream, openSync, realpathSync, unlinkSync, writeFileSync } from "node:fs"; import { join } from "node:path"; -import { PassThrough, Readable, Writable, finished, pipeline } from "node:stream"; +import { Duplex, PassThrough, Readable, Writable, finished, pipeline } from "node:stream"; import { consumers as directConsumers, expected as directExpected, @@ -4397,6 +4397,51 @@ describe("direct stream edge cases", () => { expect(out).toEqual({ queueMicrotask: "ab", nextTick: "ab", setImmediate: "ab", setTimeout0: "ab" }); }); + // A consumer that tears down when reader.closed rejects (Duplex.fromWeb, Node's adapters) drops a chunk + // whose read() settles after it. + test.each([ + [ + "flushed inside pull(), close(error) after an await", + async c => { + c.write("a"); + await c.flush(); + c.close(new Error("source failed")); + }, + ], + [ + "flushed after an await, close(error) in the same tick", + async c => { + await later(); + c.write("a"); + c.flush(); + c.close(new Error("source failed")); + }, + ], + [ + "flushed inside pull(), error() after an await", + async c => { + c.write("a"); + await c.flush(); + c.error(new Error("source failed")); + }, + ], + [ + "flushed inside pull(), then pull() rejects", + async c => { + c.write("a"); + await c.flush(); + throw new Error("source failed"); + }, + ], + ])("a read() that received a chunk is observed before reader.closed rejects: %s", async (_, pull) => { + const log = []; + const reader = direct(tally(), pull).getReader(); + reader.closed.catch(e => log.push("closed: " + e.message)); + await reader.read().then(r => log.push("read: " + txt(r.value))); + await later(); + expect(log).toEqual(["read: a", "closed: source failed"]); + }); + test("a pull() that rejects after close() ran is not an unhandled rejection and does not fail the consumer", async () => { await using proc = Bun.spawn({ cmd: [ @@ -4888,6 +4933,25 @@ describe("direct stream edge cases", () => { }); }); + test("Duplex.fromWeb(direct): close(error) after a flushed chunk emits 'data' with it, then 'error'", async () => { + const t = tally(); + const readable = direct(t, async c => { + c.write("a"); + await c.flush(); + c.close(new Error("source failed")); + }); + const duplex = Duplex.fromWeb({ readable, writable: new WritableStream() }); + const events = []; + duplex.on("data", d => events.push("data:" + txt(d))); + duplex.on("end", () => events.push("end")); + const err = await new Promise(resolve => duplex.on("error", resolve)); + expect({ events, err: err.message, pulls: t.pulls }).toEqual({ + events: ["data:a"], + err: "source failed", + pulls: 1, + }); + }); + test("Readable.fromWeb(direct).destroy(err) cancels the source once", async () => { const t = tally(); const rs = direct(t, async c => { From dad1c488da8185722d3fc92a3eb608e2488bb366 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Tue, 22 Sep 2026 03:39:20 +0000 Subject: [PATCH 2/5] test: cover overlapping reads and a fulfilled reader.closed on a direct stream --- test/js/web/streams/streams.test.js | 84 +++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 4 deletions(-) diff --git a/test/js/web/streams/streams.test.js b/test/js/web/streams/streams.test.js index ecb70396a2c4..a85a06d17155 100644 --- a/test/js/web/streams/streams.test.js +++ b/test/js/web/streams/streams.test.js @@ -4397,7 +4397,7 @@ describe("direct stream edge cases", () => { expect(out).toEqual({ queueMicrotask: "ab", nextTick: "ab", setImmediate: "ab", setTimeout0: "ab" }); }); - // A consumer that tears down when reader.closed rejects (Duplex.fromWeb, Node's adapters) drops a chunk + // A consumer that tears down when reader.closed settles (Duplex.fromWeb, Node's adapters) drops a chunk // whose read() settles after it. test.each([ [ @@ -4407,6 +4407,7 @@ describe("direct stream edge cases", () => { await c.flush(); c.close(new Error("source failed")); }, + "closed: source failed", ], [ "flushed after an await, close(error) in the same tick", @@ -4416,6 +4417,7 @@ describe("direct stream edge cases", () => { c.flush(); c.close(new Error("source failed")); }, + "closed: source failed", ], [ "flushed inside pull(), error() after an await", @@ -4424,6 +4426,7 @@ describe("direct stream edge cases", () => { await c.flush(); c.error(new Error("source failed")); }, + "closed: source failed", ], [ "flushed inside pull(), then pull() rejects", @@ -4432,14 +4435,87 @@ describe("direct stream edge cases", () => { await c.flush(); throw new Error("source failed"); }, + "closed: source failed", ], - ])("a read() that received a chunk is observed before reader.closed rejects: %s", async (_, pull) => { + [ + "flushed after an await, close() in the same tick", + async c => { + await later(); + c.write("a"); + c.flush(); + c.close(); + }, + "closed", + ], + [ + "end() hands its final chunk to the pending read", + async c => { + await later(); + c.write("a"); + c.end(); + }, + "closed", + ], + ])("a read() that received a chunk is observed before reader.closed settles: %s", async (_, pull, closed) => { const log = []; const reader = direct(tally(), pull).getReader(); - reader.closed.catch(e => log.push("closed: " + e.message)); + reader.closed.then( + () => log.push("closed"), + e => log.push("closed: " + e.message), + ); await reader.read().then(r => log.push("read: " + txt(r.value))); await later(); - expect(log).toEqual(["read: a", "closed: source failed"]); + expect(log).toEqual(["read: a", closed]); + }); + + describe("overlapping read()s are observed in the order they were issued", () => { + const observe = async (reader, log = []) => { + const show = r => (r.done ? "done" : txt(r.value)); + await Promise.all([1, 2].map(i => reader.read().then(r => log.push(`read ${i}: ` + show(r))))); + await later(); + return log; + }; + + // Read 2 waits in the reader's read requests until the flush for read 1 makes it the pending read. + test("each receives a chunk, then close(error)", async () => { + const t = tally(); + const reader = direct(t, async c => { + await later(); + for (const part of ["a", "b"]) { + c.write(part); + c.flush(); + } + c.close(new Error("source failed")); + }).getReader(); + const log = []; + reader.closed.catch(e => log.push("closed: " + e.message)); + expect({ log: await observe(reader, log), pulls: t.pulls }).toEqual({ + log: ["read 1: a", "read 2: b", "closed: source failed"], + pulls: 1, + }); + }); + + test("a sync pull() writes and closes: read 2 finds the stream closed", async () => { + const reader = direct(tally(), c => { + c.write("a"); + c.close(); + }).getReader(); + expect(await observe(reader)).toEqual(["read 1: a", "read 2: done"]); + }); + + test("end() with nobody reading keeps the final chunk for read 1", async () => { + let controller; + const reader = direct(tally(), async c => { + controller = c; + c.write("first"); + await c.flush(); + await new Promise(() => {}); + }).getReader(); + expect(txt((await reader.read()).value)).toBe("first"); + controller.write("a"); + controller.end(); + expect(await observe(reader)).toEqual(["read 1: a", "read 2: done"]); + }); }); test("a pull() that rejects after close() ran is not an unhandled rejection and does not fail the consumer", async () => { From 069e033c4ba6672751a4e3b68315874a69314494 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Tue, 22 Sep 2026 03:58:39 +0000 Subject: [PATCH 3/5] streams: reject the read with undefined when the stored error is empty --- src/jsc/bindings/webcore/streams/JSDirectStreamController.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/jsc/bindings/webcore/streams/JSDirectStreamController.cpp b/src/jsc/bindings/webcore/streams/JSDirectStreamController.cpp index a65c7276c767..f215b546112f 100644 --- a/src/jsc/bindings/webcore/streams/JSDirectStreamController.cpp +++ b/src/jsc/bindings/webcore/streams/JSDirectStreamController.cpp @@ -818,7 +818,8 @@ bool JSDirectStreamController::onPull(JSGlobalObject* globalObject, JSPromise* r if (!readPromise) return true; if (stream && stream->m_state == ReadableStreamState::Errored) { - rejectPromise(globalObject, readPromise, stream->m_storedError.get()); + JSValue storedError = stream->m_storedError.get(); + rejectPromise(globalObject, readPromise, storedError ? storedError : jsUndefined()); RETURN_IF_EXCEPTION(scope, false); return true; } From c77ecf0b88a68434a4745059cce43c317b57c69e Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Tue, 22 Sep 2026 04:03:35 +0000 Subject: [PATCH 4/5] streams: shorten the onPull contract comments --- .../bindings/webcore/streams/JSDirectStreamController.h | 7 +------ .../webcore/streams/JSReadableStreamDefaultReader.cpp | 3 +-- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/jsc/bindings/webcore/streams/JSDirectStreamController.h b/src/jsc/bindings/webcore/streams/JSDirectStreamController.h index 90cc3de9ef4b..a45ce2bfb0b4 100644 --- a/src/jsc/bindings/webcore/streams/JSDirectStreamController.h +++ b/src/jsc/bindings/webcore/streams/JSDirectStreamController.h @@ -166,12 +166,7 @@ class JSDirectStreamController final : public JSC::JSDestructibleObject { // The state machine. All userJS: YES. // The READ pump: every default-reader read on a Direct stream lands here. A promise-backed - // read()/readMany() passes its own promise, and the pump settles that promise itself. A - // promise the caller adopted would settle two microtasks after the delivery, behind a - // reader.closed that the source settles right after the chunk. false = the pump refused - // (the source already ended, or a re-entrant pull): readPromise is untouched and the - // caller reports done. A pipeTo / tee / for-await read adds its JSReadRequest to - // [[readRequests]] first and passes nullptr: that request is the consumer. + // read()/readMany() passes its own promise for the pump to settle (false = refused: the caller reports done); a read that queued a JSReadRequest (pipeTo / tee / for-await) passes nullptr. bool onPull(JSC::JSGlobalObject*, JSC::JSPromise* readPromise); // `end()` / `close(reason)` — reason may be the empty JSValue (absent). void onClose(JSC::JSGlobalObject*, JSC::JSValue reason); diff --git a/src/jsc/bindings/webcore/streams/JSReadableStreamDefaultReader.cpp b/src/jsc/bindings/webcore/streams/JSReadableStreamDefaultReader.cpp index 033ecf0cdffb..03ba9168ffa9 100644 --- a/src/jsc/bindings/webcore/streams/JSReadableStreamDefaultReader.cpp +++ b/src/jsc/bindings/webcore/streams/JSReadableStreamDefaultReader.cpp @@ -124,8 +124,7 @@ void readableStreamDefaultReaderRead(JSGlobalObject* globalObject, JSReadableStr RELEASE_AND_RETURN(scope, readableStreamAddReadRequest(globalObject, stream, readRequest)); case ControllerKind::Direct: { auto* controller = uncheckedDowncast(stream->m_controller.get()); - // The direct pump settles a promise-backed read's promise itself, as its head-of-line - // read, instead of the read waiting in [[readRequests]]. + // The direct pump settles a promise-backed read's promise itself: the read does not wait in [[readRequests]]. if (readRequest->kind() == ReadRequestKind::Promise) { auto* readPromise = uncheckedDowncast(readRequest->context()); bool pulled = controller->onPull(globalObject, readPromise); From b8639586094d9841705f4427ef2b72cad5468585 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Tue, 22 Sep 2026 04:34:45 +0000 Subject: [PATCH 5/5] test: fail the Duplex.fromWeb direct stream test fast when 'error' is missing --- test/js/web/streams/streams.test.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/js/web/streams/streams.test.js b/test/js/web/streams/streams.test.js index a85a06d17155..ed0b09ce8e81 100644 --- a/test/js/web/streams/streams.test.js +++ b/test/js/web/streams/streams.test.js @@ -5020,12 +5020,10 @@ describe("direct stream edge cases", () => { const events = []; duplex.on("data", d => events.push("data:" + txt(d))); duplex.on("end", () => events.push("end")); - const err = await new Promise(resolve => duplex.on("error", resolve)); - expect({ events, err: err.message, pulls: t.pulls }).toEqual({ - events: ["data:a"], - err: "source failed", - pulls: 1, - }); + duplex.on("error", e => events.push("error:" + e.message)); + // 'close' follows 'error' and a clean 'end' alike, so a missing 'error' fails the assertion. + await new Promise(resolve => duplex.on("close", resolve)); + expect({ events, pulls: t.pulls }).toEqual({ events: ["data:a", "error:source failed"], pulls: 1 }); }); test("Readable.fromWeb(direct).destroy(err) cancels the source once", async () => {