Bug? ESP8266WIFI unreliable - consuming memory

JanG

Member
Licensed User
Longtime User
Hi!

I'm sorry to say that, esp. since b4r is for free, but I hope I'm allowed to note that ESP8266WIFI is unreliable and not usable for a 24/7 environment...

I use the same (sample) code for wifi client as described here:
https://www.b4x.com/android/forum/threads/memory-leak-in-wifisocket-connecthost.74286/
The first connection from a client throws wifiServerSocket_NewConnection() successfully. But every following connection (from other clients) leads to less memory on the ESP8266 (Wemos D1), although the code in wifiServerSocket_NewConnection() is not fired after successful connection. But while disconnecting the first connection all wifiServerSocket_NewConnection()s are fired, but this is too late.

Do you have a solution for this? This means that sending a lot request to my device will make it crash...

Thanks for your support!
Jan
 

JanG

Member
Licensed User
Longtime User
Please upload a small project with instructions on how to reproduce it.
You find my project appended. You can reproduce by starting the b4j App twice and connect from App1 and then App2. App1 will get a connection, the connection is logged. Starting App2 will consume memory on the Wemos, but no event is thrown. After disconnection from App1, the events from App2 are fired (too late). If the connection from App1 is not disconnected, you can consume memory with a secend App2 until the Wemos crashes.
B4X:
ListenServer
Connected 1, AvailableRAM: 37224 <= App1 connected
wifiServerSocket_NewConnection <= The connection events from App2
streamData_Error
Connected 1, AvailableRAM: 36976 <= App2 connected
Connected 1, AvailableRAM: 36752 <= App2 connected
Connected 1, AvailableRAM: 36712 <= App2 connected
Connected 1, AvailableRAM: 36672 <= App2 connected
Connected 1, AvailableRAM: 36632 <= App2 connected
Connected 1, AvailableRAM: 36304 <= App2 connected
Connected 1, AvailableRAM: 36592 <= App2 connected
streamData_NewData <= App1 send data
Pos: 0
streamData_Error
MaxBufferSize: 101
wifiServerSocket_NewConnection <= Now the connection events from App2
streamData_Error
MaxBufferSize: 101
wifiServerSocket_NewConnection <= Now the connection events from App2
streamData_Error
MaxBufferSize: 101
wifiServerSocket_NewConnection <= Now the connection events from App2
streamData_Error
MaxBufferSize: 101
wifiServerSocket_NewConnection <= Now the connection events from App2
streamData_Error
MaxBufferSize: 101
wifiServerSocket_NewConnection <= Now the connection events from App2
Connected 1, AvailableRAM: 36976
 

Attachments

  • TestESP8266ServerSocket.zip
    2.7 KB · Views: 331
  • ConnectWemos.zip
    2.2 KB · Views: 342

JanG

Member
Licensed User
Longtime User
A small but important addition: It's not only that the events will be fired too late. More bad is that under a lot more requests the system is not reponding. That means no new connections accepted, the lost memory stays, but no more memory will be consumpted.

And the device is not stopped/crashed! The timers are fired. I added a check that disconnects/reconncts WIFI under this circumstances. After this reconnect the memory is back to its starting value and connections are accepted again.

[ADDITION 2]
I checked your C++ code. I think the problem is that your WiFiServerSocket class has only one item wifiClient. That means you can only have 1 connection at a time. If there is a connection, the function WiFiServerSocket::checkForClient is not called. So new connections are not noticed.
Am I right? Is this a problem by design? Do you think there will be a solution/redesign? Another solution could be stopping new server clients to avoid the memory consumption by serverClient.stop(). In b4r this is not possible, because the new connections are not passed-through to it. But this solution means only 1 connection at a time. Better would be more connections...
 
Last edited:

JanG

Member
Licensed User
Longtime User
If I use multiple WiFiServerSocket then I have to use different Ports. And the problem is not a single possible connection. For my purpose it's okay. The problem is the memory that gets smaller and smaller. At the end the sockets don't response, although the b4r app is running. If I can't rely on the available memory, if it isn't controllable, that's a big problem for a 24/7 app.
 

JanG

Member
Licensed User
Longtime User
Why are you initializing wifiServerSocket multiple times?
I didn't do that... You recommended me to do it this way... ;)
You can create multiple WiFiServerSocket and use a different one for each connection.
But to stay on track: I found the solution! No more lost memory, you can fire as many connections as you want. Only one connection is established, the others are rejected. But the solution needs some changes at your rESP8266WiFi library and at the internal Adruino ESP8266WiFi library. I added a new function that kills the connection instantly. Without this change your (changed) rESP8266WiFi library will reject all unwanted connection but the releasing of the ram lasts a bit.
Thanks to:
https://github.com/pfabri/Arduino/commit/9fac2903ebad14752bfaa2324f01e526240bc2f3
and
http://www.esp8266.com/viewtopic.php?f=33&t=3201
Changes files:
In rESP8266WiFi:
rESP8266WiFi.cpp
rESP8266WiFi.h
In ESP8266WiFi:
WiFiClient.cpp
WiFiClient.h
include\ClientContext.h
 

Attachments

  • rESP8266WiFi.zip
    7.8 KB · Views: 334
  • ESP8266WiFi.zip
    68 KB · Views: 369
Top