cloudflare / cloudflare/workerd

Question: `wailUntil()` lifetime on SSE response.

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

Description

I have used `waitUntil` for parsing streaming responses in a non-blocking manner.

Code looks like:

```js
const proxyHeaders = new Headers(req.headers);
proxyHeaders.set("host", UPSTREAM.host);

const contextPromise = extractRequestContext(req);
const proxyRequest = new Request(targetUrl, {
method: req.method,
body: req.body,
headers: proxyHeaders,
redirect: "follow",
});

const response = await fetch(proxyRequest);

if (!response.body) {
return response;
}

const [passthrough, metricsStream] = response.body.tee();

const metricsPipeline = (async () => {
try {
const context = await contextPromise;

await metricsStream
.pipeThrough(new TextDecoderStream())
.pipeThrough(new LineSplitTransform())
.pipeThrough(new SSEParserTransform())
.pipeThrough(new SSEMetricsTransform(context, writeMetrics))
.getReader()
.read();
} catch (error) {
console.error("Metrics pipeline error:", error);
}
})();

ctx.waitUntil(metricsPipeline);

return new Response(passthrough, {
status: response.status,
headers: response.headers,
});
```

But I got this warning from the production worker:

```
waitUntil() tasks did not complete within the allowed time after invocation end and have been cancelled. See: https://developers.cloudflare.com/workers/runtime-apis/context/#waituntil
```

I read the documentation, but I'm not sure whether "invocation end" refers to immediately after the response is returned, or when the response stream ends.

If I implemented it correctly, the `metricsPipeline` terminates as soon as the response stream ends and doesn't run for more than 30 seconds. There are no additional IO except for writing Analytics Engine data points.

Does this mean that `waitUntil` does not wait for the SSE stream response?

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.