[Libwebsockets] Repetitive LWS_CALLBACK_SERVER_WRITABLE making cpu load 99%

Shmuel Weiss sweiss at kramerel.com
Sun Jul 3 23:16:52 CEST 2016

I didn’t change anything yet. I am just trying to understand what happens.

We are monitoring our processes behavior by some routines.

An infinite loop without sleeping is appearing into 'top' with 99-100% CPU. (dummy load, because the CPU is not really busy, this is just an infinite loop, but pointing always to something wrong happening in the application).

This should not happen in programs where at some time the function should block on an epoll or a select waiting for events (even wait of 50msec get done the cpu load to 0.01%).

In case the application receive thousands of events this could be the case and effectively my CPU will be loaded by requests, processing real data.

But In my case nothing is happening on web sockets neither on the HTTP session.

My Web Client just connects to http://<myip>/<http://%3cmyip%3e/> get index.html, open 1 websocket get some data and it's done.

So why the WebSocket thread is entering into an infinite loop ?

I am debugging my callbacks and the function lws_callback_on_writable(); is not solicited into the callback (since I have nothing to send), the callback function exit with "return 0", why the callback is immediately re-called ?

WebSocket call:

[cid:image002.png at 01D1D589.5B7E8870]

HTTP call:

[cid:image004.png at 01D1D588.0886B710]

I am seeing that I am arriving to line 306 into service.c where lws_callback_on_writable(wsi) is called. Why I have falling in this scenario?


-----Original Message-----
From: Andy Green [mailto:andy at warmcat.com]
Sent: Sunday, July 03, 2016 22:18
To: Shmuel Weiss; libwebsockets at ml.libwebsockets.org
Subject: RE: [Libwebsockets] Repetitive LWS_CALLBACK_SERVER_WRITABLE making cpu load 99%

On July 4, 2016 2:00:09 AM GMT+08:00, Shmuel Weiss <sweiss at kramerel.com<mailto: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<mailto: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<mailto: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<mailto:Libwebsockets at ml.libwebsockets.org>

>> > http://libwebsockets.org/mailman/listinfo/libwebsockets

>> This mail was sent via Mail-SeCure System.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20160703/e7bca69e/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image004.png
Type: image/png
Size: 39768 bytes
Desc: image004.png
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20160703/e7bca69e/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.png
Type: image/png
Size: 97742 bytes
Desc: image002.png
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20160703/e7bca69e/attachment-0003.png>

More information about the Libwebsockets mailing list