From 269756a171a63a2ff96f1973df11b6563020201a Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 27 Apr 2023 23:25:52 +0100 Subject: [PATCH] Fix high CPU use on slow TLS connect. Closes #2794. Thanks to Evgeny S. --- ChangeLog.txt | 1 + lib/loop.c | 22 ++++++++++++---------- lib/mosquitto.c | 13 +------------ 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 351eaad16c..655ddd0716 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 965294f052..0277a1d3a8 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 83bf42c2d7..a83d43a7f5 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; }