-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
High load websockets causing crashes + SOLUTION #1334
Comments
That's an interesting solution, I like it, thanks for posting! I agree with your comment about how a really nice app is possible with the combination of libraries you mention. My web socket app does not have a high message rate, just some knobs and buttons on a browser for human interaction. At first I had similar watchdog timer crashes and learned (after days of debug) that doing almost anything in the message handler causes the watchdog to bite. My solution is maybe not so fast as yours but maybe more flexible. The message handler triggers a timer Ticker (with 1millisec delay) that calls a function and passes a JSON argument. The function asynchronously processes the arg, then calls a client notifier to socket.textAll to update all clients. It requires some care to prevent successive timer calls from short-circuiting previous calls but it's not possible to hang the main loop. It has not rebooted itself in nearly a year of adding code. Here is a sample message handler, where the incoming message has an action and optional value
|
So, quick update here. At high message rates, it was still occasionally crashing (like every 12h or so, at 100 messages/sec) I changed to this fork (https://github.com/nthd/ESPAsyncWebServer) that has some stability fixes applied and that helped quite a bit. It still crashes occasionally under higher load, but not nearly as much. Under light load I haven't seen any crashes in a while. |
see #1363 to see the code that causes mentioned crash |
looks like the fork also suffers from this bug |
after closer look, the fork will crash but after much higer load as you said...it might be related to active number of connections. maybe in between calls ws.cleanupClients which I do only once per second. unfortunately, platformio esp exception decoder does not seem to be able to decode the crash even with debug build |
I believe this will be fixed after applying this set of patches Aircoookie/WLED#3382 (comment), which I plan to do soon on top of nthd fork |
I've done some more digging, which might be helpful to someone. There doesn't seem to be any leak when connection is open and websockets data is streamed, but there is a per-connection leak after connection is closed. So a simple I will be looking for more luck with Espressif native websockets server.... |
@ninja- funny you say that.... I've been working on a new project that is a wrapper in the style of ESPAsyncWebServer but uses the native ESP-IDF server calls under the hood. literally just wrote most of it in the last week but its functional and almost feature complete. Here's the link: https://github.com/hoeken/PsychicHTTP/ |
@hoeken that's some really nice code - looks like we've been duplicating effort of porting the websockets demo code to c++, a quite slow and painful process :) |
@ninja- thanks man. yeah the esp-idf library is great, but its not exactly user friendly. it does make a fantastic base for a proper HTTP server though. I don't know where you are in your process, but maybe we can collaborate. I just have a few things I'd like to sort out (uploads) before I make a v1.0.0 release of my library. My personal project Yarrboard has now been running on it for a couple days flawlessly, and I also did a load test on the example code which went well . There are definitely some performance gains to be made, but its definitely acceptable for now, and more importantly rock solid. |
I've got caught up in a couple issues today:
Sooo only now I got it to compile and upload with espressif 1.5.2, so I am looking forward to testing the stability with WiFI, BLE and WebSockets enabled... |
@ninja- have you seen the new v3.0 beta of the official esp-idf/arduino framework? looks like its using latest v5.1.x branch of esp-idf. https://blog.espressif.com/announcing-the-arduino-esp32-core-version-3-0-0-3bf9f24e20d4 What platform are you using? Currently I'm only targeting Arduino, but it would be cool to have pure esp-idf support as well. This is my setup in platformio: platform = espressif32 If you want, lets move this thread to the psychichttp github issues tracker |
I do yes, but it's not compatible with platformio without using a fork.
|
If anyone is following this thread, PsychicHttp v1.0 has been released. Feel free to give it a try and let me know your feedback. https://github.com/hoeken/PsychicHTTP/ |
I'm writing a server that handles websocket connections using ESPAsyncWebserver and ArduinoJSON. As part of that, I was testing to see how many messages it could handle and I was just generally having things crash a lot. Often, I would get a message like this:
After digging around online, I found this good suggestion on Stackoverflow.
Based on that, I implemented something in my code sort of like below. After that, the crashes are pretty much gone unless I try to push it past 150 messages per second. Its more than happy to chug along at 100 messages per second.
When you do a lot of work in the callback, you run the risk of a watchdog timer reset. If you change the callback to basically just take the message and mark it for processing, then that callback becomes very fast. You then process the message and respond in your main loop which can take as long as you like.
I haven't seen anything in the documentation, so I thought I'd share this with you all as it might save you some headache.
As a side note, running the ESPAsyncWebserver + websockets + ArduinoJSON + serving static html+js+css through SPIFFS is such a killer combo. You can build really nice, responsive websites. I absolutely cannot believe that this thing is running on a little microcontroller. So good. If you want to see how I'm doing it, check out Yarrboard.
The text was updated successfully, but these errors were encountered: