[Libwebsockets] help on wsi pointer usage outside http callback

Andy Green andy at warmcat.com
Thu Nov 28 12:04:06 CET 2019

On 11/28/19 10:53 AM, 이성훈(Sunghoon Lee) wrote:
> Hello,
> Below is what I have written before.
> https://libwebsockets.org/pipermail/libwebsockets/2019-November/008154.html
> Thank you for the kind reply before. I have not learned how to reply in 
> the mailing list yet.
> I have several additional questions.
> I am currently porting libwebsockets into the my existing server.
> https://libwebsockets.org/pipermail/libwebsockets/2014-September/001357.html 
> This link says that wsi should be handled only inside callback.
> 1.Can I deliver/pass “wsi”pointer received from my http callback to 
> other threads and call lws_write(wsi, …) in those other threads like 
> below? Is wsi pointer thread safe?

The only thing that is thread safe in lws is lws_cancel_service() api. 
You cannot randomly call lws apis from other threads.

> I observe a crash sometimes inside lws_write which is hard to find the 
> reason.
> lws_write(wsi_pointer,
> (unsigned char *)(buffer+LWS_SEND_BUFFER_PRE_PADDING),

For many years the lws examples show to use LWS_PRE instead of 
LWS_SEND_BUFFER_PRE_PADDING... it's a good idea to base your code on 
current lws examples because they show the best way to do things.

> Buffer Length,
> 2.Let's assume that using wsi pointer is allowed to access from other 
> thread.

It isn't.

> Then, I'd like to close client connection from different threads in my 
> streaming server instead of closing it from the inside of my http callback.
> How can I close a specific client from other threads which are different 
> from my http callback.
> 3.Let's again assume that using wsi pointer is allowed to access from 
> other thread.
> Do I have to monitor writable status of the wsi from http callback and 
> let other threads know that and call lws_write from other threads?
> Is there any way which I can use like (xxx.revents &POLLOUT) from 
> different threads through wsi pointer?
> 4.If inside lws_xxx function crashes, how do you usually debug?

The way to develop with lws is cut and paste from an existing minimal 
example.  These should always work cleanly from the start, for what they 
do.  They're licensed CC0 / Public Domain so you can just copy them.

Eg this shows how to do a websockets server with threads


In particular look at


> (P.S: I don’t know how to add additional question in the added answer. 
> Please teach me how to do that as well J)

Just reply to email and add your text under the original text quoting.


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

More information about the Libwebsockets mailing list