Skip to content

Commit 5574d82

Browse files
committed
Made a temporary fix for OpenSSL thread problem
1 parent 4320d7b commit 5574d82

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

httplib.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,15 +2029,23 @@ inline std::shared_ptr<Response> Client::post(const char* path, const Headers& h
20292029
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
20302030
namespace detail {
20312031

2032+
// TODO: OpenSSL 1.0.2 occasionally crashes... The upcoming 1.1.0 is going to be thread safe.
2033+
static std::mutex ssl_ctx_mutex_;
2034+
20322035
template <typename U, typename V, typename T>
20332036
inline bool read_and_close_socket_ssl(
20342037
socket_t sock, bool keep_alive,
20352038
SSL_CTX* ctx, U SSL_connect_or_accept, V setup,
20362039
T callback)
20372040
{
2038-
auto ssl = SSL_new(ctx);
2039-
if (!ssl) {
2040-
return false;
2041+
SSL* ssl = nullptr;
2042+
{
2043+
std::lock_guard<std::mutex> guard(ssl_ctx_mutex_);
2044+
2045+
ssl = SSL_new(ctx);
2046+
if (!ssl) {
2047+
return false;
2048+
}
20412049
}
20422050

20432051
auto bio = BIO_new_socket(sock, BIO_NOCLOSE);
@@ -2069,8 +2077,14 @@ inline bool read_and_close_socket_ssl(
20692077
}
20702078

20712079
SSL_shutdown(ssl);
2072-
SSL_free(ssl);
2080+
2081+
{
2082+
std::lock_guard<std::mutex> guard(ssl_ctx_mutex_);
2083+
SSL_free(ssl);
2084+
}
2085+
20732086
close_socket(sock);
2087+
20742088
return ret;
20752089
}
20762090

test/test.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ class ServerTest : public ::testing::Test {
263263
res.set_content("Hello World!", "text/plain");
264264
})
265265
.get("/slow", [&](const Request& /*req*/, Response& res) {
266-
msleep(3000);
266+
msleep(2000);
267267
res.set_content("slow", "text/plain");
268268
})
269269
.get("/remote_addr", [&](const Request& req, Response& res) {
@@ -368,6 +368,9 @@ class ServerTest : public ::testing::Test {
368368

369369
virtual void TearDown() {
370370
svr_.stop();
371+
for (auto& t: request_threads_) {
372+
t.join();
373+
}
371374
t_.join();
372375
}
373376

@@ -380,6 +383,7 @@ class ServerTest : public ::testing::Test {
380383
Server svr_;
381384
#endif
382385
thread t_;
386+
std::vector<thread> request_threads_;
383387
};
384388

385389
TEST_F(ServerTest, GetMethod200)
@@ -736,10 +740,10 @@ TEST_F(ServerTest, GetMethodRemoteAddr)
736740

737741
TEST_F(ServerTest, SlowRequest)
738742
{
739-
std::thread([=]() { auto res = cli_.get("/slow"); }).detach();
740-
std::thread([=]() { auto res = cli_.get("/slow"); }).detach();
741-
std::thread([=]() { auto res = cli_.get("/slow"); }).detach();
742-
msleep(1000);
743+
request_threads_.push_back(std::thread([=]() { auto res = cli_.get("/slow"); }));
744+
request_threads_.push_back(std::thread([=]() { auto res = cli_.get("/slow"); }));
745+
request_threads_.push_back(std::thread([=]() { auto res = cli_.get("/slow"); }));
746+
msleep(100);
743747
}
744748

745749
#ifdef CPPHTTPLIB_ZLIB_SUPPORT

0 commit comments

Comments
 (0)