-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Memory exhaustion due to erroneous read size #26
Comments
Interesting. The MAX value you are seeing is actually Also, I've added a I will look at it and will keep you updated. Gil. |
I thought that might be the case but didn't want to presume! Let me know if I can help with the debugging. |
Sure,
client.setFragmentsPolicy(FragmentsPolicy_Notify); Websocket servers often use fragmented messages to send large amounts of data. I aggregate those fragments inside the library's memory and then pass it as a complete message. Adding that line will cause each fragment to be passed to the callback, thus (hopefully) saving plenty of memory. If the esp32 is running out of space, this might help. I had to reset my PC this weekend, so I'm still setting everything up. Thanks, |
I've worked on your issue, I believe it is fixed now. Please look at the branch bugfix/unchecked_reads_issue_26 It should really improve error handling and memory management. Please let me know if it's working for you (with your new board) before I merge it. Gil. |
This works beautifully! Apologies I didn't have time to help you debug it. Thank you so much for all your help. If you have a wishlist/patreon/similar, please let me know. |
Thank you! Iv'e released version 0.4.10 with the patch. Your time invested in opening your issues and helping the library evolve is all I need! (You could give the library a star, it might help others find it 😅) Have an awesome day, |
I've found yet another bug while working on receiving extremely large websocket messages, however I'm not entirely convinced it's size related.
The same test program as from the previous large message issue should reproduce the problem:
I obtained an ESP32 WROVER module with 8mb of ram, which I know all the messages should easily fit into. At first I thought the allocator was failing to use the external memory as it carried on crashing, but after some digging it's revealed an odd bug.
It seems that the following segment of code in
websockets_endpoint.cpp
is the cause of the memory exhaustion:ArduinoWebsockets/src/websockets_endpoint.cpp
Lines 177 to 181 in a3da335
Inserting a
print
statement to show the size ofnumReceived
reveals normal behaviour right up until the crash, at which point the value ofnumReceived
is set to the maximum value foruint32_t
(4294967295) and the loop reads garbage data until the device runs out of memory.Changing
uint32_t
touint64_t
for the return ofclient.read()
resulted in the max value foruint64_t
being returned instead.This is a log & backtrace of the above program running, with
websockets_endpoint.cpp
patched to print out the size ofnumReceived
:to_read
is always a sane value, so I also tried adding an extra check to the read loop to ensure thati <= to_read
and while this prevented the memory exhaustion it resulted in being disconnected from the server shortly afterward, so clearly the session state is messed up.This should be easily reproducible on an ESP32 with limited memory as the exhaustion trigger happens before any significant memory usage, although I've not tested this.
Let me know if you need any more information, or want me to test things.
The text was updated successfully, but these errors were encountered: