[Libwebsockets] Which are the events received by the callback when "lws_context_destroy" is called

Andy Green andy at warmcat.com
Tue Nov 19 11:44:30 CET 2019



On 11/19/19 10:30 AM, Gilles Printemps wrote:
> Hi,
> I would like to understand which events should be received by the 
> callback when "lws_context_destroy" function is called.
> 
> Currently, in debug mode, I have the following trace:
> 
>     [2019/11/19 10:01:57:4407] I: lws_context_destroy: ctx 0xffff99a03280
>     [2019/11/19 10:01:57:4408] I: lws_destroy_event_pipe
>     [2019/11/19 10:01:57:4409] I: __lws_close_free_wsi: 0xffff98203500:
>     caller: ctx destroy
>      >>> Reason [30] wsi [0xffff98203500] session [(nil)] data [(nil)]
>      >>> Protocol [0xffff9a202820]
>     [2019/11/19 10:01:57:4411] I: lws_vhost_unbind_wsi: vh default:
>     count_bound_wsi 0
>     [2019/11/19 10:01:57:4412] I: lws_vhost_destroy1
>     [2019/11/19 10:01:57:4413] I: lws_context_destroy2: ctx 0xffff99a03280
>      >>> Reason [28] wsi [0xffffe73b2750] session [(nil)] data [(nil)]
>      >>> Protocol [0xffff9a202820]
>     [2019/11/19 10:01:57:4414] I: __lws_vhost_destroy2: 0xffff98203880
>     [2019/11/19 10:01:57:4415] I:   __lws_vhost_destroy2: Freeing vhost
>     0xffff98203880
>     [2019/11/19 10:01:57:4416] I: lws_context_destroy3: ctx
>     0xffff99a03280 freed
>     WebSocket Server stopped 
> 
> 
> But in release mode (with -O2 optimisation), I have something different 
> and strange:
> 
>      >>> Reason [1] wsi [0xaaaaedae9eb0] session [(nil)] data [(nil)]
>      >>> Protocol [(nil)]
>      >>> Reason [-8707407386353682176] wsi [(nil)] session
>     [0xffffa4b03928] data [0xffffa431c8c0]
>     Segmentation fault (core dumped)

Well, wsi shouldn't be NULL.  For the cases where the event is not 
actually related to a wsi, like a systemwide or vhost related event, lws 
keeps a fake wsi in the per-thread struct in the context so it can set 
the content, vhost and maybe the protocol in there and pass that as the 
wsi for the callback.

> It is ending by a segmentation fault because I'm trying to retrieve the 
> protocol with a wsi which is equal to NULL a the entrance of the callback.
> 
>     int WebSocketServer::callback(struct lws *wsi,enum
>     lws_callback_reasons reason,void *session,void *data,size_t len) {
>        const struct lws_protocols *protocol=NULL;
>        Tools::debug(">>> Reason [%ld] wsi [%p] session [%p] data
>     [%p]\n",reason,wsi,session,data);
>        Tools::debug(">>> Protocol [%p]\n",lws_get_protocol(wsi));
> 
>   Seem also the reason is wrong!!!
> 
> Where did the strange behavior can come from?

Dunno.  Does it exist on the lws minimal examples?

What I'd do is run it under valgrind, not so much for the leak detection 
but it also allows it to spew backtraces as soon as something goes off 
the rails.

-Andy


More information about the Libwebsockets mailing list