Skip to content

Commit

Permalink
Fix high CPU use on slow TLS connect.
Browse files Browse the repository at this point in the history
Closes #2794. Thanks to Evgeny S.
  • Loading branch information
ralight committed Apr 27, 2023
1 parent 3c51816 commit 269756a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
1 change: 1 addition & 0 deletions ChangeLog.txt
Expand Up @@ -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.
Expand Down
22 changes: 12 additions & 10 deletions lib/loop.c
Expand Up @@ -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){
Expand Down
13 changes: 1 addition & 12 deletions lib/mosquitto.c
Expand Up @@ -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;
}


Expand Down

1 comment on commit 269756a

@jsopenrb
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't mosquitto_want_write have the same checks as mosquitto_loop? Seems that it will still spin if an external select is used.

Please sign in to comment.