@@ -8541,13 +8541,29 @@ inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex,
8541
8541
return ssl;
8542
8542
}
8543
8543
8544
- inline void ssl_delete (std::mutex &ctx_mutex, SSL *ssl,
8544
+ inline void ssl_delete (std::mutex &ctx_mutex, SSL *ssl, socket_t sock,
8545
8545
bool shutdown_gracefully) {
8546
8546
// sometimes we may want to skip this to try to avoid SIGPIPE if we know
8547
8547
// the remote has closed the network connection
8548
8548
// Note that it is not always possible to avoid SIGPIPE, this is merely a
8549
8549
// best-efforts.
8550
- if (shutdown_gracefully) { SSL_shutdown (ssl); }
8550
+ if (shutdown_gracefully) {
8551
+ #ifdef _WIN32
8552
+ SSL_shutdown (ssl);
8553
+ #else
8554
+ timeval tv;
8555
+ tv.tv_sec = 1 ;
8556
+ tv.tv_usec = 0 ;
8557
+ setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO,
8558
+ reinterpret_cast <const void *>(&tv), sizeof (tv));
8559
+
8560
+ auto ret = SSL_shutdown (ssl);
8561
+ while (ret == 0 ) {
8562
+ std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
8563
+ ret = SSL_shutdown (ssl);
8564
+ }
8565
+ #endif
8566
+ }
8551
8567
8552
8568
std::lock_guard<std::mutex> guard (ctx_mutex);
8553
8569
SSL_free (ssl);
@@ -8826,7 +8842,7 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
8826
8842
// Shutdown gracefully if the result seemed successful, non-gracefully if
8827
8843
// the connection appeared to be closed.
8828
8844
const bool shutdown_gracefully = ret;
8829
- detail::ssl_delete (ctx_mutex_, ssl, shutdown_gracefully);
8845
+ detail::ssl_delete (ctx_mutex_, ssl, sock, shutdown_gracefully);
8830
8846
}
8831
8847
8832
8848
detail::shutdown_socket (sock);
@@ -9109,7 +9125,8 @@ inline void SSLClient::shutdown_ssl_impl(Socket &socket,
9109
9125
return ;
9110
9126
}
9111
9127
if (socket.ssl ) {
9112
- detail::ssl_delete (ctx_mutex_, socket.ssl , shutdown_gracefully);
9128
+ detail::ssl_delete (ctx_mutex_, socket.ssl , socket.sock ,
9129
+ shutdown_gracefully);
9113
9130
socket.ssl = nullptr ;
9114
9131
}
9115
9132
assert (socket.ssl == nullptr );
0 commit comments