Skip to content

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

Open

Description

@syuez

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.

Activity

  1. Theldus commented on Sep 1, 2021

    @Theldus
    Owner

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

  2. syuez commented on Sep 6, 2021

    @syuez
    Author

    Thanks for help!

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

  3. syuez commented on Sep 8, 2021

    @syuez
    Author

    I know the reason.Thanks again!

  4. Theldus commented on Sep 8, 2021

    @Theldus
    Owner

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

  5. changed the title [-]How to exit program correctly[/-] [+]<feature> How to gracefully shutdown the server?[/+] on Apr 19, 2022
  6. amdrozdov commented on Aug 6, 2024

    @amdrozdov

    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)
    ...
    
  7. Theldus commented on Aug 6, 2024

    @Theldus
    Owner

    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.

  8. amdrozdov commented on Aug 7, 2024

    @amdrozdov

    Ok I see, thank you @Theldus !

  9. Theldus commented on Aug 7, 2024

    @Theldus
    Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions