cloudflare / cloudflare/workerd
ReadableStream calls pull() before it's finished with previous result.
- Dominant language
- C++
- Stars
- 8.7k
- Forks
- 739
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 174
Description
### Which Cloudflare product(s) does this pertain to?
Workers Runtime
### What versions & operating system are you using?
node v20.19, wrangler 4.10.0, compatibility_date 2025-03-27
### Please provide a link to a minimal reproduction
_No response_
### Describe the Bug
I have a `pageGenerator()` function which yields a view of its internal `Uint8Array`, and I'm calling this from a `ReadableStream` like so:
```typescript
const generator = pageGenerator(hash, url.pathname);
const stream = new ReadableStream({
async pull(controller) {
const { value, done } = generator.next();
if (done) {
controller.close();
} else {
controller.enqueue(value);
}
},
cancel() {
generator.return(undefined);
}
});
return new Response(stream, { headers: html_headers });
```
What I observe is that after returning from `pull()`, it can be called again even though it's not yet safe to overwrite the previous output buffer.
I pass this `ReadableStream` as the body to a `Response()`, and what I observe is data loss where a subsequent chunk appears too early in the stream because `pageGenerator()` overwrote the content before it was delivered.
But this occurs fairly rarely. My generator returns 16kB chunks, and I might see a glitch about once every few megabytes.
### Please provide any relevant error logs
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.