Skip to content

ReadableStream.cancel never trigger #279

Closed

Description

@dinwwwh

ReadableStream.cancel should be triggered when the user cancels a response before it finishes.

Currently, only "aborted" is printted, while "cancelled" never prints when a response is canceled before completion.

"@hono/node-server": "^1.19.4",

import { serve } from "@hono/node-server";

serve({
    fetch: ({ signal }) => {
        signal?.addEventListener("abort", () => console.log("aborted"));

        return new Response(new ReadableStream({
            async pull(controller) {
                controller.enqueue(new TextEncoder().encode("Hello, World!"));
                await new Promise((resolve) => setTimeout(resolve, 1000));
            },
            async cancel() {
                console.log("cancelled");
            }
        }), {
            headers: {
                "content-type": "text/event-stream"
            }
        })
    },
    port: 3000,
});

Activity

  1. usualoma commented on Sep 25, 2025

    @usualoma
    Member

    Hi @unnoq, thank you for your comment. We will look into it.

    We may need to review the details of the following changes.
    #258

  2. usualoma commented on Sep 27, 2025

    @usualoma
    Member

    What kind of usage is ReadableStream intended for?

    As shown in the example below, it is expected that when an instance of ReadableStream no longer needs to generate data, cancel() is called, and upon being called, it should stop generating data itself.

    https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream#examples

    Examples mentioned in #239

    In the example mentioned below, an error occurs because the generation started within start() does not stop when cancel() is called.

    #239

    In response to this, #258 addressed the issue by preventing cancel() from being called. However, I believe the correct approach would have been to leave the code calling cancel() unchanged and instead modify the application side.

    How does it behave in other runtimes?

    I have prepared the repository.

    https://github.com/usualoma/hono-stream-cancel

    app.ts

    This is the implementation using pull() shown in the example for this issue.

    When executed as follows, only node-server does not output "cancel".

    # Confirmation of bun's behavior
    $ bun run --hot app.ts &
    $ curl -N http://localhost:3000/
    # Confirmation of deno's behavior
    $ deno serve app.ts &
    $ curl -N http://localhost:8000/
    # Confirmation of node-server's behavior
    $ npx tsx app-node.ts &
    $ curl -N http://localhost:3000/

    app2.ts

    This is the implementation in #239 that uses only start() without pull().

    An error occurs in bun or deno.

    https://github.com/usualoma/hono-stream-cancel/blob/main/fix-app2.patch

  3. dinwwwh commented on Sep 28, 2025

    @dinwwwh
    Author

    @usualoma Thanks for your response. I believe this is a critical issue that should be fixed as soon as possible.

    For context: I'm the author of oRPC. We have a feature called Event Iterator, and we rely on .cancel to trigger cleanup of the event iterator by invoking its .return. You can see the implementation here: https://github.com/unnoq/orpc/blob/main/packages/standard-server-fetch/src/event-iterator.ts#L226

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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