#Close in-flight WebSockets with --watch

1 messages · Page 1 of 1 (latest)

finite belfry
#

When using bun --watch, restarts leave WebSocket connections "open" but the new server doesn't send messages to them or receive messages from them. How can I make the server close the connections on restart like server.stop(true)? Is there something I can use to listen for restart events?

weary galleon
#

--watch should be the same as manually ctrl-c and restart, --hot is the fancy one which is more hmr and keeps js context

finite belfry
weary galleon
#

hmm, i havent looked into that before, but i'd think by killing process it should kill connections?

finite belfry
weary galleon
#

ah

#

id imagine a process.on exit should work

#

but also consider trying out --hot instead as it'll try to just keep it open!

finite belfry
#

i want to just close the ws so that the client resets itself with the server

weary galleon
#

fair

#

one of those process hooks id hope gets emitted

finite belfry
#

i've tried exit and beforeExit

weary galleon
#

damn

#

yeah im looking into it and also not seeing much

finite belfry
#

it doesn't look like i can even do server.publish() in the handler

#

removing the server makes the events work??

#

i can keep the server if i do server.unref() but then the process doesn't stay alive

#

if i try to keep it alive with a promise that never resolves then the events don't trigger either

#

which maybe they're only supposed to if the event queue is empty? but the SIGINT event doesn't fire either

#

this seems like a bug

weary galleon
#

if you're on windows and using bun run start, then i think there is some weird bug where it doesnt properly pass on the exit, but in macos they work for me™ in normal mode

finite belfry
#

i'm on macos

weary galleon
#

oh

finite belfry
# weary galleon

apparently SIGINT fires but only if i register the listener before the server.

#

and exit and beforeExit still don't work

weary galleon
#

prob as i dont have any long lasting open connections

#

seems like weird bugs

finite belfry
#

and SIGINT doesn't fire on watch restarts

weary galleon
#

yeah that looks like its just for ctrlc

#

i cant manage to get node working for those exit events fwiw

finite belfry
#

hm

weary galleon
#

nvm node does SIGTERM event for their --watch, and bun's doesnt... can you file an issue on bun's repo for that

#

and also maybe for websockets to properly close on exits (but maybe it makes it take longer 🤷)

finite belfry
#

ok if i use --hot then i can keep the websocket connections and publish messages to tell the client to close the connection