cloudflare / cloudflare/workerd

🐛 Bug Report — Runtime APIs: `Can't read from request stream after response has been sent.` in Durable Object when `request.body` not consumed

Open
#918 7 comments 6 reactions 0 assignees View on GitHub
Durable Objects
Dominant language
C++
Stars
8.7k
Forks
739
Avg merge
2d 20h
Merged PRs (30d)
174

Description

Hey! 👋

With the following configuration...

```capnp
using Workerd = import "/workerd/workerd.capnp";

const config :Workerd.Config = (
services = [
( name = "main", worker = .worker ),
],
sockets = [
( name = "http", address = "*:8080", http = (), service = "main" ),
]
);

const worker :Workerd.Worker = (
compatibilityDate = "2023-07-24",
modules = [ ( name = "index.mjs", esModule = embed "index.mjs" ) ],
durableObjectNamespaces = [
( className = "TestObject", uniqueKey = "TestObject" )
],
durableObjectStorage = ( inMemory = void ),
bindings = [
( name = "OBJECT", durableObjectNamespace = "TestObject" ),
],
);
```

```js
// index.mjs
export class TestObject {
async fetch(request) {
const { pathname } = new URL(request.url);
if (pathname === "/1") {
return new Response(); // ✅
} else if (pathname === "/2") {
return new Response("body"); // `TypeError: Can't read from request stream after response has been sent.`
} else if (pathname === "/3") {
await request.body.pipeTo(new WritableStream());
return new Response("body"); // ✅
}
return new Response(null, { status: 404 });
}
}

export default {
async fetch(request, env, ctx) {
const id = env.OBJECT.idFromName("");
const stub = env.OBJECT.get(id);
return stub.fetch(request);
}
}
```
...`workerd` returns a 200 OK response for all paths, but logs `workerd/io/io-context.c++:380: info: uncaught exception; exception = workerd/api/global-scope.c++:81: failed: jsg.TypeError: Can't read from request stream after response has been sent.` when `--verbose` is enabled and `/2` is requested with a body (e.g. `$ curl -d 0123456789 http://127.0.0.1:8080/2`). This only reproduces in a Durable Object, and not when an empty response is returned or the body is consumed.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.