Skip to content

Question: wailUntil() lifetime on SSE response. #6151

@cometkim

Description

@cometkim

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

Code looks like:

  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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions