-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
WiFiClientSecure gets stuck with bad connection #3675
Comments
Could you please enable logging and post the log for the situation when this happens? In Arduino IDE: Tools > Debug Level = Core + SSL, and add The reason why i'm asking is that i'm having trouble reproducing this issue. Logs will likely contain at least some information which can provide a clue. Thanks. |
I'm using PlatformIO instead of Arduino IDE, so I don't know how to enable other debug prints than
Additional info: the connection is kept open, because the upload happens every minute or so and reopening a secure connection every time uses lots of data. The status of the connection is checked just before an upload with |
Can you try adding extra flags to CPPFLAGS? (or CFLAGS and CXXFLAGS)
Once you do that successfully, you should see lots of debug messages from WiFiClient (starting with a colon) as well as messages from axTLS. |
I suppose
and slightly later
The code is supposed to first upload the HTTP header and then the JSON data, it was stuck for 51 seconds uploading the header and 65 more seconds for the JSON, which eventually failed. |
Update: Thanks to [aignatiev] for pointing out that version [2.4.0-rc2] is now available! (I hadn't spotted this!) I can also confirm that this new [2.4.0-rc2] version fixes my below issue. (ie. with this [-rc2], a loss of SSL-internet connection does not block, but instead times-out and returns.) 20Nov17. PS. Interestingly [2.4.0-rc2] seems to produce code which is approx 5% larger than the [2.4.0-rc1], BUT which uses approx 5% LESS dynamic-memory (which is what I am short of!). PostScript: I note that "WiFiClientSecure::write" requires a significant amount of RAM ("ESP.getFreeHeap()") to work reliably. The documentation (eg. #2499) suggests it needs at least sufficient FreeHeap for a contiguous 16kB block in order to initiate an SSL connection. In addition, if sending large chunks of data, it is also necessary to ensure that you maintain sufficient FreeHeap. In my case, my code sent about 3kB of data (in 1kB chunks) successfully over an SSL link when using the UK-4G-mobile network. However when instead using the UK-ADSL-Broadband network, the SSL link failed about 90% of the time (and hung the ESP8266). I believe this issue was due to the apparently slower ADSL-network (at busy times) causing the SSL-Send-Buffer in the ESP8266 to get too large, causing the FreeHeap get so low it hung the ESP8266. My solution was to check the amount of FreeHeap before any large "WiFiClientSecure::write" chunk was sent, and if the FreeHeap was ever below approx 10kB (for my code), wait until it rose back above 10kB before performing the "WiFiClientSecure::write". (I also do this check when using the non-secure "WiFiClient::Write", but this seems to need to maintain FreeHeap levels of only approx 3kB to work reliably!) 8Dec17.
Hardware: Wemos D1 mini I am also having what is probably the same issue with WiFiClientSecure::print("....."), where the ESP8266 just hangs/blocks if the internet-connection disappears (and sometimes after a few minutes generates an exception and reboots). IE. There is no 5second timeout! It is most easily caused by simply disconnecting the internet connection (but NOT the wifi): My ESP8266 is connected to the hotspot of my mobile-phone, and then this is connected to the internet over 4G. If you just turn-off the 4G whilst the ESP8266 is in the middle of doing a sequence of WiFiClientSecure::print("....."), the ESP8266 hangs/blocks... (NB. the ESP8266 is still connected via wifi to the hotspot, but just no longer to the internet.) Note1: Interestingly, if you re-connect the 4G, say 20seconds after the ESP8266 has hung/blocked, the ESP8266 will often (although not always) start responding again. It then appears to complete the remaining WiFiClientSecure::print's, and at least some of the time, sends all the data (both before and after the hang/block) sucessfully (ie. the complete data is received at the remote end). Note2: Also note, that during the above ESP8266-hang/block, the "os_timer_setfn/os_timer_setfn" ISR routines still work, so it is possible to detect the above ESP8266-hang using a watchdog, and then perform a "ESP.restart()", however this is far from ideal. Update: Note3: Finally note that there are (two) different ways that this "WiFiClientSecure::print" causes issues, (3a) the first being as described above, where the "WiFiClientSecure::print" just never returns (ie. blocks - presumeably when its buffer gets full), and (3b) the second being that the final ""WiFiClientSecure::print" does return, but as the internet connection has failed it's internal buffer is still full of stuff, which is never cleared (even after " WiFi_Client_secure.stop()"). In the latter case, the "ESP.getFreeHeap()" is often now so small that the ESP8266 generates an exception whenever you next try to do anything with the WiFi. And the only way to free up the Heap appears to be an "ESP.restart()" in this case (3b) too!? PS1: Note that I do not believe that the above (Note 3a) hanging/blocking issue is due to running out of heap: (a) because of the above Note1 occasional successful restart and complete data transfer; and (b) as during this print process, the "ESP.getFreeHeap()" starts at 14176-bytes, drops immediately to 10976-bytes where it stays for 26 "WiFiClientSecure::print"s, then drops to 9456-bytes where it stays for another 31 "WiFiClientSecure::print"s, at which point the ESP8266 hangs/blocks. PS2. Also note that if I switch to using "WiFiClient::print"s (ie. non-secure), then the loss of internet connection does not cause the ESP8266 to hang/block. Instead, the heap starts off at 28640-bytes, immediately drops to 25440-bytes, and then stays at this level for the rest of the "WiFiClient::print"s (ie. continues to return from the "WiFiClient::print"s even after the internet has been disconnected), enabling me to subsequently check if the remote-server is still responding. A solution would be appreciated!! |
I'm facing the same issue. I've tried almost (I hope) everything to fix it with no success. ESP8266 just hangs for no good reason while trying to connect the server (client.connect(HOST, PORT)). It can take place after 20 min, 1h, 4h, and so on... The same happens with the time the ESP8266 remains hanging. I mean... sometimes it can take 10 min and sometimes it can last 2 hours. For some reason, as @Rob58329 has mentioned, in the meantime, if I turn off/on the internet and re-connect, the ESP8266 starts responding. Please @igrr , let me know if you need more information. Thank you in advance. |
@machadoroger from your description it doesn't look like the same issue. The issue described in the original post is related to writes not being handled correctly. In you case it is the connection phase. Are you using WiFiClientSecure or WiFiClient? |
In the meantime I tried to use this code with 2.4.0-rc2 and it seems that I cannot reproduce this issue with it. The current code has been updated and has a payload of around 1k, but I still managed to get it stuck with 2.3.0 and with 2.4.0-rc2 it just seems to work. P.S.
It should work as long as the branch exists. |
@igrr I seem to have a similiar problem which can be reproduced (and fixed?) easily. I use WifiClientSecure to connect to a web server and to download a file. It seems that any calls to client (at least client->connected() and client->available()) are blocking indefinitely. I was able to fix this blocking behavior by removing SSL_READ_BLOCKING from ssl_ctx_new() fc. in WiFiClientSecure.cpp SSLContext() class constructor. And also noticed that WiFiClientSecure.h is missing timeout parm. in connect() fc., so I guess there is no way to specify connect failed timeout. Thank you. Also created Pull Request #3872. |
My question is how can I make WiFiClientSecure client.connect not hang during bad connection. I get timeouts much longer than defined (
|
BearSSL is merged in #4273 , with alternate BearSSL::WiFi* classes. Although axtls-based classes are still available and even the default, they are planned for deprecation and then retirement, hence won't be fixed. Any issues with BearSSL-based classes should be reported in new issues. |
|
@macedolfm , what @d-a-v was trying to say is you're posting about ESP32 problems on the ESP8266 Arduino site. Pop to https://github.com/espressif/arduino-esp32 if you want help, because we can't do anything for you here unless you get that ESP8266. :) |
Content-Length header shall be equal to the body length. |
Basic Infos
Hardware
Hardware: ESP-12
Core Version: 2.3.0
Description
The problem is that WiFiClientSecure gets stuck when the WiFi connection is weak. It just gets stuck for a random amount of time, usually it's just less than a minute or two, but at least once it was stuck for 20 minutes. It does not restart, it does not print any debug messages, it's just stuck. The test with 20 minutes was done by using a phone as a hotspot and slowly walking away from the ESP, the ESP continued to be stuck even when the phone was brought closer, the ESP continued to function normally only after the hotspot was switched off. I also tried to apply the fix listed in #3537, but it didn't seem to help.
Additional info: I'm making a sensor that logs data to flash memory whenever there is no internet, and uploads this data to Amazon AWS Lambda function whenever there is internet. The samples should be taken arond every 16 seconds and these random delays are no good. Would also be nice to control the timeouts, because the data can always be resent from memory. Please help me to fix this problem or give any advice how to overcome this.
Sketch
Debug Messages
The text was updated successfully, but these errors were encountered: