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

ReadableStream in response body not canceled when client closes connection #9068

Closed
1 task done
pilcrowonpaper opened this issue Nov 12, 2023 · 3 comments · Fixed by #9071
Closed
1 task done

ReadableStream in response body not canceled when client closes connection #9068

pilcrowonpaper opened this issue Nov 12, 2023 · 3 comments · Fixed by #9071
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) pkg: node Related to Node adapter (scope)

Comments

@pilcrowonpaper
Copy link
Contributor

pilcrowonpaper commented Nov 12, 2023

Astro Info

Astro                    v3.5.2
Node                     v20.2.0
System                   macOS (x64)
Package Manager          pnpm
Output                   server
Adapter                  @astrojs/node
Integrations             @astrojs/tailwind

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

When implementing server-sent events, cancel() is not called when client closes the connection (e.g. closes the tab).

export async function GET() {
  const stream = new ReadableStream({
    start(controller) {
      // do stuff
    },
    cancel() {
      console.log("cancelled");
    },
  });
  return new Response(stream, {
    headers: {
      "X-Content-Type-Options": "nosniff",
      "Content-Type": "text/event-stream; charset=utf-8",
      "Transfer-Encoding": "chunked",
    },
  });
}

What's the expected result?

cancelled is logged to the console

Link to Minimal Reproducible Example

https://github.com/pilcrowOnPaper/astro-readable-stream-bug

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Nov 12, 2023
@pilcrowonpaper pilcrowonpaper changed the title cancel() callback in ReadableStream not being called when connection closes cancel() callback in ReadableStream not called when connection closes Nov 12, 2023
@pilcrowonpaper
Copy link
Contributor Author

I don't think I know enough about the code base to make a PR, but fix would look something like this:

const reader = await webResponse.body.getReader();
res.on("close", () => {
   reader.cancel();
})
let result = await reader.read();
while (!result.done) {
   res.write(result.value);
   result = await reader.read();
}

@pilcrowonpaper pilcrowonpaper changed the title cancel() callback in ReadableStream not called when connection closes ReadableStream in response body not canceled when client closes connection Nov 12, 2023
@lilnasy lilnasy added - P3: minor bug An edge case that only affects very specific usage (priority) pkg: node Related to Node adapter (scope) and removed needs triage Issue needs to be triaged labels Nov 13, 2023
@tarekwiz
Copy link

@lilnasy This doesn't work on dev. Only after build.

@ruohki
Copy link

ruohki commented Jan 15, 2024

@lilnasy This doesn't work on dev. Only after build.

THIS IS SUCH A BANGER INFO ... I've been pulling my hair out about this.

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) pkg: node Related to Node adapter (scope)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants