Skip to content

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

@pilcrowonpaper

Description

@pilcrowonpaper

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P3: minor bugAn edge case that only affects very specific usage (priority)pkg: nodeRelated to Node adapter (scope)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions