[Libwebsockets] Repetitive LWS_CALLBACK_SERVER_WRITABLE making cpu load 99%

Andy Green andy at warmcat.com
Sun Jul 3 21:18:24 CEST 2016

On July 4, 2016 2:00:09 AM GMT+08:00, Shmuel Weiss <sweiss at kramerel.com> wrote:
>Thread still at 99%.

If the socket was writeable, and you ask to be called back again when it's writeable without doing anything, it's still writeable.  So you spin.

You don't even explain what you changed in the code you don't share, aren't you expecting a bit much?

>My HTTP callback is called 4 times with LWS_CALLBACK_LOCK_POLL and then
>my WEBSOCKET callback once (every 200msec, corresponding to my TIMEOUT
>in  lsw_service).
>But why the HTTP callback is called so many times? What means this
>LOCK_POLL event ?

It's unrelated to your problem.  The callbacks return 0 immediately and do nothing.

But what does it mean both 'thread is 99% busy' and effectively that "poll() reaches his timeout"?  Poll sitting there and then returning due to timeout means the thread is *idle* for those 200ms.  If he just does housekeeping then returns to poll() idle, how can it be 'busy'? You don't mean the thread is 1% busy do you?

Use a debugger or up the logging to debug your code and find out what it does that makes it 'busy', if that is actually what happens.


>-----Original Message-----
>From: Andy Green [mailto:andy at warmcat.com] 
>Sent: Thursday, June 30, 2016 02:24
>To: Shmuel Weiss; libwebsockets at ml.libwebsockets.org
>Subject: Re: [Libwebsockets] Repetitive LWS_CALLBACK_SERVER_WRITABLE
>making cpu load 99%
>On Wed, 2016-06-29 at 23:17 +0000, Shmuel Weiss wrote:
>>  I have to check, I am not using neither compiling the testserver.
>> Tell me if the flow is as following:
>> My Main loop is doing the following 
>> while(running)
>> {
>> 	For all my protocol do:
>> 		lws_callback_on_writable_all_protocol(context,
>> protocol);
>> 	lws_service(context,timeout); ===> here it is no more waiting.
>> }
>> My LWS Callback is doing:
>> 	Read the data send and send it to the main engine for processing
>> If lws_partial_buffered != 1
>It's not a problem, but lws guarantees if there is some partial
>buffered, you will not get the WRITEABLE callback again until it has
>been cleared, basically it uses up the writability internally to clear
>the buffered partial first.
>So you only need that check if you will loop sending things in your
>WRITABLE callback.
>> 	Check if there is something to send from the main engine and send it
>> Else
>>  	lws_callback_on_writable();
>This means you will continuously get WRITEABLE callbacks...
>> I expected to have ONWRITE called each time lws_service returns after
>> timeout, but it never blocks (except at startup until the first
>> happen).
>That is not how poll() works.  If any of his fds have active events, he
>returns immediately.  If nothing has any events, he will return anyway
>after the timeout period.
>So you are getting exactly what you asked for here I think.
>> -----Original Message-----
>> From: Andy Green [mailto:andy at warmcat.com]
>> Sent: Thursday, June 30, 2016 00:11
>> To: Shmuel Weiss; libwebsockets at ml.libwebsockets.org
>> Subject: Re: [Libwebsockets] Repetitive LWS_CALLBACK_SERVER_WRITABLE 
>> making cpu load 99%
>> On Wed, 2016-06-29 at 20:02 +0000, Shmuel Weiss wrote:
>> > Hi Andy,
>> >  
>> > A new phenomena.
>> > The  LWS main thread is quiet until I initiate the first
>> connection,
>> > where sending data and receiving data is OK but since this moment
>> the
>> > lws_service function doesn’t block until timeout and process 
>> > immediately even if no data arrived.
>> >  
>> > What I missed ?
>> Does the test server, without your code, act like that?
>> -Andy
>> > My lws callback return 0 , should I return -1 ?
>> >  
>> > thanks
>> > This mail was sent via Mail-SeCure System.
>> >  _______________________________________________
>> > Libwebsockets mailing list
>> > Libwebsockets at ml.libwebsockets.org
>> > http://libwebsockets.org/mailman/listinfo/libwebsockets
>> This mail was sent via Mail-SeCure System.

More information about the Libwebsockets mailing list