[Libwebsockets] libuv and clean shutdown

Andy Green andy at warmcat.com
Fri Mar 1 15:50:40 CET 2019



On March 1, 2019 9:34:57 PM GMT+08:00, "Mateusz Stępień" <mateusz.stepien at netrounds.com> wrote:
>
>On 3/1/19 12:51 PM, Andy Green wrote:
>> I pushed a one-line patch on master that should fix it.
>Thank you for the quick response, that fixes the issue with SIGTERM 
>handling.
>
>However, I can't seem to find a way to cleanly close the process from 
>within the callback. In case of lws-minimal-ws-client-echo example, if 
>there's no server to be connected to, or if the server closed the 
>connection, I would like the process to exit in a clean way. Instead I 
>can see that there's a 1 sec timer still keeping the event loop busy:

You need to back up a bit...

You changed the minimal example to get this... so what did you do?

>     ERR: CLIENT_CONNECTION_ERROR: (null)
>     INFO: lws_cancel_service
>     DEBUG: __lws_close_free_wsi: real just_kill_connection: 
>0x56523085c5b0 (sockfd 12)
>     DEBUG: elops_io_uv: 0x56523085c5b0: -2147483637
>     DEBUG: __remove_wsi_socket_from_fds: wsi=0x56523085c5b0, skt=12, 
>fds pos=1, end guy pos=2, endfd=0
>     DEBUG: elops_io_uv: 0x56523085c5b0: 11
>     DEBUG: lwsi_set_state(0x56523085c5b0, 0x1000001e)
>     DEBUG: elops_wsi_logical_close_uv: lws_libuv_closehandle: wsi 
>0x56523085c5b0
>     DEBUG: lws_libuv_closehandle: 0x56523085c5b0
>     INFO: __lws_close_free_wsi: 0x56523085c5b0: caller: 
>close_and_handled uv repeat test
>     INFO: lws_libuv_closewsi: 0x56523085c5b0
>     DEBUG: __lws_close_free_wsi_final: wsi 0x56523085c5b0: fd 12
>     INFO: __lws_header_table_detach: wsi 0x56523085c5b0: ah 
>0x565230896bc0 (tsi=0, count = 1)
>     INFO: __lws_header_table_detach: nobody usable waiting
>     INFO: _lws_destroy_ah: freed ah 0x565230896bc0 : pool length 0
>     INFO: __lws_header_table_detach: wsi 0x56523085c5b0: ah 
>0x565230896bc0 (tsi=0, count = 0)
>     INFO: rops_destroy_role_h1: ah det due to close
>     INFO: lws_vhost_unbind_wsi: vh default: count_bound_wsi 0
>     DEBUG: __lws_free_wsi: 0x56523085c5b0, remaining wsi 1
>     INFO: lws_libuv_closewsi: sa left 8: dyn left: 1 (rk 0)
>     DEBUG: lws_uv_timeout_cb
>     DEBUG: lws_uv_timeout_cb
>     DEBUG: lws_uv_timeout_cb
>     ...
>
>Is there any lws API available to close the event loop?

Pressing ^C works?

https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/http-server/minimal-http-server-eventlib/minimal-http-server-eventlib.c#n43-59

This calls lws_context_destroy() to start the staged shutdown.

SIGINT is handled by libuv directly outside the lws callback.  Looking at the eventloop-foreign example

https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/http-server/minimal-http-server-eventlib-foreign/minimal-http-server-eventlib-foreign.c

...it acheives this by having an external event-loop timer object that's again not handled by lws which starts the destroy context ball rolling from outside the lws callback itself.

It seems you need something similar as a basis to shut down lws from within lws.

-Andy

>Thanks,
>Mateusz


More information about the Libwebsockets mailing list