-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Problem with OpenSSL memory leaks #2025
Comments
@BlehPoster thanks for the feedback, but I am not really sure that this is memory leak... |
@BlehPoster could you try with v0.18.3 and v0.18.4 to see if there is any difference? Thanks a lot! |
@yhirose I tested different versions, but they had no effect on the outcome. The example I provided already works around many of the existing memory leaks. However, the following code demonstrates a scenario that creates more than 40 leaking data blocks: #define CPPHTTPLIB_OPENSSL_SUPPORT
#include <cpp-httplib/httplib.h>
#include <vld.h>
#include <thread>
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
auto url = "echo.free.beeceptor.com";
std::thread([&]() {
auto client = httplib::SSLClient(std::string("https://") + url);
client.Get("/");
}).join();
return 0;
} |
@BlehPoster I found a bug in your code. The string in You can see all the correct usages with |
I apologize for the mistake. During testing and debugging, I repeatedly modified the code. I have now retested the following code, and the issue still occurs. #define CPPHTTPLIB_OPENSSL_SUPPORT
#include <cpp-httplib/httplib.h>
#include <vld.h>
#include <thread>
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
/* From simple example*/
/* Does not leak but get a connection error (assert failed) */
std::thread([&]() {
httplib::Client cli("https://yhirose.github.io/hi");
auto res = cli.Get("/hi");
assert(res); // failure
}).join();
/* From client example, but using yhirose.github.io */
/* Does leak, but receive a valid reply */
std::thread([&]() {
httplib::Client cli("https://yhirose.github.io");
auto res = cli.Get("/hi");
assert(res); // ok
}).join();
return 0;
} I debugged the client creation to investigate why For testing, I updated the regex to: const static std::regex re(
R"((?:([a-z]+):\/\/)?(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?(\/[^\s]*)?)"); With this updated regex, |
@BlehPoster sorry that my recent change in README isn't correct and I just updated it. httplib::Client cli("https://yhirose.github.io/hi"); should be httplib::Client cli("https://yhirose.github.io"); |
@yhirose I finally figured it out. I recreated the SSL code in a separate, smaller project and reduced it to its simplest form while still reproducing the memory leak. Unfortunately, I should have approached the problem from the other direction, as creating an SSL context itself leads to significant memory leaks when done in a separate thread. std::thread([]() {
auto ctx_ = SSL_CTX_new(TLS_client_method());
SSL_CTX_free(ctx_);
}).join(); However, using A possible solution could be adding a flag that ensures For now, my personal workaround is to explicitly call |
@BlehPoster thanks for the research. Is it the same as openssl/openssl#22831? |
@yhirose yes, the two problems are quite similar. the fact that openssl expects the user to manually free the memory for each thread is a bit worrying. it also creates significant problems for me as i don't always have direct access to all threads in my production environment (boost asio). But yes, this is not a bug on your part, but a feature of openssl. |
I'll leave it with 'information' tag. |
Description
I am experiencing memory leaks when using
yhirose/cpp-httplib
with OpenSSL, particularly when integrating with Boost IO services. While I have implemented a workaround for some of the issues, there are still persistent memory leaks that I would like to bring to your attention.Environment
Steps to Reproduce
httplib
with Boost IO services or just regular threads.httplib
functionalities in multi-threaded configurations.Expected Behavior
No memory leaks should occur when using
httplib
with OpenSSL and Boost IO services.Sample Code
Here is a reproducible example:
Remaining memory leaks
The text was updated successfully, but these errors were encountered: