@@ -316,6 +316,7 @@ class SSLServer : public Server {
316316 virtual bool read_and_close_socket (socket_t sock);
317317
318318 SSL_CTX* ctx_;
319+ std::mutex ctx_mutex_;
319320};
320321
321322class SSLClient : public Client {
@@ -334,6 +335,7 @@ class SSLClient : public Client {
334335 virtual bool read_and_close_socket (socket_t sock, Request& req, Response& res);
335336
336337 SSL_CTX* ctx_;
338+ std::mutex ctx_mutex_;
337339};
338340#endif
339341
@@ -2029,18 +2031,17 @@ inline std::shared_ptr<Response> Client::post(const char* path, const Headers& h
20292031#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
20302032namespace detail {
20312033
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-
20352034template <typename U, typename V, typename T>
20362035inline bool read_and_close_socket_ssl (
20372036 socket_t sock, bool keep_alive,
2038- SSL_CTX* ctx, U SSL_connect_or_accept, V setup,
2037+ // TODO: OpenSSL 1.0.2 occasionally crashes... The upcoming 1.1.0 is going to be thread safe.
2038+ SSL_CTX* ctx, std::mutex& ctx_mutex,
2039+ U SSL_connect_or_accept, V setup,
20392040 T callback)
20402041{
20412042 SSL* ssl = nullptr ;
20422043 {
2043- std::lock_guard<std::mutex> guard (ssl_ctx_mutex_ );
2044+ std::lock_guard<std::mutex> guard (ctx_mutex );
20442045
20452046 ssl = SSL_new (ctx);
20462047 if (!ssl) {
@@ -2079,7 +2080,7 @@ inline bool read_and_close_socket_ssl(
20792080 SSL_shutdown (ssl);
20802081
20812082 {
2082- std::lock_guard<std::mutex> guard (ssl_ctx_mutex_ );
2083+ std::lock_guard<std::mutex> guard (ctx_mutex );
20832084 SSL_free (ssl);
20842085 }
20852086
@@ -2172,7 +2173,7 @@ inline bool SSLServer::read_and_close_socket(socket_t sock)
21722173 return detail::read_and_close_socket_ssl (
21732174 sock,
21742175 keep_alive,
2175- ctx_,
2176+ ctx_, ctx_mutex_,
21762177 SSL_accept,
21772178 [](SSL* /* ssl*/ ) {},
21782179 [this ](Stream& strm, bool last_connection) {
@@ -2204,7 +2205,8 @@ inline bool SSLClient::read_and_close_socket(socket_t sock, Request& req, Respon
22042205{
22052206 return is_valid () && detail::read_and_close_socket_ssl (
22062207 sock, false ,
2207- ctx_, SSL_connect,
2208+ ctx_, ctx_mutex_,
2209+ SSL_connect,
22082210 [&](SSL* ssl) {
22092211 SSL_set_tlsext_host_name (ssl, host_.c_str ());
22102212 },
0 commit comments