[Libwebsockets] libuv and clean shutdown

Andy Green andy at warmcat.com
Fri Mar 1 12:51:22 CET 2019



On 01/03/2019 18:39, Mateusz Stępień wrote:
> Hello,
> 
> I want to use libwebsockets library with an external event loop (libuv 
> specifically). I used lws-minimal-ws-client-echo as a base successfully, 
> however I noticed that when connection is closed for whatever reason, 
> the process does not shutdown at all. Sending SIGTERM does not help, it 
> results in log message:
> 
>      ERR: internal signal handler caught signal 15
>      ERR: lws_libuv_stop
>      ERR: lws_libuv_stop: ignoring
> 
> When I tried to run lws-minimal-ws-client-echo example unmodified with 
> --libuv option, I had the same result. The only thing that worked is to 
> manually close the associated libuv loop handle from the callback:
> 
>      case LWS_CALLBACK_EVENT_WAIT_CANCELLED:
>          uv_stop(lws_uv_getloop(lws_get_context(wsi), 0));
>          break;

You shouldn't need to get involved with that.

> The downside of that solution is that I use event loop specific API. So 
> my question is: is there any event loop agnostic way to close the 
> process in a clean way?

Since v3.0, the user code is agnostic about the event loop.

For example, -minimal-http-server-eventlib or 
-minimal-http-server-eventlib-demos work find under libuv or the other 
event libs without any event-loop specific code.

It looks like the example you tried shows up a problem with the libuv 
handle counting... the handle for the pipe is accidentally counted as 
closed twice.  The normal exit is only coming when lws counts that every 
handle opened in libuv has completed its close flow through libuv as 
well.  (Because libuv closes things asynchronously via the event loop, 
this is necessary to understand when the event loop has nothing left on 
it relevant to lws: it's also used to cleanly detach lws from a foreign 
event loop that outlives the lws instance).

I pushed a one-line patch on master that should fix it.

-Andy

> Thanks,
> Mateusz
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> https://libwebsockets.org/mailman/listinfo/libwebsockets


More information about the Libwebsockets mailing list