Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 43 additions & 41 deletions src/jsc/bindings/webcore/streams/JSDirectStreamController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -812,46 +815,45 @@ 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);
Comment thread
robobun marked this conversation as resolved.
Resolved
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);
}
}

if (deferredClose == 1) {
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);
}
Comment thread
robobun marked this conversation as resolved.
Resolved
return promiseToReturn ? JSValue(promiseToReturn) : jsUndefined();
return true;
}

void JSDirectStreamController::onClose(JSGlobalObject* globalObject, JSValue reason)
Expand Down
16 changes: 9 additions & 7 deletions src/jsc/bindings/webcore/streams/JSDirectStreamController.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ class JSDirectStreamController final : public JSC::JSDestructibleObject {
JSC::WriteBarrier<JSReadableStream> m_stream;
// The converted user source (this + pull/cancel/close). Cleared once no hook can run.
JSC::WriteBarrier<JSDirectStreamSource> 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<JSC::JSPromise> m_pendingRead;
// _deferCloseReason
JSC::WriteBarrier<JSC::Unknown> m_deferCloseReason;
Expand Down Expand Up @@ -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.
Comment thread
robobun marked this conversation as resolved.
Resolved
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,26 @@ void readableStreamDefaultReaderRead(JSGlobalObject* globalObject, JSReadableStr
RELEASE_AND_RETURN(scope, readableStreamAddReadRequest(globalObject, stream, readRequest));
case ControllerKind::Direct: {
auto* controller = uncheckedDowncast<WebCore::JSDirectStreamController>(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]].
Comment thread
robobun marked this conversation as resolved.
Resolved
if (readRequest->kind() == ReadRequestKind::Promise) {
auto* readPromise = uncheckedDowncast<JSPromise>(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: {
Expand Down Expand Up @@ -387,10 +387,10 @@ JSValue readableStreamDefaultReaderReadMany(JSGlobalObject* globalObject, JSRead
if (state == ReadableStreamState::Closed)
break;
auto* controller = uncheckedDowncast<WebCore::JSDirectStreamController>(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<JSPromise>(pulled);
if (!pulledPromise)
if (!pulled)
break;
auto* result = JSPromise::create(vm, globalObject->promiseStructure());
pulledPromise->performPromiseThenWithContext(vm, globalObject, runtime->onReadManyDirectPullFulfilled(), jsUndefined(), result, reader);
Comment thread
robobun marked this conversation as resolved.
Resolved
Expand Down
142 changes: 141 additions & 1 deletion test/js/web/streams/streams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -4397,6 +4397,127 @@ describe("direct stream edge cases", () => {
expect(out).toEqual({ queueMicrotask: "ab", nextTick: "ab", setImmediate: "ab", setTimeout0: "ab" });
});

// A consumer that tears down when reader.closed settles (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"));
},
"closed: 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"));
},
"closed: source failed",
],
[
"flushed inside pull(), error() after an await",
async c => {
c.write("a");
await c.flush();
c.error(new Error("source failed"));
},
"closed: source failed",
],
[
"flushed inside pull(), then pull() rejects",
async c => {
c.write("a");
await c.flush();
throw new Error("source failed");
},
"closed: source failed",
],
[
"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.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]);
});

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 () => {
await using proc = Bun.spawn({
cmd: [
Expand Down Expand Up @@ -4888,6 +5009,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));
Comment thread
robobun marked this conversation as resolved.
Resolved
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 => {
Expand Down
Loading