Limit SSL_ERROR_WANT_READ retries to 1 sec (#957)

retry with 1ms delays to prevent CPU hoggin
This commit is contained in:
Baruch Nissenbaum 2021-06-14 15:41:20 +03:00 committed by GitHub
parent fc9b223acc
commit b8dec12f15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6687,15 +6687,17 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) {
auto ret = SSL_read(ssl_, ptr, static_cast<int>(size));
if (ret < 0) {
auto err = SSL_get_error(ssl_, ret);
int n = 1000;
#ifdef _WIN32
while (err == SSL_ERROR_WANT_READ ||
err == SSL_ERROR_SYSCALL && WSAGetLastError() == WSAETIMEDOUT) {
while (--n >= 0 && (err == SSL_ERROR_WANT_READ ||
err == SSL_ERROR_SYSCALL && WSAGetLastError() == WSAETIMEDOUT)) {
#else
while (err == SSL_ERROR_WANT_READ) {
while (--n >= 0 && err == SSL_ERROR_WANT_READ) {
#endif
if (SSL_pending(ssl_) > 0) {
return SSL_read(ssl_, ptr, static_cast<int>(size));
} else if (is_readable()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
ret = SSL_read(ssl_, ptr, static_cast<int>(size));
if (ret >= 0) { return ret; }
err = SSL_get_error(ssl_, ret);