From: Guus Sliepen Date: Tue, 26 Jun 2018 12:23:04 +0000 (+0200) Subject: Fix a potential segfault when closing a stopped meshlink handle. X-Git-Url: http://git.meshlink.io/?p=meshlink;a=commitdiff_plain;h=f0d03b570cf72570573b76d74c2700ab60abda93 Fix a potential segfault when closing a stopped meshlink handle. If meshlink_stop() was called before meshlink_close(), the latter would cause pthread_join() being called on a thread that was already joined. --- diff --git a/src/meshlink.c b/src/meshlink.c index 24d4c249..1de54e91 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -1127,6 +1127,7 @@ bool meshlink_start(meshlink_handle_t *mesh) { logger(mesh, MESHLINK_DEBUG, "Could not start thread: %s\n", strerror(errno)); memset(&mesh->thread, 0, sizeof(mesh)->thread); meshlink_errno = MESHLINK_EINTERNAL; + event_loop_stop(&mesh->loop); pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -1173,12 +1174,14 @@ void meshlink_stop(meshlink_handle_t *mesh) { } } - // Wait for the main thread to finish - pthread_mutex_unlock(&(mesh->mesh_mutex)); - pthread_join(mesh->thread, NULL); - pthread_mutex_lock(&(mesh->mesh_mutex)); + if(mesh->threadstarted) { + // Wait for the main thread to finish + pthread_mutex_unlock(&(mesh->mesh_mutex)); + pthread_join(mesh->thread, NULL); + pthread_mutex_lock(&(mesh->mesh_mutex)); - mesh->threadstarted = false; + mesh->threadstarted = false; + } // Close all metaconnections if(mesh->connections) { @@ -1192,10 +1195,9 @@ void meshlink_stop(meshlink_handle_t *mesh) { if(mesh->outgoings) { list_delete_list(mesh->outgoings); + mesh->outgoings = NULL; } - mesh->outgoings = NULL; - pthread_mutex_unlock(&(mesh->mesh_mutex)); }