diff --git a/ChangeLog.txt b/ChangeLog.txt index 351eaad1..655ddd07 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -17,6 +17,7 @@ Client library: problem of the client OS sleeping and the client hence not being able to calculate the actual time for keepalive purposes. Closes #2760. - Fix default settings incorrectly allowing TLS v1.1. Closes #2722. +- Fix high CPU use on slow TLS connect. Closes #2794. Clients: - Fix incorrect topic-alias property value in mosquitto_sub json output. diff --git a/lib/loop.c b/lib/loop.c index 965294f0..0277a1d3 100644 --- a/lib/loop.c +++ b/lib/loop.c @@ -63,20 +63,22 @@ int mosquitto_loop(struct mosquitto *mosq, int timeout, int max_packets) if(mosq->sock != INVALID_SOCKET){ maxfd = mosq->sock; FD_SET(mosq->sock, &readfds); - pthread_mutex_lock(&mosq->current_out_packet_mutex); - pthread_mutex_lock(&mosq->out_packet_mutex); - if(mosq->out_packet || mosq->current_out_packet){ + if(mosq->want_write){ FD_SET(mosq->sock, &writefds); - } + }else{ #ifdef WITH_TLS - if(mosq->ssl){ - if(mosq->want_write){ - FD_SET(mosq->sock, &writefds); + if(mosq->ssl == NULL || SSL_is_init_finished(mosq->ssl)) +#endif + { + pthread_mutex_lock(&mosq->current_out_packet_mutex); + pthread_mutex_lock(&mosq->out_packet_mutex); + if(mosq->out_packet || mosq->current_out_packet){ + FD_SET(mosq->sock, &writefds); + } + pthread_mutex_unlock(&mosq->out_packet_mutex); + pthread_mutex_unlock(&mosq->current_out_packet_mutex); } } -#endif - pthread_mutex_unlock(&mosq->out_packet_mutex); - pthread_mutex_unlock(&mosq->current_out_packet_mutex); }else{ #ifdef WITH_SRV if(mosq->achan){ diff --git a/lib/mosquitto.c b/lib/mosquitto.c index 83bf42c2..a83d43a7 100644 --- a/lib/mosquitto.c +++ b/lib/mosquitto.c @@ -332,18 +332,7 @@ int mosquitto_socket(struct mosquitto *mosq) bool mosquitto_want_write(struct mosquitto *mosq) { - bool result = false; - if(mosq->out_packet || mosq->current_out_packet){ - result = true; - } -#ifdef WITH_TLS - if(mosq->ssl){ - if (mosq->want_write) { - result = true; - } - } -#endif - return result; + return mosq->out_packet || mosq->current_out_packet || mosq->want_write; }