ust-consumerd: fix exit race log corruption
authorJason Wessel <jason.wessel@windriver.com>
Wed, 27 Apr 2011 20:22:15 +0000 (22:22 +0200)
committerNils Carlson <nils.carlson@ericsson.com>
Thu, 28 Apr 2011 13:17:19 +0000 (15:17 +0200)
In the following scenario on an SMP system the ust-consumerd can end
up not properly closing out file handles which leads to log
corruption:
  * usttrace -m -l small_quick_app_lots_of_malloc_and_free
  * The app completes and usttrace sees and sends the SIGTERM to ust-consumerd
  * The ust-consumerd main thread will exit and the _exit() handlers
    kills off the remaining pthreads without everything getting closed out

The solution to the problem is to introduce an active_thread count for
the private ustconsumer_instance.  This counter will be zeroed out
when it is safe to completely shutdown the main thread, which will
subsequently run the _exit() handlers.

Signed-off-by: Jason Wessel <jason.wessel@windriver.com>
Signed-off-by: Nils Carlson <nils.carlson@ericsson.com>
include/ust/ustconsumer.h
libustconsumer/libustconsumer.c

index f99f8f13f2d72f39725588842877ddccab79f67b..cde84408d936b55ecdfb0e0de6f8efc0e9992480 100644 (file)
@@ -89,6 +89,7 @@ struct ustconsumer_instance {
        char *sock_path;
        pthread_mutex_t mutex;
        int active_buffers;
+       int active_threads;
 };
 
 /**
index abf21d801ca1473fd8b1b3c1453636361fc0199a..b47f04ab08779ce03db1f5f845110ef9f13ca172 100644 (file)
@@ -543,6 +543,10 @@ void *consumer_thread(void *arg)
        int result;
        sigset_t sigset;
 
+       pthread_mutex_lock(&args->instance->mutex);
+       args->instance->active_threads++;
+       pthread_mutex_unlock(&args->instance->mutex);
+
        if(args->instance->callbacks->on_new_thread)
                args->instance->callbacks->on_new_thread(args->instance->callbacks);
 
@@ -584,6 +588,10 @@ void *consumer_thread(void *arg)
        if(args->instance->callbacks->on_close_thread)
                args->instance->callbacks->on_close_thread(args->instance->callbacks);
 
+       pthread_mutex_lock(&args->instance->mutex);
+       args->instance->active_threads--;
+       pthread_mutex_unlock(&args->instance->mutex);
+
        free((void *)args->channel);
        free(args);
        return NULL;
@@ -735,7 +743,7 @@ int ustconsumer_start_instance(struct ustconsumer_instance *instance)
 
                if (instance->quit_program) {
                        pthread_mutex_lock(&instance->mutex);
-                       if(instance->active_buffers == 0) {
+                       if (instance->active_buffers == 0 && instance->active_threads == 0) {
                                pthread_mutex_unlock(&instance->mutex);
                                break;
                        }
This page took 0.025125 seconds and 4 git commands to generate.