Commits

Roger Light committed db52ced

Don't terminate threads on mosquitto_destroy() when a client is not using
the threaded interface but does use their own thread. Fixes bug #1291473.

Thanks to Greg Beresford.

Comments (0)

Files changed (3)

 - Break out of select() on publish(), subscribe() etc. when using the threaded
   interface. Fixes bug #1270062.
 - Handle incoming and outgoing messages separately. Fixes bug #1263172.
+- Don't terminate threads on mosquitto_destroy() when a client is not using
+  the threaded interface but does use their own thread. Fixes bug #1291473.
 
 Clients:
 - Add --ciphers to allow specifying which TLS ciphers to support.
 	if(!mosq) return;
 
 #ifdef WITH_THREADING
-	if(!pthread_equal(mosq->thread_id, pthread_self())){
+	if(mosq->threaded && !pthread_equal(mosq->thread_id, pthread_self())){
 		pthread_cancel(mosq->thread_id);
 		pthread_join(mosq->thread_id, NULL);
+		mosq->threaded = false;
 	}
 
 	if(mosq->id){
 int mosquitto_loop_start(struct mosquitto *mosq)
 {
 #ifdef WITH_THREADING
-	if(!mosq) return MOSQ_ERR_INVAL;
+	if(!mosq || mosq->threaded) return MOSQ_ERR_INVAL;
 
+	mosq->threaded = true;
 	pthread_create(&mosq->thread_id, NULL, _mosquitto_thread_main, mosq);
 	return MOSQ_ERR_SUCCESS;
 #else
 int mosquitto_loop_stop(struct mosquitto *mosq, bool force)
 {
 #ifdef WITH_THREADING
-	if(!mosq) return MOSQ_ERR_INVAL;
+	if(!mosq || !mosq->threaded) return MOSQ_ERR_INVAL;
 	
 	if(force){
 		pthread_cancel(mosq->thread_id);
 	}
 	pthread_join(mosq->thread_id, NULL);
 	mosq->thread_id = pthread_self();
+	mosq->threaded = false;
 
 	return MOSQ_ERR_SUCCESS;
 #else
 
 	if(!mosq) return NULL;
 
-	mosq->threaded = true;
 	pthread_mutex_lock(&mosq->state_mutex);
 	if(mosq->state == mosq_cs_connect_async){
 		pthread_mutex_unlock(&mosq->state_mutex);
 		mosquitto_loop_forever(mosq, mosq->keepalive*1000, 1);
 	}
 
-	mosq->threaded = false;
 	return obj;
 }
 #endif