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 cancel is not called when client disconnects #6758

Open
luccas40 opened this issue Oct 27, 2023 · 4 comments · May be fixed by #13687
Open

ReadableStream cancel is not called when client disconnects #6758

luccas40 opened this issue Oct 27, 2023 · 4 comments · May be fixed by #13687
Labels
bug Something isn't working bun:serve Bun.serve and HTTP server web-api Something that relates to a standard Web API

Comments

@luccas40
Copy link

What version of Bun is running?

1.0.7

What platform is your computer?

Debian Bullseye x64

What steps can reproduce the bug?

Running Bun with Svelte, in a API GET function:

    let keepAlive = true;
    const stream = new ReadableStream({
        start: async controller =>{
            while(keepAlive){           
                await new Promise(r => setTimeout(r, 1000));
            }
        },
        cancel: () =>{
            console.log("cancelled");
            keepAlive = false;
        }
    });
    return new Response(stream, {
        headers: {
            "Cache-Control": "no-store",
            "Content-Type": "text/event-stream",
            "Connection": "keep-alive",
        }
    });

What is the expected behavior?

Everytime the page is closed or the connection is closed by the client we should see a log on the server "cancelled".

What do you see instead?

Nothing is logged and the loop keeps running even if the page is fully closed.

Additional information

It works just fine with Node

@luccas40 luccas40 added the bug Something isn't working label Oct 27, 2023
@Electroid Electroid added the web-api Something that relates to a standard Web API label Oct 27, 2023
@Electroid Electroid changed the title ReadableStream Cancel is never called ReadableStream cancel is not called when client disconnects Oct 27, 2023
@giesf
Copy link

giesf commented Nov 30, 2023

Any news on this? :/

@ksmithut
Copy link

@giesf I'm not exactly sure how it's supposed to work, but I got something working going off of the request abort signal rather than relying on the response cancel callback:

const server = Bun.serve({
  async fetch(req) {
    const stream = new ReadableStream({
      async start(controller) {
        const encoder = new TextEncoder()
        let count = 0
        const interval = setInterval(() => {
          controller.enqueue(encoder.encode(`${count  }\n`))
        }, 1000)
        let closed = false
        function close() {
          if (closed) return
          console.log('aborted')
          closed = true
          clearInterval(interval)
          req.signal.removeEventListener('abort', close)
          controller.close()
        }
        req.signal.addEventListener('abort', close)
        if (req.signal.aborted) return close()
      }
    })
    return new Response(stream)
  }
})

Now running curl localhost:3000 will get a stream of numbers, and when you ctrl c you get the aborted log and the request artifacts get cleaned up.

Is that what you're looking for?

@TheArmagan
Copy link

Any updates?

@pekeler
Copy link

pekeler commented Jun 6, 2024

We're using SSE for the "realtime" aspect of our SPA. Once these ReadableStreams are used up, the app freezes (because server event listeners don't get unregistered). So this isn't just a blocker for going to prod, but it's making development super annoying. Can we please have an ETA for a bug fix?

@Jarred-Sumner Jarred-Sumner added the bun:serve Bun.serve and HTTP server label Sep 2, 2024
Jarred-Sumner added a commit that referenced this issue Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bun:serve Bun.serve and HTTP server web-api Something that relates to a standard Web API
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants