[Libwebsockets] libwebsocket problem

Wei, Catherine catherine.wei at commscope.com
Fri May 24 03:54:57 CEST 2019

I'm using the LWS_TO_KILL_SYNC in lws_set_timeout, since I want to close it immediately.
I tried LWS_TO_KILL_ASYNC in my code and it doesn't work, do I need to use LWS_TO_KILL_ASYNC and set a real timeout at the same time. I found the libwebsocket doesn't close it at all.

The callback reason is LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION, my code:

      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);
        *connection = new TConnection(*wsi, *ConnectionHandler, requestPath);
        if (ConnectionHandler->HandleNewConnection(**connection)) {
    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 2
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

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 the 
> new connection. When the server handles the new connection, it rejects 
> 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 lws_set_timeout()
  * 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 nonzero
  * return).
#define LWS_TO_KILL_SYNC -2
/**< If LWS_TO_KILL_SYNC is given as the timeout sec in a lws_set_timeout()
  * call, then the connection is closed before returning (which may delete
  * the wsi).  This should only be used where the wsi being closed is not the
  * wsi currently being serviced.


More information about the Libwebsockets mailing list