From 40779875937730f77c1a08de163349b5b4c8ccb4 Mon Sep 17 00:00:00 2001 From: Christian Salvasohn Date: Wed, 13 Apr 2022 20:05:35 +0200 Subject: [PATCH] fix data race mosquitto_loop function next_msg_out must be protected with the msgtime_mutex as done everywhere else in the code else there is a data race e.g. if mosquitto_publish is called from another thread Signed-off-by: Christian Salvasohn --- lib/loop.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/loop.c b/lib/loop.c index 3373c186..2c35ee19 100644 --- a/lib/loop.c +++ b/lib/loop.c @@ -114,9 +114,11 @@ int mosquitto_loop(struct mosquitto *mosq, int timeout, int max_packets) } now = mosquitto_time(); + pthread_mutex_lock(&mosq->msgtime_mutex); if(mosq->next_msg_out && now + timeout_ms/1000 > mosq->next_msg_out){ timeout_ms = (mosq->next_msg_out - now)*1000; } + pthread_mutex_unlock(&mosq->msgtime_mutex); if(timeout_ms < 0){ /* There has been a delay somewhere which means we should have already