libwebsockets icon indicating copy to clipboard operation
libwebsockets copied to clipboard

lws callbacks LWS_CALLBACK_FILTER_HTTP_CONNECTION or LWS_CALLBACK_HTTP not being called under high load with locust.io

Open deionut opened this issue 5 years ago • 7 comments

I am using LWS to implement a web server. I have tested my web server with 6 different browsers running in parallel and everything is fine.

However for stress testing I used locust.io scripts to test my web server with 30 different users in parallel. I decorate each URL on the locust side with a short uid so in this way I can track each request on the server side.

The first thing I do in the lws_callback function is to log it, something like this:

int MyHttpServer::lws_http_cb(struct lws* aLWS, enum lws_callback_reasons aReason, void* aUserData, void* aInput, size_t aInputLen)
{
  if(aInputLen > 0)
  {
    lwsl_user("--lws_http_cb(): reason %s; len=%d; in='%.*s'; aLWS=%p; aUserData=%p\n", LWSUtil::LWSCbReason2Str(aReason).c_str(), aInputLen, aInputLen, (char*) aInput, aLWS, aUserData);
  }
  else
  {
    lwsl_user("--lws_http_cb(): reason %s; len=%d; aLWS=%p; aUserData=%p\n", LWSUtil::LWSCbReason2Str(aReason).c_str(), aInputLen, aLWS, aUserData);
  }

Under moderate throughput (1-15 locust.io users in parallel) everything is fine, however from > 20 users sometimes the requests fail on locust.io side with a return status_code of 0. On the server side I can not find the URLs that fail in the logs. NO entries at all in the logs.

Why this might be?

P.S. The logs show in around the time frame of the fails a gap of about 2 seconds when no output is in the logs. When new output is present after this gap I see entries for LWS_CALLBACK_EVENT_WAIT_CANCELLED

Thanks, Ionut

deionut avatar Apr 13 '21 13:04 deionut

It sounds like the remote client either cannot connect at tcp level, or cannot negotiate the tls tunnel, assuming that's what you're doing. I assume you're using one context / pt / event loop, ie, LWS_MAX_SMP is at the default of 1 and we don't have to think about SMP.

There are some OS level settings about listen socket accept queue length, this is how many new incoming connections the OS will queue on the listen socket before rejecting them, aka listen backlog

https://levelup.gitconnected.com/linux-kernel-tuning-for-high-performance-networking-high-volume-incoming-connections-196e863d458a

in lws we set it at listen() to SOMAXCONN, it's 4096 on my Fedora box. But back in the day 128 was a popular OS limit apparently, it might be related to that.

Check your uname -n, the number of fds the lws process is allowed, it's printed in the early logs from lws.

lws-team avatar Apr 13 '21 14:04 lws-team

... also

P.S. The logs show in around the time frame of the fails a gap of about 2 seconds when no output is in the logs. When new output is present after this gap I see entries for

...you might want to run perf on it and try to see what happens during that 2s. Everything in lws itself should be nonblocking, except for client DNS lookups (and you can switch to LWS_WITH_SYS_ASYNC_DNS to solve that). It may be your user code is blocking the event loop, or the OS is choked for memory and swapping (track it with top) or somesuch. EVENT_WAIT_CANCELLED just means someone (lws itself perhaps) called lws_cancel_service(), it doesn't cost anything much by itself.

lws-team avatar Apr 13 '21 14:04 lws-team

I am running on Windows, below is the output from starting LWS with all the logs enabled. What is the number of fds the lws process is allowed? Thanks.

[2021/04/13 15:26:14:7173] N: LWS: 4.1.99-, loglevel 4095 [2021/04/13 15:26:14:7183] N: NET CLI SRV H1 WS IPv6-absent [2021/04/13 15:26:14:7203] I: Event loop: poll [2021/04/13 15:26:14:7213] D: _realloc: size 7240: context [2021/04/13 15:26:14:7223] D: _realloc: size 128: lws_metric_create [2021/04/13 15:26:14:7233] D: _realloc: size 129: lws_metric_create [2021/04/13 15:26:14:7253] D: _realloc: size 129: lws_metric_create [2021/04/13 15:26:14:7263] D: _realloc: size 129: lws_metric_create [2021/04/13 15:26:14:7273] D: _realloc: size 131: lws_metric_create [2021/04/13 15:26:14:7293] D: _realloc: size 134: lws_metric_create [2021/04/13 15:26:14:7303] D: _realloc: size 126: lws_metric_create [2021/04/13 15:26:14:7323] D: _realloc: size 480000: fds table [2021/04/13 15:26:14:7343] I: ctx: 6288B (2192 ctx + pt(1 thr x 4096)), pt-fds: 30000, fdmap: 480000 [2021/04/13 15:26:14:7383] I: http: ah_data: 8192, ah: 976, max count 30000 [2021/04/13 15:26:14:7403] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7427] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7446] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7467] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7486] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7506] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7530] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7549] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7569] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7579] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7599] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7609] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7619] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7629] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7639] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7659] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7708] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7748] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7808] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7840] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7850] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7870] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7880] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7900] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7910] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7932] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7942] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7962] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:7987] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:8007] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:8024] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:8034] D: _realloc: size 240000: win hashtable [2021/04/13 15:26:14:8064] D: _realloc: size 952: __lws_wsi_create_with_role [2021/04/13 15:26:14:8088] D: lws_role_transition: : wsistate 0x200, ops pipe [2021/04/13 15:26:14:8098] N: ++ [wsi|0|pipe] (1) [2021/04/13 15:26:14:8108] I: lws_plat_pipe_create: cancel UDP skt [2021/04/13 15:26:14:8148] D: event pipe fd 1460 [2021/04/13 15:26:14:8158] D: __insert_wsi_socket_into_fds: [wsi|0|pipe]: tsi=0, sock=1460, pos-in-fds=0 [2021/04/13 15:26:14:8178] I: Compiled with OpenSSL support [2021/04/13 15:26:14:8198] I: Doing SSL library init [2021/04/13 15:26:14:8218] I: canonical_hostname = MY-PC [2021/04/13 15:26:14:8224] D: _realloc: size 776: lws_create_vhost [2021/04/13 15:26:14:8234] N: ++ [vh|0|default] (1) [2021/04/13 15:26:14:8244] D: _realloc: size 48: vh paths [2021/04/13 15:26:14:8254] D: _realloc: size 112: vhost-specific plugin table [2021/04/13 15:26:14:8274] D: _realloc: size 24: same vh list [2021/04/13 15:26:14:8313] D: _realloc: size 131: lws_metric_create [2021/04/13 15:26:14:8343] D: _realloc: size 131: lws_metric_create [2021/04/13 15:26:14:8363] I: Creating Vhost 'default' port 8888, 1 protocols, IPv6 off [2021/04/13 15:26:14:8373] N: Vhost 'default' using TLS mode [2021/04/13 15:26:14:8393] I: SSL options 0x2520004 [2021/04/13 15:26:14:8413] U: --lws_http_cb(): reason LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS; len=0; aLWS=00000161F35B4E60; aUserData=00000161F3F51B60 [2021/04/13 15:26:14:8453] I: Server 'default' advertising ALPN: http/1.1 [2021/04/13 15:26:14:8463] D: _realloc: size 72: client ctx tcr [2021/04/13 15:26:14:8473] I: lws_tls_client_create_vhost_context: vh default: created new client ctx 0 [2021/04/13 15:26:14:8493] I: created client ssl context for default [2021/04/13 15:26:14:8503] U: --lws_http_cb(): reason LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS; len=0; aLWS=00000161F35B4E60; aUserData=00000161F3F551A0 [2021/04/13 15:26:14:8543] D: _realloc: size 952: __lws_wsi_create_with_role [2021/04/13 15:26:14:8553] D: lws_role_transition: : wsistate 0x200, ops listen [2021/04/13 15:26:14:8563] D: _lws_vhost_init_server: lws_socket_bind says 8888 [2021/04/13 15:26:14:8583] D: lws_vhost_bind_wsi: vh default: wsi listen/http, count_bound_wsi 1 [2021/04/13 15:26:14:8623] D: __insert_wsi_socket_into_fds: : tsi=0, sock=1472, pos-in-fds=1 [2021/04/13 15:26:14:8653] N: ++ [wsi|1|listen|default||8888] (2) [2021/04/13 15:26:14:8673] I: Listening on port 8888 [2021/04/13 15:26:14:8683] I: mem: per-conn: 952 bytes + protocol rx buf [2021/04/13 15:26:14:8703] D: lws_cancel_service [2021/04/13 15:26:14:8723] U: --lws_http_cb(): reason LWS_CALLBACK_GET_THREAD_ID; len=0; aLWS=00000161F35B4E60; aUserData=0000000000000000 [2021/04/13 15:26:14:8773] D: _lws_state_transition: system: changed 1 'CONTEXT_CREATED' -> 2 'INITIALIZED' [2021/04/13 15:26:14:8783] D: _lws_state_transition: system: changed 2 'INITIALIZED' -> 3 'IFACE_COLDPLUG' [2021/04/13 15:26:14:8803] D: _lws_state_transition: system: changed 3 'IFACE_COLDPLUG' -> 4 'DHCP' [2021/04/13 15:26:14:8832] D: _lws_state_transition: system: changed 4 'DHCP' -> 5 'CPD_PRE_TIME' [2021/04/13 15:26:14:8853] D: _lws_state_transition: system: changed 5 'CPD_PRE_TIME' -> 6 'TIME_VALID' [2021/04/13 15:26:14:8863] D: _lws_state_transition: system: changed 6 'TIME_VALID' -> 7 'CPD_POST_TIME' [2021/04/13 15:26:14:8883] I: lws_state_notify_protocol_init: doing protocol init on POLICY_VALID [2021/04/13 15:26:14:8902] I: lws_protocol_init [2021/04/13 15:26:14:8922] U: --lws_http_cb(): reason LWS_CALLBACK_PROTOCOL_INIT; len=0; aLWS=00000161F35B4E60; aUserData=0000000000000000 [2021/04/13 15:26:14:8952] D: _lws_state_transition: system: changed 7 'CPD_POST_TIME' -> 8 'POLICY_VALID' [2021/04/13 15:26:14:8962] D: _lws_state_transition: system: changed 8 'POLICY_VALID' -> 9 'REGISTERED' [2021/04/13 15:26:14:8992] D: _lws_state_transition: system: changed 9 'REGISTERED' -> 10 'AUTH1' [2021/04/13 15:26:14:9012] D: _lws_state_transition: system: changed 10 'AUTH1' -> 11 'AUTH2' [2021/04/13 15:26:14:9022] D: _lws_state_transition: system: changed 11 'AUTH2' -> 12 'OPERATIONAL' [2021/04/13 15:26:14:9042] I: lws_state_transition_steps: CONTEXT_CREATED -> OPERATIONAL [2021/04/13 15:26:14:9052] U: --lws_http_cb(): reason LWS_CALLBACK_EVENT_WAIT_CANCELLED; len=0; aLWS=00000161F35B4E60; aUserData=0000000000000000

deionut avatar Apr 13 '21 14:04 deionut

It says 30,000, but windows / WSA is not competitive with Linux for serving at all.

Out of the box IIRC it only supports 64 sockets.

At any rate I can't help you with windows unless there's something lws should be doing better, if it's possible you should think about a linux or bsd server.

lws-team avatar Apr 13 '21 14:04 lws-team

Thanks!

deionut avatar Apr 13 '21 14:04 deionut

Initially I implemented my webapp using libmicrohttpd and then moved to libwebsockets because of the built-in implementation of http2 and use of OpenSSL for https. I redid the locust.io tests with the old version based on libmicrohttpd and I do not get this issue where requests fail on the client side with status_code 0 ( I don't get any issues). Of course there may be many integration conditions that changed between the 2 versions of the server code that may affect the tests but perhaps LWS needs more testing on Windows...

deionut avatar Apr 19 '21 10:04 deionut

perhaps LWS needs more testing on Windows...

I can't test lws with your user code, right?

If you think something broken in lws, adapt one of the provided examples so the problem can be reproduced.

But AFAIK WSAPoll() is the limitation.

lws-team avatar Apr 19 '21 11:04 lws-team