From afa120900edda1e48a5e91bd6697a6e48395526c Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Fri, 8 Aug 2014 16:09:11 +0200 Subject: [PATCH] Only start discovery once per process. Starting multiple Avahi threads in one process will fail with one of the threads triggering an assert(), thus killing the process. It's not critical that Avahi runs, so make sure we only start one. --- src/meshlink.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/meshlink.c b/src/meshlink.c index d5ea5418..c7a6c0b2 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -47,6 +47,9 @@ typedef struct { #define MSG_NOSIGNAL 0 #endif +static pthread_mutex_t global_mutex; +static bool discovery_started; + __thread meshlink_errno_t meshlink_errno; //TODO: this can go away completely @@ -869,8 +872,16 @@ bool meshlink_start(meshlink_handle_t *mesh) { mesh->threadstarted=true; // Start discovery - if(!discovery_start(mesh)) - return false; + // Since only one Avahi instance can run in one program at the moment, make sure we only start one, ignore it otherwise. + + bool discovery_on = false; + pthread_mutex_lock(&global_mutex); + if(!discovery_started) + discovery_on = discovery_started = true; + pthread_mutex_unlock(&global_mutex); + + if(discovery_on) + discovery_start(mesh); return true; } @@ -883,7 +894,14 @@ void meshlink_stop(meshlink_handle_t *mesh) { } // Stop discovery + + bool discovery_on = mesh->discovery_threadstarted; discovery_stop(mesh); + if(discovery_on) { + pthread_mutex_lock(&global_mutex); + discovery_started = false; + pthread_mutex_unlock(&global_mutex); + } // Shut down a listening socket to signal the main thread to shut down @@ -1702,7 +1720,6 @@ extern void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node append_config_file(mesh, node->name, "Address", full_addr_str); fail: -done: free(addr_str); free(port_str); free(full_addr_str); -- 2.39.5