Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
56 changes: 33 additions & 23 deletions src/jsc/bindings/webcore/streams/JSReadRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ namespace WebCore {
using namespace JSC;
using namespace Bun::WebStreams;

// The tee state's branches always carry the controller kind their tee installed.
// A tee branch's controller slot usually still carries the kind its tee installed, but
// Bun's native-sink pumps clear a consumed stream's controller slot in their finally step,
// so these recover as null when the branch has been torn down.
static JSReadableStreamDefaultController* defaultControllerOf(JSReadableStream* stream)
{
ASSERT(stream->m_controllerKind == ControllerKind::Default);
if (stream->m_controllerKind != ControllerKind::Default)
return nullptr;
return uncheckedDowncast<JSReadableStreamDefaultController>(stream->m_controller.get());
}

static JSReadableByteStreamController* byteControllerOf(JSReadableStream* stream)
{
ASSERT(stream->m_controllerKind == ControllerKind::Byte);
if (stream->m_controllerKind != ControllerKind::Byte)
return nullptr;
return uncheckedDowncast<JSReadableByteStreamController>(stream->m_controller.get());
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

Expand Down Expand Up @@ -146,12 +150,14 @@ void JSReadRequest::closeSteps(JSGlobalObject* globalObject)
case ReadRequestKind::DefaultTee: {
auto* teeState = uncheckedDowncast<JSStreamTeeState>(m_context.get());
teeState->m_reading = false;
if (!teeState->m_canceled1) {
readableStreamDefaultControllerClose(globalObject, defaultControllerOf(teeState->m_branch1.get()));
auto* controller1 = defaultControllerOf(teeState->m_branch1.get());
auto* controller2 = defaultControllerOf(teeState->m_branch2.get());
if (!teeState->m_canceled1 && controller1) {
readableStreamDefaultControllerClose(globalObject, controller1);
RETURN_IF_EXCEPTION(scope, void());
}
if (!teeState->m_canceled2) {
readableStreamDefaultControllerClose(globalObject, defaultControllerOf(teeState->m_branch2.get()));
if (!teeState->m_canceled2 && controller2) {
readableStreamDefaultControllerClose(globalObject, controller2);
RETURN_IF_EXCEPTION(scope, void());
}
if (!teeState->m_canceled1 || !teeState->m_canceled2)
Expand All @@ -161,20 +167,22 @@ void JSReadRequest::closeSteps(JSGlobalObject* globalObject)
case ReadRequestKind::ByteTee: {
auto* teeState = uncheckedDowncast<JSStreamTeeState>(m_context.get());
teeState->m_reading = false;
if (!teeState->m_canceled1) {
readableByteStreamControllerClose(globalObject, byteControllerOf(teeState->m_branch1.get()));
auto* controller1 = byteControllerOf(teeState->m_branch1.get());
auto* controller2 = byteControllerOf(teeState->m_branch2.get());
if (!teeState->m_canceled1 && controller1) {
readableByteStreamControllerClose(globalObject, controller1);
RETURN_IF_EXCEPTION(scope, void());
}
if (!teeState->m_canceled2) {
readableByteStreamControllerClose(globalObject, byteControllerOf(teeState->m_branch2.get()));
if (!teeState->m_canceled2 && controller2) {
readableByteStreamControllerClose(globalObject, controller2);
RETURN_IF_EXCEPTION(scope, void());
}
if (!byteControllerOf(teeState->m_branch1.get())->m_pendingPullIntos.isEmpty()) {
readableByteStreamControllerRespond(globalObject, byteControllerOf(teeState->m_branch1.get()), 0);
if (controller1 && !controller1->m_pendingPullIntos.isEmpty()) {
readableByteStreamControllerRespond(globalObject, controller1, 0);
RETURN_IF_EXCEPTION(scope, void());
}
if (!byteControllerOf(teeState->m_branch2.get())->m_pendingPullIntos.isEmpty()) {
readableByteStreamControllerRespond(globalObject, byteControllerOf(teeState->m_branch2.get()), 0);
if (controller2 && !controller2->m_pendingPullIntos.isEmpty()) {
readableByteStreamControllerRespond(globalObject, controller2, 0);
RETURN_IF_EXCEPTION(scope, void());
}
if (!teeState->m_canceled1 || !teeState->m_canceled2)
Expand Down Expand Up @@ -307,24 +315,26 @@ void JSReadIntoRequest::closeSteps(JSGlobalObject* globalObject, JSArrayBufferVi
teeState->m_reading = false;
auto* byobBranch = forBranch2 ? teeState->m_branch2.get() : teeState->m_branch1.get();
auto* otherBranch = forBranch2 ? teeState->m_branch1.get() : teeState->m_branch2.get();
auto* byobController = byteControllerOf(byobBranch);
auto* otherController = byteControllerOf(otherBranch);
bool byobCanceled = forBranch2 ? teeState->m_canceled2 : teeState->m_canceled1;
bool otherCanceled = forBranch2 ? teeState->m_canceled1 : teeState->m_canceled2;
if (!byobCanceled) {
readableByteStreamControllerClose(globalObject, byteControllerOf(byobBranch));
if (!byobCanceled && byobController) {
readableByteStreamControllerClose(globalObject, byobController);
RETURN_IF_EXCEPTION(scope, void());
}
if (!otherCanceled) {
readableByteStreamControllerClose(globalObject, byteControllerOf(otherBranch));
if (!otherCanceled && otherController) {
readableByteStreamControllerClose(globalObject, otherController);
RETURN_IF_EXCEPTION(scope, void());
}
if (chunkOrNull) {
ASSERT(!chunkOrNull->byteLength());
if (!byobCanceled) {
readableByteStreamControllerRespondWithNewView(globalObject, byteControllerOf(byobBranch), chunkOrNull);
if (!byobCanceled && byobController) {
readableByteStreamControllerRespondWithNewView(globalObject, byobController, chunkOrNull);
RETURN_IF_EXCEPTION(scope, void());
}
if (!otherCanceled && !byteControllerOf(otherBranch)->m_pendingPullIntos.isEmpty()) {
readableByteStreamControllerRespond(globalObject, byteControllerOf(otherBranch), 0);
if (!otherCanceled && otherController && !otherController->m_pendingPullIntos.isEmpty()) {
readableByteStreamControllerRespond(globalObject, otherController, 0);
RETURN_IF_EXCEPTION(scope, void());
}
}
Expand Down
113 changes: 79 additions & 34 deletions src/jsc/bindings/webcore/streams/ReadableStreamOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ static JSReadableByteStreamController* byteControllerOf(JSReadableStream* stream
return nullptr;
}

// Null-safe tee-branch controller recovery: Bun's native-sink pumps clear a consumed
// stream's controller slot in their finally step, so a tee reaction queued before that
// teardown can see a branch with no controller. A torn-down branch is terminal; skip it.
static JSReadableStreamDefaultController* teeBranchDefaultController(JSReadableStream* branch)
{
if (branch->m_controllerKind != ControllerKind::Default)
return nullptr;
return uncheckedDowncast<JSReadableStreamDefaultController>(branch->m_controller.get());
}

static JSReadableByteStreamController* teeBranchByteController(JSReadableStream* branch)
{
if (branch->m_controllerKind != ControllerKind::Byte)
return nullptr;
return uncheckedDowncast<JSReadableByteStreamController>(branch->m_controller.get());
}

// The byte tee's mutable reader slot is erased to JSCell; recover the non-polymorphic
// reader base through the two concrete classes.
static JSReadableStreamReaderBase* teeReader(JSStreamTeeState* teeState)
Expand Down Expand Up @@ -977,6 +994,8 @@ static EncodedJSValue defaultTeeChunkStepsMicrotask(JSGlobalObject* globalObject
auto& vm = getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
teeState->m_readAgain1 = false;
auto* controller1 = teeBranchDefaultController(teeState->m_branch1.get());
auto* controller2 = teeBranchDefaultController(teeState->m_branch2.get());
JSValue chunk1 = chunk;
JSValue chunk2 = chunk;
if (!teeState->m_canceled2 && teeState->m_shouldClone) {
Expand All @@ -988,10 +1007,14 @@ static EncodedJSValue defaultTeeChunkStepsMicrotask(JSGlobalObject* globalObject
JSValue thrown = takeAbruptCompletion(globalObject, catchScope);
if (thrown.isEmpty())
return {};
readableStreamDefaultControllerError(globalObject, defaultControllerOf(teeState->m_branch1.get()), thrown);
RETURN_IF_EXCEPTION(scope, {});
readableStreamDefaultControllerError(globalObject, defaultControllerOf(teeState->m_branch2.get()), thrown);
RETURN_IF_EXCEPTION(scope, {});
if (controller1) {
readableStreamDefaultControllerError(globalObject, controller1, thrown);
RETURN_IF_EXCEPTION(scope, {});
}
if (controller2) {
readableStreamDefaultControllerError(globalObject, controller2, thrown);
RETURN_IF_EXCEPTION(scope, {});
}
auto* cancelResult = readableStreamCancel(globalObject, teeState->m_stream.get(), thrown);
RETURN_IF_EXCEPTION(scope, {});
resolvePromise(globalObject, teeState->m_cancelPromise.get(), cancelResult);
Expand All @@ -1001,12 +1024,12 @@ static EncodedJSValue defaultTeeChunkStepsMicrotask(JSGlobalObject* globalObject
}
chunk2 = cloneResult;
}
if (!teeState->m_canceled1) {
readableStreamDefaultControllerEnqueue(globalObject, defaultControllerOf(teeState->m_branch1.get()), chunk1);
if (!teeState->m_canceled1 && controller1) {
readableStreamDefaultControllerEnqueue(globalObject, controller1, chunk1);
RETURN_IF_EXCEPTION(scope, {});
}
if (!teeState->m_canceled2) {
readableStreamDefaultControllerEnqueue(globalObject, defaultControllerOf(teeState->m_branch2.get()), chunk2);
if (!teeState->m_canceled2 && controller2) {
readableStreamDefaultControllerEnqueue(globalObject, controller2, chunk2);
RETURN_IF_EXCEPTION(scope, {});
}
teeState->m_reading = false;
Expand All @@ -1022,10 +1045,14 @@ static EncodedJSValue defaultTeeReaderClosedRejected(JSGlobalObject* globalObjec
{
auto& vm = getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
readableStreamDefaultControllerError(globalObject, defaultControllerOf(teeState->m_branch1.get()), reason);
RETURN_IF_EXCEPTION(scope, {});
readableStreamDefaultControllerError(globalObject, defaultControllerOf(teeState->m_branch2.get()), reason);
RETURN_IF_EXCEPTION(scope, {});
if (auto* controller1 = teeBranchDefaultController(teeState->m_branch1.get())) {
readableStreamDefaultControllerError(globalObject, controller1, reason);
RETURN_IF_EXCEPTION(scope, {});
}
if (auto* controller2 = teeBranchDefaultController(teeState->m_branch2.get())) {
readableStreamDefaultControllerError(globalObject, controller2, reason);
RETURN_IF_EXCEPTION(scope, {});
}
if (!teeState->m_canceled1 || !teeState->m_canceled2) {
resolvePromise(globalObject, teeState->m_cancelPromise.get(), jsUndefined());
RETURN_IF_EXCEPTION(scope, {});
Expand Down Expand Up @@ -1158,6 +1185,8 @@ static EncodedJSValue byteTeeChunkStepsMicrotask(JSGlobalObject* globalObject, J
auto scope = DECLARE_THROW_SCOPE(vm);
teeState->m_readAgain1 = false;
teeState->m_readAgain2 = false;
auto* controller1 = teeBranchByteController(teeState->m_branch1.get());
auto* controller2 = teeBranchByteController(teeState->m_branch2.get());
auto* chunk1 = uncheckedDowncast<JSArrayBufferView>(chunk);
JSArrayBufferView* chunk2 = chunk1;
if (!teeState->m_canceled1 && !teeState->m_canceled2) {
Expand All @@ -1169,10 +1198,14 @@ static EncodedJSValue byteTeeChunkStepsMicrotask(JSGlobalObject* globalObject, J
JSValue thrown = takeAbruptCompletion(globalObject, catchScope);
if (thrown.isEmpty())
return {};
readableByteStreamControllerError(globalObject, byteControllerOf(teeState->m_branch1.get()), thrown);
RETURN_IF_EXCEPTION(scope, {});
readableByteStreamControllerError(globalObject, byteControllerOf(teeState->m_branch2.get()), thrown);
RETURN_IF_EXCEPTION(scope, {});
if (controller1) {
readableByteStreamControllerError(globalObject, controller1, thrown);
RETURN_IF_EXCEPTION(scope, {});
}
if (controller2) {
readableByteStreamControllerError(globalObject, controller2, thrown);
RETURN_IF_EXCEPTION(scope, {});
}
auto* cancelResult = readableStreamCancel(globalObject, teeState->m_stream.get(), thrown);
RETURN_IF_EXCEPTION(scope, {});
resolvePromise(globalObject, teeState->m_cancelPromise.get(), cancelResult);
Expand All @@ -1182,12 +1215,12 @@ static EncodedJSValue byteTeeChunkStepsMicrotask(JSGlobalObject* globalObject, J
}
chunk2 = cloneResult;
}
if (!teeState->m_canceled1) {
readableByteStreamControllerEnqueue(globalObject, byteControllerOf(teeState->m_branch1.get()), chunk1);
if (!teeState->m_canceled1 && controller1) {
readableByteStreamControllerEnqueue(globalObject, controller1, chunk1);
RETURN_IF_EXCEPTION(scope, {});
}
if (!teeState->m_canceled2) {
readableByteStreamControllerEnqueue(globalObject, byteControllerOf(teeState->m_branch2.get()), chunk2);
if (!teeState->m_canceled2 && controller2) {
readableByteStreamControllerEnqueue(globalObject, controller2, chunk2);
RETURN_IF_EXCEPTION(scope, {});
}
teeState->m_reading = false;
Expand All @@ -1214,6 +1247,8 @@ static EncodedJSValue byteTeeReadIntoChunkStepsMicrotask(JSGlobalObject* globalO
teeState->m_readAgain2 = false;
auto* byobBranch = forBranch2 ? teeState->m_branch2.get() : teeState->m_branch1.get();
auto* otherBranch = forBranch2 ? teeState->m_branch1.get() : teeState->m_branch2.get();
auto* byobController = teeBranchByteController(byobBranch);
auto* otherController = teeBranchByteController(otherBranch);
bool byobCanceled = forBranch2 ? teeState->m_canceled2 : teeState->m_canceled1;
bool otherCanceled = forBranch2 ? teeState->m_canceled1 : teeState->m_canceled2;

Expand All @@ -1226,25 +1261,31 @@ static EncodedJSValue byteTeeReadIntoChunkStepsMicrotask(JSGlobalObject* globalO
JSValue thrown = takeAbruptCompletion(globalObject, catchScope);
if (thrown.isEmpty())
return {};
readableByteStreamControllerError(globalObject, byteControllerOf(byobBranch), thrown);
RETURN_IF_EXCEPTION(scope, {});
readableByteStreamControllerError(globalObject, byteControllerOf(otherBranch), thrown);
RETURN_IF_EXCEPTION(scope, {});
if (byobController) {
readableByteStreamControllerError(globalObject, byobController, thrown);
RETURN_IF_EXCEPTION(scope, {});
}
if (otherController) {
readableByteStreamControllerError(globalObject, otherController, thrown);
RETURN_IF_EXCEPTION(scope, {});
}
auto* cancelResult = readableStreamCancel(globalObject, teeState->m_stream.get(), thrown);
RETURN_IF_EXCEPTION(scope, {});
resolvePromise(globalObject, teeState->m_cancelPromise.get(), cancelResult);
RETURN_IF_EXCEPTION(scope, {});
return JSValue::encode(jsUndefined());
}
}
if (!byobCanceled) {
readableByteStreamControllerRespondWithNewView(globalObject, byteControllerOf(byobBranch), chunk);
if (!byobCanceled && byobController) {
readableByteStreamControllerRespondWithNewView(globalObject, byobController, chunk);
RETURN_IF_EXCEPTION(scope, {});
}
readableByteStreamControllerEnqueue(globalObject, byteControllerOf(otherBranch), clonedChunk);
RETURN_IF_EXCEPTION(scope, {});
} else if (!byobCanceled) {
readableByteStreamControllerRespondWithNewView(globalObject, byteControllerOf(byobBranch), chunk);
if (otherController) {
readableByteStreamControllerEnqueue(globalObject, otherController, clonedChunk);
RETURN_IF_EXCEPTION(scope, {});
}
} else if (!byobCanceled && byobController) {
readableByteStreamControllerRespondWithNewView(globalObject, byobController, chunk);
RETURN_IF_EXCEPTION(scope, {});
}
teeState->m_reading = false;
Expand All @@ -1266,10 +1307,14 @@ static EncodedJSValue byteTeeReaderClosedRejected(JSGlobalObject* globalObject,
auto* teeState = uncheckedDowncast<JSStreamTeeState>(context->getInternalField(0));
if (context->getInternalField(1) != teeState->m_reader.get())
return JSValue::encode(jsUndefined());
readableByteStreamControllerError(globalObject, byteControllerOf(teeState->m_branch1.get()), reason);
RETURN_IF_EXCEPTION(scope, {});
readableByteStreamControllerError(globalObject, byteControllerOf(teeState->m_branch2.get()), reason);
RETURN_IF_EXCEPTION(scope, {});
if (auto* controller1 = teeBranchByteController(teeState->m_branch1.get())) {
readableByteStreamControllerError(globalObject, controller1, reason);
RETURN_IF_EXCEPTION(scope, {});
}
if (auto* controller2 = teeBranchByteController(teeState->m_branch2.get())) {
readableByteStreamControllerError(globalObject, controller2, reason);
RETURN_IF_EXCEPTION(scope, {});
}
if (!teeState->m_canceled1 || !teeState->m_canceled2) {
resolvePromise(globalObject, teeState->m_cancelPromise.get(), jsUndefined());
RETURN_IF_EXCEPTION(scope, {});
Expand Down
57 changes: 57 additions & 0 deletions test/js/bun/http/serve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3186,3 +3186,60 @@ it("type: direct stream — small write queued under backpressure is delivered i
socket.destroy();
}
});

// `const [a,b] = req.body.tee(); return new Response(a)`: when the client aborts the
// upload, the response-side sink pump tears down branch `a`'s controller slot, then
// the tee's queued closed-rejected / chunk reactions run against a branch that no
// longer carries a default controller. That used to hit a RELEASE_ASSERT in
// defaultControllerOf() and SIGABRT the whole server process.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
it("survives aborted uploads while responding with a tee()d request-body branch", async () => {
const script = `
const net = require("node:net");
const readAll = async rs => { const rd = rs.getReader(); for (;;) { if ((await rd.read()).done) return; } };
const srv = Bun.serve({
hostname: "127.0.0.1", port: 0, idleTimeout: 0,
error() { return new Response("err"); },
async fetch(req) {
if (!req.body) return new Response("nobody");
const [a, b] = req.body.tee();
readAll(b).catch(() => {});
return new Response(a);
},
});
const chunk = Buffer.alloc(8192, 66);
let it = 0;
for (; it < 40; it++) {
await new Promise(done => {
const s = net.connect(srv.port, "127.0.0.1", () => {
s.write("POST / HTTP/1.1\\r\\nhost: x\\r\\ncontent-length: 262144\\r\\n\\r\\n");
setImmediate(() => {
s.write(chunk);
s.write(chunk);
s.write(chunk);
setImmediate(() => { s.destroy(); done(); });
});
});
s.on("data", () => {});
s.on("error", () => done());
});
}
await new Promise(r => setTimeout(r, 20));
console.log("SURVIVED", it);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
srv.stop(true);
`;

await using proc = Bun.spawn({
cmd: [bunExe(), "-e", script],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);

expect({ stdout: stdout.trim(), stderr, exitCode, signalCode: proc.signalCode }).toEqual({
stdout: "SURVIVED 40",
stderr: "",
exitCode: 0,
signalCode: null,
});
});
Loading