[Libwebsockets] libwebsocket problem

Andy Green andy at warmcat.com
Fri May 24 04:04:51 CEST 2019

On May 23, 2019 6:54:57 PM PDT, "Wei, Catherine" <catherine.wei at commscope.com> wrote:
>I'm using the LWS_TO_KILL_SYNC in lws_set_timeout, since I want to
>close it immediately.

That's not an option... have a read of the documentation I pasted for ...SYNC.

What you're doing is a sequence like this

 - something happens on a wsi
 - lws calls the callback
 -    user code deletes the wsi
 - user code returns to lws
 - lws uses the now-deleted wsi

>I tried LWS_TO_KILL_ASYNC in my code and it doesn't work, do I need to

Hmmm what does it mean, 'doesn't work'?

>use LWS_TO_KILL_ASYNC and set a real timeout at the same time. I found
>the libwebsocket doesn't close it at all.

Let's have a look at the logs for that, then.

>    { 
>   auto requestPathSize = lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI);
>      if (requestPathSize == 0) {
>        KTRACE1("Rejected non-GET request");
>        result = -1; // Disconnect
>      }
>      else {
>        std::string requestPath = lws_get_complete_url(wsi);
>        auto protocol = static_cast<const char*>(in);
>        if (!protocol) {
>          protocol = NO_PROTOCOL;
>        }
>        KERROR("New connection for protocol \"%s\", path \"%s\"",
>                protocol, requestPath.c_str());
>        auto connection = static_cast<TConnection**>(user);
>        assert(ConnectionHandler);
>  *connection = new TConnection(*wsi, *ConnectionHandler, requestPath);
>        if (ConnectionHandler->HandleNewConnection(**connection)) {
>          (*connection)->SetRequestAcceptedFlag(true);
>        }
>      } 
>    }
>    return true;
>After the above code is executed, I want to close the client connection
>later, using lws_set_timeout(LWS_TO_KILL_SYNC) can disconnect the
>client connection, using lws_set_timeout(LWS_TO_KILL_ASYNC), the
>libwebsocket does nothing and client is not disconnected.
>This is the log after executing lws_set_timeout(LWS_TO_KILL_ASYNC)
>2093 09:53:30.564 hbbtv(373) Debug: Connection::Disconnect: Disconnect
>2094 09:53:30.564 hbbtv(373) None: <1B>[0m<1B>[34;1m[2019/05/24
>01:53:30:3568] DEBUG: __lws_set_timeout: 0x1d2861e0: 0 secs
>2095 09:53:30.564 hbbtv(373) None: <1B>[0m<1B>[34;1m[2019/05/24
>01:53:30:3568] DEBUG: __lws_free_wsi: 0x1d2866e0, remaining wsi 3

What does your event loop type code look like?

I mean like this kind of thing



>Best regards,
>-----Original Message-----
>From: Andy Green <andy at warmcat.com> 
>Sent: 2019年5月23日 19:31
>To: Wei, Catherine <catherine.wei at commscope.com>;
>libwebsockets at ml.libwebsockets.org
>Subject: Re: [Libwebsockets] libwebsocket problem
>Message received from external source. Exercise caution when opening
>attachments, clicking links, or exchanging information.
>On 5/23/19 7:32 AM, Wei, Catherine wrote:
>> Thanks, the api that you offered works in my case mentioned in my 
>> early email. Calling it when I disconnect the connection from server 
>> side will disconnect the connection and also send callback to the 
>> server in my case.
>> However, in another case, I found that when a client request a 
>> connection, the websocket server will receive a callback to handle
>> new connection. When the server handles the new connection, it
>> the connection directly by the api lws_set_timeout(). At this time, 
>> when I disconnect the connection from the server side using api
>> lws_set_timeout() when the client request a connection will lead to 
>> libwebsocket crash. The process in detail: 1,Client request a
>> connection   ->libwebsocket 2.Server HandleNewConnection callback,
>> and call "lws_set_timeout()", then the process of websocket server 
>> will report segment fault error like follow:
>You don't mention the callback reason nor show the code.
>You're using LWS_TO_KILL_ASYNC, right?
>#define LWS_TO_KILL_ASYNC -1
>/**< If LWS_TO_KILL_ASYNC is given as the timeout sec in a
> * call, then the connection is marked to be killed at the next timeout
> * check.  This is how you should force-close the wsi being serviced if
>* you are doing it outside the callback (where you should close by
>  * return).
>  */
>#define LWS_TO_KILL_SYNC -2
>/**< If LWS_TO_KILL_SYNC is given as the timeout sec in a
>* call, then the connection is closed before returning (which may
>* the wsi).  This should only be used where the wsi being closed is not
>  * wsi currently being serviced.
>  */

More information about the Libwebsockets mailing list