cloudflare / cloudflare/workerd

Invalid unhandled rejection warning when calling `await reader.read()`

Open
#501 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
C++
Stars
8.7k
Forks
739
Avg merge
2d 20h
Merged PRs (30d)
174

Description

Hi there, when using `fetch` in combination with `AbortSignal.timeout(1000)`, I see a false `A promise rejection was handled asynchronously. This warning occurs when attaching a catch handler to a promise after it rejected. (rejection #1)` warning cause by `await reader.read()`.

```typescript
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise {
const res = await fetch("https://speed.hetzner.de/100MB.bin", {
signal: AbortSignal.timeout(1000),
});
if (!res.ok || !res.body) {
return new Response("not ok");
}

try {
const reader = res.body.getReader();
let size = 0;
while (true) {
// reader.read() causes the following warning:
// A promise rejection was handled asynchronously. This warning occurs when attaching a catch handler to a promise after it rejected. (rejection #1)
const { done, value } = await reader.read();
if (done) {
break;
}
size += value.length;
}
return new Response(`size: ${size}`);
} catch (e: any) {
return new Response(e.message);
}
},
};
```

I think the cause might be located somewhere in the internal stream implementation: https://github.com/cloudflare/workerd/blob/f609d1aca6797266d2144151b2fc3ef974c12395/src/workerd/api/streams/internal.c%2B%2B#L431-L450

Best,
Chris

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.