withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
46.68k stars 2.47k forks source link

There was an uncaught error in the middle of the stream while rendering (TypeError: iterator.return is not a function) #10184

Closed xriter closed 8 months ago

xriter commented 8 months ago

Astro Info

Astro                    v4.4.0
Node                     v18.17.1
System                   macOS (arm64)
Package Manager          npm
Output                   server
Adapter                  @astrojs/node
Integrations             @astrojs/tailwind
                         @astrojs/react

Describe the Bug

The following error is being logged sometimes:

There was an uncaught error in the middle of the stream while rendering /. TypeError: iterator.return is not a function
    at Object.cancel (node:internal/deps/undici/undici:666:34)
    at readableStreamDefaultControllerCancelSteps (node:internal/webstreams/readablestream:2281:39)
    at [kCancel] (node:internal/webstreams/readablestream:1057:12)
    at ensureIsPromise (node:internal/webstreams/util:192:19)
    at readableStreamCancel (node:internal/webstreams/readablestream:1882:5)
    at readableStreamReaderGenericCancel (node:internal/webstreams/readablestream:2022:10)
    at ReadableStreamDefaultReader.cancel (node:internal/webstreams/readablestream:880:12)
    at ServerResponse.<anonymous> (file:///app/dist/server/entry.mjs:2083:16)
    at ServerResponse.emit (node:events:517:28)
    at emitCloseNT (node:_http_server:1020:10)
// dist/server/entry.mjs (lines: 2075-2099)

static async writeResponse(source, destination) {
    const { status, headers, body } = source;
    destination.writeHead(status, createOutgoingHttpHeaders(headers));
    if (!body)
      return destination.end();
    try {
      const reader = body.getReader();
      destination.on("close", () => {
        reader.cancel().catch((err) => {
          console.error(
            `There was an uncaught error in the middle of the stream while rendering ${destination.req.url}.`,
            err
          );
        });
      });
      let result = await reader.read();
      while (!result.done) {
        destination.write(result.value);
        result = await reader.read();
      }
      destination.end();
    } catch {
      destination.end("Internal server error");
    }
  }

I have not been able to reproduce it consistently yet. The stack trace doesn't point to any custom code, so it seems like it is a problem in the node code?

Anyone else experiencing this?

bluwy commented 8 months ago

Could be related to https://github.com/withastro/astro/pull/9614

matthewp commented 8 months ago

Fixed by #10197