You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
<feature> How to gracefully shutdown the server? #31
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.
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.
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)
...
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
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.
Activity
Theldus commentedon Sep 1, 2021
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:
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.
Yes, it is clear, do not worry =).
syuez commentedon Sep 6, 2021
Thanks for help!
When i execute
git apply niceclose.patch,i got error: unrecognized input.syuez commentedon Sep 8, 2021
I know the reason.Thanks again!
Theldus commentedon Sep 8, 2021
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...
[-]How to exit program correctly[/-][+]<feature> How to gracefully shutdown the server?[/+]amdrozdov commentedon Aug 6, 2024
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):
Theldus commentedon Aug 6, 2024
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 commentedon Aug 7, 2024
Ok I see, thank you @Theldus !
Theldus commentedon Aug 7, 2024
@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.