Skip to content

Commit 445f27b

Browse files
committed
Unconditionally init/destroy client mutexes
This removes the remote chance that we may init but then not destroy, or destroy after not initing
1 parent e3f1438 commit 445f27b

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

lib/libmosquitto.c

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_st
149149
mosquitto__destroy(mosq);
150150
memset(mosq, 0, sizeof(struct mosquitto));
151151

152+
#ifdef WITH_THREADING
153+
COMPAT_pthread_mutex_init(&mosq->callback_mutex, NULL);
154+
COMPAT_pthread_mutex_init(&mosq->log_callback_mutex, NULL);
155+
COMPAT_pthread_mutex_init(&mosq->state_mutex, NULL);
156+
COMPAT_pthread_mutex_init(&mosq->out_packet_mutex, NULL);
157+
COMPAT_pthread_mutex_init(&mosq->msgtime_mutex, NULL);
158+
COMPAT_pthread_mutex_init(&mosq->msgs_in.mutex, NULL);
159+
COMPAT_pthread_mutex_init(&mosq->msgs_out.mutex, NULL);
160+
COMPAT_pthread_mutex_init(&mosq->mid_mutex, NULL);
161+
mosq->thread_id = pthread_self();
162+
#endif
163+
152164
if(userdata){
153165
mosq->userdata = userdata;
154166
}else{
@@ -221,17 +233,6 @@ int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_st
221233
mosq->tls_insecure = false;
222234
mosq->want_write = false;
223235
mosq->tls_ocsp_required = false;
224-
#endif
225-
#ifdef WITH_THREADING
226-
COMPAT_pthread_mutex_init(&mosq->callback_mutex, NULL);
227-
COMPAT_pthread_mutex_init(&mosq->log_callback_mutex, NULL);
228-
COMPAT_pthread_mutex_init(&mosq->state_mutex, NULL);
229-
COMPAT_pthread_mutex_init(&mosq->out_packet_mutex, NULL);
230-
COMPAT_pthread_mutex_init(&mosq->msgtime_mutex, NULL);
231-
COMPAT_pthread_mutex_init(&mosq->msgs_in.mutex, NULL);
232-
COMPAT_pthread_mutex_init(&mosq->msgs_out.mutex, NULL);
233-
COMPAT_pthread_mutex_init(&mosq->mid_mutex, NULL);
234-
mosq->thread_id = pthread_self();
235236
#endif
236237
if(mosq->disable_socketpair == false){
237238
/* This must be after pthread_mutex_init(), otherwise the log mutex may be
@@ -261,19 +262,14 @@ void mosquitto__destroy(struct mosquitto *mosq)
261262
}
262263
# endif
263264

264-
if(mosq->id){
265-
/* If mosq->id is not NULL then the client has already been initialised
266-
* and so the mutexes need destroying. If mosq->id is NULL, the mutexes
267-
* haven't been initialised. */
268-
COMPAT_pthread_mutex_destroy(&mosq->callback_mutex);
269-
COMPAT_pthread_mutex_destroy(&mosq->log_callback_mutex);
270-
COMPAT_pthread_mutex_destroy(&mosq->state_mutex);
271-
COMPAT_pthread_mutex_destroy(&mosq->out_packet_mutex);
272-
COMPAT_pthread_mutex_destroy(&mosq->msgtime_mutex);
273-
COMPAT_pthread_mutex_destroy(&mosq->msgs_in.mutex);
274-
COMPAT_pthread_mutex_destroy(&mosq->msgs_out.mutex);
275-
COMPAT_pthread_mutex_destroy(&mosq->mid_mutex);
276-
}
265+
COMPAT_pthread_mutex_destroy(&mosq->callback_mutex);
266+
COMPAT_pthread_mutex_destroy(&mosq->log_callback_mutex);
267+
COMPAT_pthread_mutex_destroy(&mosq->state_mutex);
268+
COMPAT_pthread_mutex_destroy(&mosq->out_packet_mutex);
269+
COMPAT_pthread_mutex_destroy(&mosq->msgtime_mutex);
270+
COMPAT_pthread_mutex_destroy(&mosq->msgs_in.mutex);
271+
COMPAT_pthread_mutex_destroy(&mosq->msgs_out.mutex);
272+
COMPAT_pthread_mutex_destroy(&mosq->mid_mutex);
277273
#endif
278274
if(net__is_connected(mosq)){
279275
net__socket_close(mosq);

0 commit comments

Comments
 (0)