<p>Benjamin Keith Ford has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/11304">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">stasis: Fix crash at shutdown.<br><br>When compiling in dev mode, stasis statistics are enabled and can cause<br>a crash at shutdown due to the following:<br>- Containers are freed<br>- Topics and subscriptions remain<br>- When those topics and subscriptions are deallocated, they go to do<br> things with the container<br><br>This changes the containers to global ao2 objects, and whenever needed<br>in the code, a reference must be obtained and checked before any<br>operations can be done.<br><br>ASTERISK-28353 #close<br><br>Change-Id: Ie7d5e907fcfcb4d65bd36d5e4eb923126fde8d33<br>---<br>M main/stasis.c<br>1 file changed, 92 insertions(+), 22 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/04/11304/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/main/stasis.c b/main/stasis.c</span><br><span>index 3fee6ba..13cdb71 100644</span><br><span>--- a/main/stasis.c</span><br><span>+++ b/main/stasis.c</span><br><span>@@ -317,11 +317,11 @@</span><br><span> /*! The number of buckets to use for subscription statistics */</span><br><span> #define SUBSCRIPTION_STATISTICS_BUCKETS 57</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-/*! Container which stores statistics for topics */</span><br><span style="color: hsl(0, 100%, 40%);">-static struct ao2_container *topic_statistics;</span><br><span style="color: hsl(120, 100%, 40%);">+/*! Global container which stores statistics for topics */</span><br><span style="color: hsl(120, 100%, 40%);">+static AO2_GLOBAL_OBJ_STATIC(topic_statistics);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-/*! Container which stores statistics for subscriptions */</span><br><span style="color: hsl(0, 100%, 40%);">-static struct ao2_container *subscription_statistics;</span><br><span style="color: hsl(120, 100%, 40%);">+/*! Global container which stores statistics for subscriptions */</span><br><span style="color: hsl(120, 100%, 40%);">+static AO2_GLOBAL_OBJ_STATIC(subscription_statistics);</span><br><span> </span><br><span> /*! \internal */</span><br><span> struct stasis_message_type_statistics {</span><br><span>@@ -395,6 +395,9 @@</span><br><span> static void topic_dtor(void *obj)</span><br><span> {</span><br><span> struct stasis_topic *topic = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef AST_DEVMODE</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_container *topic_stats;</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span> </span><br><span> /* Subscribers hold a reference to topics, so they should all be</span><br><span> * unsubscribed before we get here. */</span><br><span>@@ -405,10 +408,12 @@</span><br><span> ast_debug(1, "Topic '%s': %p destroyed\n", topic->name, topic);</span><br><span> </span><br><span> #ifdef AST_DEVMODE</span><br><span style="color: hsl(0, 100%, 40%);">- if (topic->statistics) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlink(topic_statistics, topic->statistics);</span><br><span style="color: hsl(120, 100%, 40%);">+ topic_stats = ao2_global_obj_ref(topic_statistics);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (topic->statistics && topic_stats) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlink(topic_stats, topic->statistics);</span><br><span> ao2_ref(topic->statistics, -1);</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_cleanup(topic_stats);</span><br><span> #endif</span><br><span> }</span><br><span> </span><br><span>@@ -423,6 +428,11 @@</span><br><span> static struct stasis_topic_statistics *stasis_topic_statistics_create(struct stasis_topic *topic)</span><br><span> {</span><br><span> struct stasis_topic_statistics *statistics;</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ao2_container *, topic_stats, ao2_global_obj_ref(topic_statistics), ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!topic_stats) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span> statistics = ao2_alloc(sizeof(*statistics) + strlen(topic->name) + 1, topic_statistics_destroy);</span><br><span> if (!statistics) {</span><br><span>@@ -438,7 +448,7 @@</span><br><span> /* This is strictly used for the pointer address when showing the topic */</span><br><span> statistics->topic = topic;</span><br><span> strcpy(statistics->name, topic->name); /* SAFE */</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_link(topic_statistics, statistics);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_link(topic_stats, statistics);</span><br><span> </span><br><span> return statistics;</span><br><span> }</span><br><span>@@ -552,6 +562,9 @@</span><br><span> static void subscription_dtor(void *obj)</span><br><span> {</span><br><span> struct stasis_subscription *sub = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef AST_DEVMODE</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_container *subscription_stats;</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span> </span><br><span> /* Subscriptions need to be manually unsubscribed before destruction</span><br><span> * b/c there's a cyclic reference between topics and subscriptions */</span><br><span>@@ -570,10 +583,12 @@</span><br><span> AST_VECTOR_FREE(&sub->accepted_message_types);</span><br><span> </span><br><span> #ifdef AST_DEVMODE</span><br><span style="color: hsl(0, 100%, 40%);">- if (sub->statistics) {</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_unlink(subscription_statistics, sub->statistics);</span><br><span style="color: hsl(120, 100%, 40%);">+ subscription_stats = ao2_global_obj_ref(subscription_statistics);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sub->statistics && subscription_stats) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_unlink(subscription_stats, sub->statistics);</span><br><span> ao2_ref(sub->statistics, -1);</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_cleanup(subscription_stats);</span><br><span> #endif</span><br><span> }</span><br><span> </span><br><span>@@ -655,6 +670,11 @@</span><br><span> const char *func)</span><br><span> {</span><br><span> struct stasis_subscription_statistics *statistics;</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ao2_container *, subscription_stats, ao2_global_obj_ref(subscription_statistics), ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!subscription_stats) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span> statistics = ao2_alloc(sizeof(*statistics) + strlen(sub->uniqueid) + 1, subscription_statistics_destroy);</span><br><span> if (!statistics) {</span><br><span>@@ -674,7 +694,7 @@</span><br><span> statistics->uses_threadpool = use_thread_pool;</span><br><span> strcpy(statistics->uniqueid, sub->uniqueid); /* SAFE */</span><br><span> statistics->sub = sub;</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_link(subscription_statistics, statistics);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_link(subscription_stats, statistics);</span><br><span> </span><br><span> return statistics;</span><br><span> }</span><br><span>@@ -2152,6 +2172,7 @@</span><br><span> int count = 0;</span><br><span> int dropped = 0;</span><br><span> int passed = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ao2_container *, subscription_stats, ao2_global_obj_ref(subscription_statistics), ao2_cleanup);</span><br><span> #define FMT_HEADERS "%-64s %10s %10s %16s %16s\n"</span><br><span> #define FMT_FIELDS "%-64s %10d %10d %16ld %16ld\n"</span><br><span> #define FMT_FIELDS2 "%-64s %10d %10d\n"</span><br><span>@@ -2178,7 +2199,13 @@</span><br><span> return CLI_SUCCESS;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (ao2_container_dup(sorted_subscriptions, subscription_statistics, 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!subscription_stats) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(sorted_subscriptions, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "Could not fetch subscription_statistics container\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return CLI_FAILURE;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ao2_container_dup(sorted_subscriptions, subscription_stats, 0)) {</span><br><span> ao2_ref(sorted_subscriptions, -1);</span><br><span> ast_cli(a->fd, "Could not sort subscription statistics\n");</span><br><span> return CLI_SUCCESS;</span><br><span>@@ -2216,12 +2243,18 @@</span><br><span> static char *subscription_statistics_complete_name(const char *word, int state)</span><br><span> {</span><br><span> struct stasis_subscription_statistics *statistics;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_container *subscription_stats;</span><br><span> struct ao2_iterator it_statistics;</span><br><span> int wordlen = strlen(word);</span><br><span> int which = 0;</span><br><span> char *result = NULL;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- it_statistics = ao2_iterator_init(subscription_statistics, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ subscription_stats = ao2_global_obj_ref(subscription_statistics);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!subscription_stats) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return result;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ it_statistics = ao2_iterator_init(subscription_stats, 0);</span><br><span> while ((statistics = ao2_iterator_next(&it_statistics))) {</span><br><span> if (!strncasecmp(word, statistics->uniqueid, wordlen)</span><br><span> && ++which > state) {</span><br><span>@@ -2233,6 +2266,7 @@</span><br><span> }</span><br><span> }</span><br><span> ao2_iterator_destroy(&it_statistics);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(subscription_stats, -1);</span><br><span> return result;</span><br><span> }</span><br><span> </span><br><span>@@ -2245,6 +2279,7 @@</span><br><span> struct stasis_subscription_statistics *statistics;</span><br><span> struct ao2_iterator i;</span><br><span> char *name;</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ao2_container *, subscription_stats, ao2_global_obj_ref(subscription_statistics), ao2_cleanup);</span><br><span> </span><br><span> switch (cmd) {</span><br><span> case CLI_INIT:</span><br><span>@@ -2265,7 +2300,12 @@</span><br><span> return CLI_SHOWUSAGE;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- statistics = ao2_find(subscription_statistics, a->argv[4], OBJ_SEARCH_KEY);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!subscription_stats) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "Could not fetch subcription_statistics container\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return CLI_FAILURE;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ statistics = ao2_find(subscription_stats, a->argv[4], OBJ_SEARCH_KEY);</span><br><span> if (!statistics) {</span><br><span> ast_cli(a->fd, "Specified subscription '%s' does not exist\n", a->argv[4]);</span><br><span> return CLI_FAILURE;</span><br><span>@@ -2318,6 +2358,7 @@</span><br><span> int count = 0;</span><br><span> int not_dispatched = 0;</span><br><span> int dispatched = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ao2_container *, topic_stats, ao2_global_obj_ref(topic_statistics), ao2_cleanup);</span><br><span> #define FMT_HEADERS "%-64s %10s %10s %10s %16s %16s\n"</span><br><span> #define FMT_FIELDS "%-64s %10d %10d %10d %16ld %16ld\n"</span><br><span> #define FMT_FIELDS2 "%-64s %10s %10d %10d\n"</span><br><span>@@ -2344,7 +2385,13 @@</span><br><span> return CLI_SUCCESS;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (ao2_container_dup(sorted_topics, topic_statistics, 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!topic_stats) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(sorted_topics, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "Could not fetch topic_statistics container\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return CLI_FAILURE;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ao2_container_dup(sorted_topics, topic_stats, 0)) {</span><br><span> ao2_ref(sorted_topics, -1);</span><br><span> ast_cli(a->fd, "Could not sort topic statistics\n");</span><br><span> return CLI_SUCCESS;</span><br><span>@@ -2383,12 +2430,18 @@</span><br><span> static char *topic_statistics_complete_name(const char *word, int state)</span><br><span> {</span><br><span> struct stasis_topic_statistics *statistics;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_container *topic_stats;</span><br><span> struct ao2_iterator it_statistics;</span><br><span> int wordlen = strlen(word);</span><br><span> int which = 0;</span><br><span> char *result = NULL;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- it_statistics = ao2_iterator_init(topic_statistics, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+ topic_stats = ao2_global_obj_ref(topic_statistics);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!topic_stats) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return result;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ it_statistics = ao2_iterator_init(topic_stats, 0);</span><br><span> while ((statistics = ao2_iterator_next(&it_statistics))) {</span><br><span> if (!strncasecmp(word, statistics->name, wordlen)</span><br><span> && ++which > state) {</span><br><span>@@ -2400,6 +2453,7 @@</span><br><span> }</span><br><span> }</span><br><span> ao2_iterator_destroy(&it_statistics);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(topic_stats, -1);</span><br><span> return result;</span><br><span> }</span><br><span> </span><br><span>@@ -2412,6 +2466,7 @@</span><br><span> struct stasis_topic_statistics *statistics;</span><br><span> struct ao2_iterator i;</span><br><span> char *uniqueid;</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ao2_container *, topic_stats, ao2_global_obj_ref(topic_statistics), ao2_cleanup);</span><br><span> </span><br><span> switch (cmd) {</span><br><span> case CLI_INIT:</span><br><span>@@ -2432,7 +2487,11 @@</span><br><span> return CLI_SHOWUSAGE;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- statistics = ao2_find(topic_statistics, a->argv[4], OBJ_SEARCH_KEY);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!topic_stats) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "Could not fetch topic_statistics container\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return CLI_FAILURE;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ statistics = ao2_find(topic_stats, a->argv[4], OBJ_SEARCH_KEY);</span><br><span> if (!statistics) {</span><br><span> ast_cli(a->fd, "Specified topic '%s' does not exist\n", a->argv[4]);</span><br><span> return CLI_FAILURE;</span><br><span>@@ -2645,8 +2704,8 @@</span><br><span> #ifdef AST_DEVMODE</span><br><span> ast_cli_unregister_multiple(cli_stasis_statistics, ARRAY_LEN(cli_stasis_statistics));</span><br><span> AST_VECTOR_FREE(&message_type_statistics);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_cleanup(subscription_statistics);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_cleanup(topic_statistics);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_global_obj_release(subscription_statistics);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_global_obj_release(topic_statistics);</span><br><span> #endif</span><br><span> ast_threadpool_shutdown(pool);</span><br><span> pool = NULL;</span><br><span>@@ -2661,6 +2720,10 @@</span><br><span> struct stasis_config *cfg;</span><br><span> int cache_init;</span><br><span> struct ast_threadpool_options threadpool_opts = { 0, };</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef AST_DEVMODE</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_container *subscription_stats;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_container *topic_stats;</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span> </span><br><span> /* Be sure the types are cleaned up after the message bus */</span><br><span> ast_register_cleanup(stasis_cleanup);</span><br><span>@@ -2746,15 +2809,22 @@</span><br><span> /* Statistics information is stored separately so that we don't alter or interrupt the lifetime of the underlying</span><br><span> * topic or subscripton.</span><br><span> */</span><br><span style="color: hsl(0, 100%, 40%);">- subscription_statistics = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, SUBSCRIPTION_STATISTICS_BUCKETS,</span><br><span style="color: hsl(120, 100%, 40%);">+ subscription_stats = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, SUBSCRIPTION_STATISTICS_BUCKETS,</span><br><span> subscription_statistics_hash, 0, subscription_statistics_cmp);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!subscription_statistics) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!subscription_stats) {</span><br><span> return -1;</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_global_obj_replace_unref(subscription_statistics, subscription_stats);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_cleanup(subscription_stats);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- topic_statistics = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, TOPIC_STATISTICS_BUCKETS,</span><br><span style="color: hsl(120, 100%, 40%);">+ topic_stats = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, TOPIC_STATISTICS_BUCKETS,</span><br><span> topic_statistics_hash, 0, topic_statistics_cmp);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!topic_statistics) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!topic_stats) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_global_obj_replace_unref(topic_statistics, topic_stats);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_cleanup(topic_stats);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!topic_stats) {</span><br><span> return -1;</span><br><span> }</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/11304">change 11304</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/11304"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 13 </div>
<div style="display:none"> Gerrit-Change-Id: Ie7d5e907fcfcb4d65bd36d5e4eb923126fde8d33 </div>
<div style="display:none"> Gerrit-Change-Number: 11304 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Benjamin Keith Ford <bford@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>