Page 1 of 2

[SOLVED] Web server very slow when multiple devices connected

Posted: Wed Feb 02, 2022 1:56 am
by ZacDaMan
I have been working on a webserver for the ESP32, and it's been working excellently while only a single device is connected, but as soon as I connect a second, both slow to a crawl, with pages taking almost a minute to load. Connecting a third basically freezes everything. I'm not even sure where to start trying to debug this, is this a common issue?

Re: Web server very slow when multiple devices connected

Posted: Thu Feb 03, 2022 3:41 am
by ZacDaMan
I suspect it's to do with socket handling; if 3 devices are connected to the AP but only one is accessing the webserver, that device loads everything fine - it's as soon as one of the other connected devices starts trying to load pages as well that everything gets stuck. Interestingly, the server seems to be favouring one device; usually when all 3 are connected and trying to load, one device (not always the same one) will be able to load stuff fine while the other two will take forever. I've set max sockets to 16, and tried increasing the buffer size for sending (CONFIG_LWIP_TCP_SND_BUF_DEFAULT) but no luck. Increasing the tcp server task priority and pinning it to a core didn't make any difference either.

Re: Web server very slow when multiple devices connected

Posted: Thu Feb 03, 2022 5:16 am
by ESP_Sprite
What webserver are you using? I think there are a few that only allow one connection at a time.

Re: Web server very slow when multiple devices connected

Posted: Thu Feb 03, 2022 8:04 pm
by ZacDaMan
ESP_Sprite wrote:
Thu Feb 03, 2022 5:16 am
What webserver are you using? I think there are a few that only allow one connection at a time.
I think the webserver part is based on the HTTP File Server Example. I put some code sections I suspect might be relevant in this question on Stack Overflow

Re: Web server very slow when multiple devices connected

Posted: Mon Feb 07, 2022 8:23 pm
by ZacDaMan
ESP_Sprite wrote:
Thu Feb 03, 2022 5:16 am
What webserver are you using? I think there are a few that only allow one connection at a time.
Which are the ones that allow multiple connections?

Re: Web server very slow when multiple devices connected

Posted: Tue Feb 08, 2022 1:31 am
by ESP_Sprite
ZacDaMan wrote:
Mon Feb 07, 2022 8:23 pm
Which are the ones that allow multiple connections?
If with 'HTTP File Server Example' you mean the one that is included in esp-idf, that one should support multiple connections given that in menuconfig LWIP_MAX_SOCKETS (Component config → LWIP -> Max number of open sockets) is high enough, so that probably isn't it.

What type of data are you trying to serve over the webserver? One small HTML file, a HTML file with some large assets (images)? E.g. one big blob, websockets, ...?

Re: Web server very slow when multiple devices connected

Posted: Tue Feb 08, 2022 9:42 pm
by ZacDaMan
ESP_Sprite wrote:
Tue Feb 08, 2022 1:31 am
ZacDaMan wrote:
Mon Feb 07, 2022 8:23 pm
Which are the ones that allow multiple connections?
If with 'HTTP File Server Example' you mean the one that is included in esp-idf, that one should support multiple connections given that in menuconfig LWIP_MAX_SOCKETS (Component config → LWIP -> Max number of open sockets) is high enough, so that probably isn't it.

What type of data are you trying to serve over the webserver? One small HTML file, a HTML file with some large assets (images)? E.g. one big blob, websockets, ...?
Yeah I fiddled with MAX_SOCKETS and if I dropped it down to e.g. 10 I'd get errors, it's currently set to 16. My index.html page loads a single large-ish image, otherwise a typical page will load a (small) htm(l) file, 2-4 javascript files, a .css file and a little .jpg that sits in the corner.

Writing it out that is a fair bit, but the weird thing is that the devices that freeze/get stuck will do so even if none of the other connected devices are currently requesting pages, ie everything has already loaded. The other weird thing is that if the other devices are connected to the AP, but not on any pages, ie the browser is closed, any one device can open and load pages fine (Edit, this seems inconsistent).

As mentioned, a single connected device has no problem loading everything snappily, and when multiple devices are connected and on pages, usually one will load fine while the others stay frozen, even with multiple refreshes/page navigations etc.

Re: Web server very slow when multiple devices connected

Posted: Tue Feb 08, 2022 11:10 pm
by ZacDaMan
Most of the time, a device that gets stuck will load after anywhere from 30 seconds to 2 minutes or more, and if 2 devices are both stuck they often load simultaneously

Re: Web server very slow when multiple devices connected

Posted: Wed Feb 09, 2022 2:20 am
by ZacDaMan
Attempted (failed) solutions now include:

- Setting KEEPALIVE to 0

- changing dest_addr_ip4 from INADDR_ANY to 192.168.4.1, the desired address

- pinning the socket handling task to a different core to the tcp_server_task

- adding vTaskDelay(pdMS_TO_TICKS(x)) at various points with various values of x

Stuck/hanging devices are still consistently becoming un-stuck (ie loading the single page/group of files before getting stuck again on the next load attempt) simultaneously, even if I attempt to load the pages from the devices at different times

Re: Web server very slow when multiple devices connected

Posted: Wed Feb 09, 2022 3:08 am
by boarchuz