wsServer icon indicating copy to clipboard operation
wsServer copied to clipboard

<feature> How to gracefully shutdown the server?

Open syuez opened this issue 2 years ago • 4 comments

Hi. I want do something that when the wsServer get "exit" message from client,it will exit normal. because when i use valgrind to check my code,it always show memory leak.if wsServer can exit,i don't need to press Ctrl +C

Sorry! my English is terrible. I used google translation. I hope my description is clear.

syuez avatar Aug 31 '21 07:08 syuez

Hi @syuez, Valgrind is a really great mechanism for hunting leaks, I use it in all my projects, wsServer included.

Unfortunately so far wsServer has no mechanism to 'gracefully shut down' the server. It turns out that a proper server shutdown requires a few additional steps, such as:

  • Send a close frame packet (with timeout) to all active clients.
    • wait for all clients to disconnect
  • Signals (via flag?) that the accept loop must close
  • Close the accept socket.
  • Frees any allocated memories

Due of that, I haven't worked on any solution for this yet, but it's a really good suggestion, I'll put it on my TODO list.

A quick n' dirty solution for this (don't use it in production!), would be to use this patch that I've elaborated.

You can apply it as follows:

# Clone a fresh wsServer
$ git clone https://github.com/Theldus/wsServer.git
$ cd wsServer/

# Download the patch
$ wget https://git.io/JE7ms -O niceclose.patch

# Apply it
$ git apply niceclose.patch

# Build everything and run
$ make
$ ./example/send_receive

The above patch shuts down the server upon receiving the 'exit' string from the client. However, it is worth noting that it only works for 1 client connected at a time, but it shuts down the server gracefully with no memory leaks.

Please do not use the above patch in production, it is just a draft. This feature really should be better crafted.

Sorry! my English is terrible. I used google translation. I hope my description is clear.

Yes, it is clear, do not worry =).

Theldus avatar Sep 01 '21 02:09 Theldus

Thanks for help!

When i execute git apply niceclose.patch,i got error: unrecognized input.

syuez avatar Sep 06 '21 06:09 syuez

I know the reason.Thanks again!

syuez avatar Sep 08 '21 01:09 syuez

I'm glad it worked there, I hope there are no leaks =)

Please keep this issue open as I plan to work on this feature at some point...

Theldus avatar Sep 08 '21 02:09 Theldus