Open RosiersRobin opened 2 years ago
This seems to be the same issue as #6165. It should work in 2.0.3-rc1
Yes, please try to retest it on v2.0.3-rc1.
Tested it on v2.0.3-rc1
, then I have the folloing issue;
First calls work as expectedd (just like above)
Second call fails with error code -2 BUT the thrid call of that same URL works just fine, it then alternates bewteen 200 and -2 of status codes returned.
So;
works doesnt works doesnt works ....
Update: If I do the call every 5 minutes, it breaks, if I do it every 5 seconds, it works just fine...
Update: If I do the call every 5 minutes, it breaks, if I do it every 5 seconds, it works just fine...
Can you track the free heap? Have not tested the latest code myself, but the older code had a memory leak in loading the TLS code.
See my (still pending) PR for my own project to include TLS support. https://github.com/letscontrolit/ESPEasy/pull/3788/files These files in particular (especially ESPEasy_ssl_client.h/.cpp) :
These are taken from this lib and adapted to prevent this memory leak as the mbettls context objects are not dealt with properly. The pointers as members are not initialized correctly, thus if you destruct them you will see a crash and if you don't, you will run out of memory.
This was added to our Roadmap, we will investigate it further. Thanks for your contribution.
A request would work, it s the content that just not right.
Update: If I do the call every 5 minutes, it breaks, if I do it every 5 seconds, it works just fine...
Can you track the free heap? Have not tested the latest code myself, but the older code had a memory leak in loading the TLS code.
See my (still pending) PR for my own project to include TLS support. https://github.com/letscontrolit/ESPEasy/pull/3788/files These files in particular (especially ESPEasy_ssl_client.h/.cpp) :
- src/src/Helpers/ESPEasy_WiFiClientSecure.h
- src/src/Helpers/ESPEasy_WiFiClientSecure.cpp
- src/src/Helpers/ESPEasy_ssl_client.cpp
- src/src/Helpers/ESPEasy_ssl_client.h
These are taken from this lib and adapted to prevent this memory leak as the mbettls context objects are not dealt with properly. The pointers as members are not initialized correctly, thus if you destruct them you will see a crash and if you don't, you will run out of memory.
Sorry for the late reply.
I have checked memory usage and that didn't seem to be the rpoblem. I even extracted the code into a blank project and tested it the exact same way. Same results.
Sockets can be reused for multiple requests with HTTP1.1, but 5 minutes is a really long time - is it possible the server is just closing it? You said it works ok at a 5s interval.
If your interval is going to be that long, maybe just move the declaration of the httpclient inside the sendHeartBeatApiCall() function?
OK, found another issue with the HTTPClient on ESP32. While searching for a related issue, this one came up and it is probably related. If not, I will make a new issue.
Problem is located here: https://github.com/espressif/arduino-esp32/blob/3f69bcfca4225ead422f322e524008408ae5e170/libraries/HTTPClient/src/HTTPClient.cpp#L372-L410
When calling end()
the disconnect()
call is probably doing nothing since it is likely no longer connected.
This results in new calls made using the HTTPClient to not work untill some timeout has occurred or some instance of WiFiClient has made a connection to some other host.
By simply calling stop()
on the WiFiClient object, this is no longer an issue.
Not sure if the best fix should be in this HTTPClient::disconnect()
function, but I guess it is at least a fix for this problem.
However the true problem is probably in the WiFiClient code.
IMHO this code should be executed regardless the connected()
state.
But then there should be an extra check on whether _client
isn't a nullptr.
So it should probably something like this (untested)
void HTTPClient::disconnect(bool preserveClient)
{
if(connected()) {
if(_client->available() > 0) {
log_d("still data in buffer (%d), clean up.\n", _client->available());
_client->flush();
}
} else {
log_d("tcp is closed\n");
}
if(_reuse && _canReuse) {
log_d("tcp keep open for reuse");
} else {
log_d("tcp stop");
if (_client != nullptr) _client->stop();
if(!preserveClient || _client == nullptr) {
_client = nullptr;
#ifdef HTTPCLIENT_1_1_COMPATIBLE
if(_tcpDeprecated) {
_transportTraits.reset(nullptr);
_tcpDeprecated.reset(nullptr);
}
#endif
}
}
}
N.B. the default for _reuse
is true and _canReuse = _reuse;
is called when looking at the header response.
I had this problem when a first HTTPS request was made using HTTPClient
, and later (in another library) HTTPS requests were made using WifiClientSecure
.
The error I got with that was similar to https://github.com/platformio/platform-espressif32/issues/649:
_handle_error(): [send_ssl_data():<line number>]: (-80) UNKNOWN ERROR CODE (0050)
OK, found another issue with the HTTPClient on ESP32. While searching for a related issue, this one came up and it is probably related. If not, I will make a new issue.
Problem is located here:
When calling
end()
thedisconnect()
call is probably doing nothing since it is likely no longer connected. This results in new calls made using the HTTPClient to not work untill some timeout has occurred or some instance of WiFiClient has made a connection to some other host.By simply calling
stop()
on the WiFiClient object, this is no longer an issue.Not sure if the best fix should be in this
HTTPClient::disconnect()
function, but I guess it is at least a fix for this problem. However the true problem is probably in the WiFiClient code.IMHO this code should be executed regardless the
connected()
state.But then there should be an extra check on whether
_client
isn't a nullptr.So it should probably something like this (untested)
void HTTPClient::disconnect(bool preserveClient) { if(connected()) { if(_client->available() > 0) { log_d("still data in buffer (%d), clean up.\n", _client->available()); _client->flush(); } } else { log_d("tcp is closed\n"); } if(_reuse && _canReuse) { log_d("tcp keep open for reuse"); } else { log_d("tcp stop"); if (_client != nullptr) _client->stop(); if(!preserveClient || _client == nullptr) { _client = nullptr; #ifdef HTTPCLIENT_1_1_COMPATIBLE if(_tcpDeprecated) { _transportTraits.reset(nullptr); _tcpDeprecated.reset(nullptr); } #endif } } }
N.B. the default for
_reuse
is true and_canReuse = _reuse;
is called when looking at the header response.
This is corresponding with what I was saying. Good job @TD-er
I had this problem when a first HTTPS request was made using
HTTPClient
, and later (in another library) HTTPS requests were made usingWifiClientSecure
.The error I got with that was similar to platformio/platform-espressif32#649:
_handle_error(): [send_ssl_data():<line number>]: (-80) UNKNOWN ERROR CODE (0050)
I found that client->println()
throws an error (-80) UNKNOWN ERROR CODE (0050)
at some point
I replaced it with client->print(boundary + "\r\n");
and everything started working
@RosiersRobin Can this issue be closed?
@VojtechBartoska I still have this issue. There seems to be no fix yet for this?
Very annoying issue, and I finally found an explanation here. Does anyone have a workaround while waiting for a fix?
This suggested workaround does not work for me: https://github.com/espressif/arduino-esp32/issues/3387#issuecomment-756815055
Board
ESP32-Wroom-32UE
Device Description
Plain module un custom hardware with an ETH chip (LAN8720) connected to it, using that chip to cummunicate with internet intead of wifi.
Hardware Configuration
GPIO0 has 50Mhz crystal input for ETH -> disabled on boot, thus device boots
GPIO12 -> crystal power pin GPIO2 -> I2C clock signal GPIO17 -> I2C MDIO
Version
v2.0.2
IDE Name
Arduino IDE
Operating System
Windows 10
Flash frequency
80Mhz
PSRAM enabled
no
Upload speed
921600
Description
I updated from version 1.0.6 to 2.0.2 and changed nothing to the code. The code works still, however, it seems that the
HTTPClient
is broken since then.I can do an API call to the server once, but not twice.
I have for example a heartbeat that runs every 5 minutes, the first time it runs just fine, the second time I get a -1 error. While this uses the exact same code...
Sketch
Debug Message
All outputs were captured via the serial debug monitor setting the core debug level to verbose.