Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
xriter opened this issue Mar 2, 2024 · 1 comment · Fixed by #10319
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) feat: ssr Related to SSR (scope) pkg: node Related to Node adapter (scope)

Comments

@xriter
Copy link

xriter commented Mar 2, 2024

Astro Info

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

Describe the Bug

The problem as described below, was first mentioned in #10184, but seems to be still an issue.

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:1934:16)
    at ServerResponse.emit (node:events:517:28)
    at emitCloseNT (node:_http_server:1020:10)
// dist/server/entry.mjs (lines: 1922-1947)

 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?

What is the TypeError: iterator.return is not a function it is referring to?

Anyone else experiencing this?

Could be related to #9614
Supposedly fixed by #10197 - but the error is still thrown for unknown reasons.

@github-actions github-actions bot added the needs triage Issue needs to be triaged label Mar 2, 2024
@lilnasy
Copy link
Contributor

lilnasy commented Mar 2, 2024

I'm not sure why the previous issue was closed, the linked PR only affects logging. As for the bug itself, I can't reproduce the it but the issue is clearly with nodejs/undici - it assumes the iterator will always have a return() function, whereas it is supposed to be optional in the iterator protocol.

Considering undici is on v6 but embedded within node 18, 20, and 21 is v5, an upstream PR won't help. We probably have to do a workaround. I'm guessing a no-op return() will do.

@lilnasy lilnasy added - P3: minor bug An edge case that only affects very specific usage (priority) feat: ssr Related to SSR (scope) pkg: node Related to Node adapter (scope) and removed needs triage Issue needs to be triaged labels Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) feat: ssr Related to SSR (scope) pkg: node Related to Node adapter (scope)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants