Fix #1481 (with content provider) (#1527)

* Fix #1481 (with content provider)

* Improve shutdown performance

* Make shutdown action more stable

* Move some tests up

* Simplified

* Simplified
This commit is contained in:
yhirose 2024-08-06 13:43:00 -04:00 committed by GitHub
parent ed0719f2bc
commit 521529d24d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 235 additions and 164 deletions

View file

@ -8541,13 +8541,29 @@ inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex,
return ssl;
}
inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl,
inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock,
bool shutdown_gracefully) {
// sometimes we may want to skip this to try to avoid SIGPIPE if we know
// the remote has closed the network connection
// Note that it is not always possible to avoid SIGPIPE, this is merely a
// best-efforts.
if (shutdown_gracefully) { SSL_shutdown(ssl); }
if (shutdown_gracefully) {
#ifdef _WIN32
SSL_shutdown(ssl);
#else
timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
reinterpret_cast<const void *>(&tv), sizeof(tv));
auto ret = SSL_shutdown(ssl);
while (ret == 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
ret = SSL_shutdown(ssl);
}
#endif
}
std::lock_guard<std::mutex> guard(ctx_mutex);
SSL_free(ssl);
@ -8826,7 +8842,7 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
// Shutdown gracefully if the result seemed successful, non-gracefully if
// the connection appeared to be closed.
const bool shutdown_gracefully = ret;
detail::ssl_delete(ctx_mutex_, ssl, shutdown_gracefully);
detail::ssl_delete(ctx_mutex_, ssl, sock, shutdown_gracefully);
}
detail::shutdown_socket(sock);
@ -9109,7 +9125,8 @@ inline void SSLClient::shutdown_ssl_impl(Socket &socket,
return;
}
if (socket.ssl) {
detail::ssl_delete(ctx_mutex_, socket.ssl, shutdown_gracefully);
detail::ssl_delete(ctx_mutex_, socket.ssl, socket.sock,
shutdown_gracefully);
socket.ssl = nullptr;
}
assert(socket.ssl == nullptr);