Theldus / wsServer

wsServer - a tiny WebSocket server library written in C
https://theldus.github.io/wsServer
GNU General Public License v3.0
427 stars 86 forks source link

<feature> How to gracefully shutdown the server? #31

Open syuez opened 3 years ago

syuez commented 3 years ago

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.

Theldus commented 3 years ago

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:

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 =).

syuez commented 3 years ago

Thanks for help!

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

syuez commented 3 years ago

I know the reason.Thanks again!

Theldus commented 3 years ago

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...

amdrozdov commented 2 months ago

Hello,

Looks like I've observed same issue with my valgrind test(32 parallel connections, same number of possibly lost blocks for 1 minute test and 1 hour test):

==1147076== 
==1147076== HEAP SUMMARY:
==1147076==     in use at exit: 352 bytes in 2 blocks
==1147076==   total heap usage: 3,377,961 allocs, 3,377,959 frees, 43,231,601 bytes allocated
==1147076== 
==1147076== Thread 1:
==1147076== 288 bytes in 1 blocks are possibly lost in loss record 2 of 2
==1147076==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==1147076==    by 0x40147D9: calloc (rtld-malloc.h:44)
==1147076==    by 0x40147D9: allocate_dtv (dl-tls.c:375)
==1147076==    by 0x40147D9: _dl_allocate_tls (dl-tls.c:634)
==1147076==    by 0x4B557B4: allocate_stack (allocatestack.c:430)
==1147076==    by 0x4B557B4: pthread_create@@GLIBC_2.34 (pthread_create.c:647)
==1147076==    by 0x110EAA: ws_socket (in /home/adrozdov/CLionProjects/websockets/build/test_server)
...
Theldus commented 2 months ago

Hi @amdrozdov, The 'niceclose.patch' I've provided earlier is just a quick n' dirt hack to be able to close the server. wsServer doesn't officially supports a graceful shutdown at the moment, and thats why there might be some bugs in this patch, which I wouldn't even expected to work anymore.

amdrozdov commented 2 months ago

Ok I see, thank you @Theldus !

Theldus commented 2 months ago

@amdrozdov It is important to note that you're having only 352 bytes of 'leak' in an 1-hour test with 43MiB of data allocated, possibly due to the thread itself, not the wsServer data structures.

I do intend to work on a graceful server closure, and that's why I'm keeping this issue opened, but I can't give you any deadlines. I need to see what competing libraries do and what the standard says about it, because doing an abrupt close is quite easy, but I need to check if this is actually ideal, and etc.