Fix caching topic shutdown assertions
authorDavid M. Lee <dlee@digium.com>
Thu, 18 Jul 2013 14:08:21 +0000 (14:08 +0000)
committerDavid M. Lee <dlee@digium.com>
Thu, 18 Jul 2013 14:08:21 +0000 (14:08 +0000)
The recent changes to update stasis_cache_topics directly from the
publisher thread uncovered a race condition, which was causing asserts
in the /stasis/core tests.

If the caching topic's subscription is the last reference to the
caching topic, it will destroy the caching topic after the final
message has been processed. When dispatching to a different thread,
this usually gave the unsubscribe enough time to finish before
destruction happened. Now, however, it consistently destroys before
unsubscription is complete.

This patch adds an extra reference to the caching topic, to hold it
for the duration of the unsubscription.

This patch also removes an extra unref that was happening when the
final message was received by the caching topic. It was put there
because of an extra ref that was put into the caching topic's
constructor. Both have been removed, which makes the destructor a bit
less confusing.

Review: https://reviewboard.asterisk.org/r/2675/

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@394686 65c4cc65-6c06-0410-ace0-fbb531ad65f3

main/stasis_cache.c

index 5757c86..17be901 100644 (file)
@@ -76,9 +76,19 @@ struct stasis_topic *stasis_caching_get_topic(struct stasis_caching_topic *cachi
 struct stasis_caching_topic *stasis_caching_unsubscribe(struct stasis_caching_topic *caching_topic)
 {
        if (caching_topic) {
+               RAII_VAR(struct stasis_caching_topic *, hold_ref, NULL,
+                       ao2_cleanup);
+
+               /* The subscription may hold the last reference to this caching
+                * topic, but we want to make sure the unsubscribe finishes
+                * before kicking of the caching topic's dtor.
+                */
+               ao2_ref(caching_topic, +1);
+               hold_ref = caching_topic;
+
                if (stasis_subscription_is_subscribed(caching_topic->sub)) {
                        /* Increment the reference to hold on to it past the
-                        * unsubscribe */
+                        * unsubscribe. Will be cleaned up in dtor. */
                        ao2_ref(caching_topic->sub, +1);
                        stasis_unsubscribe(caching_topic->sub);
                } else {
@@ -389,6 +399,7 @@ static void caching_topic_exec(void *data, struct stasis_subscription *sub, stru
        struct stasis_caching_topic *caching_topic = data;
        const char *id = NULL;
 
+       ast_assert(caching_topic != NULL);
        ast_assert(caching_topic->topic != NULL);
        ast_assert(caching_topic->id_fn != NULL);
 
@@ -451,10 +462,6 @@ static void caching_topic_exec(void *data, struct stasis_subscription *sub, stru
 
                stasis_publish(caching_topic->topic, update);
        }
-
-       if (stasis_subscription_final_message(sub, message)) {
-               ao2_cleanup(caching_topic);
-       }
 }
 
 struct stasis_caching_topic *stasis_caching_topic_create(struct stasis_topic *original_topic, snapshot_get_id id_fn)
@@ -499,7 +506,7 @@ struct stasis_caching_topic *stasis_caching_topic_create(struct stasis_topic *or
        ao2_ref(caching_topic, +1);
        caching_topic->sub = sub;
 
-       ao2_ref(caching_topic, +1);
+       /* The subscription holds the reference, so no additional ref bump. */
        return caching_topic;
 }