razshare / sveltekit-sse

Server Sent Events with SvelteKit
https://www.npmjs.com/package/sveltekit-sse
MIT License
301 stars 9 forks source link

Bun prevents connection closure #58

Open apeman76 opened 3 weeks ago

apeman76 commented 3 weeks ago

Hello,

Thanks for this library. I have been using it since atleast 0.3.7, but have not been active for a while. Im back now and am using the most recent version, I noticed that with bun --bun run dev my connection closure function never gets called, but if I run it with node (bun run dev) it does get called.

I have no idea how to fix this, do you have any idea? Im pretty much using the cleanup example now and it has this issue aswell: https://github.com/razshare/sveltekit-sse?tab=readme-ov-file#cleanup

razshare commented 3 weeks ago

Hello @apeman76 , I think you're experiencing an issue with Bun itself or SvelteKit, I'm not exactly sure which one it is.

It looks like the ReadableStream class doesn't trigger the cancel method whenever the stream is consumed.

This can be either because SvelteKit doesn't trigger it, or because Bun's got some implementation issues.

I'm more inclined to say it's because of Bun itself, because it does work fine using NodeJs.

[!NOTE] well, now it does, after patch 0.13.9

The issue is this part here

https://github.com/razshare/sveltekit-sse/blob/498ec1c393f5bdb278ccf7b27a399350b9d69ef0/src/lib/produce.js#L112-L114

it doesn't seem to trigger under Bun whenever the stream is consumed.

Here's a reproduction https://github.com/razshare/sveltekit-sse-issue-58

The code is not obfuscated with typescript stuff, so you can check the problem yourself by throwing in a console.log in there (see video below)

Peek 2024-10-06 11-32

I think you might have better luck opening an issue in https://github.com/oven-sh/bun

I'll try look into it more next week and possibly open an issue there myself explaining the situation.

If you get around to open an issue on their tracker before I do, please link back to this issue, so that I can see it referenced here and avoid creating duplicate issues, I'll do the same.

I'll leave this open for now.

razshare commented 3 weeks ago

There's also this issue opened in kit https://github.com/sveltejs/kit/issues/11751#issuecomment-2252094984

Though I don't think it's relevant anymore, because it works fine in NodeJs, it's just bun that doesn't work, and since bun promisses NodeJs compatibility it should be seen as a bun issue (I think).

apeman76 commented 3 weeks ago

Im not sure what version of bun i'm on. It might be a few weeks old, then https://github.com/oven-sh/bun/issues/6758#issuecomment-2324547731 might have fixed it. Will report back when I have the chance to be on PC

/edit Was already using bun 1.1.29, so that didnt fix it. Opened https://github.com/oven-sh/bun/issues/14390