[asterisk-commits] dlee: trunk r390122 - in /trunk: include/ include/asterisk/ main/
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Thu May 30 12:06:06 CDT 2013
Author: dlee
Date: Thu May 30 12:05:53 2013
New Revision: 390122
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=390122
Log:
Avoid unnecessary cleanups during immediate shutdown
This patch addresses issues during immediate shutdowns, where modules
are not unloaded, but Asterisk atexit handlers are run.
In the typical case, this usually isn't a big deal. But the
introduction of the Stasis message bus makes it much more likely for
asynchronous activity to be happening off in some thread during
shutdown.
During an immediate shutdown, Asterisk skips unloading modules. But
while it is processing the atexit handlers, there is a window of time
where some of the core message types have been cleaned up, but the
message bus is still running. Specifically, it's still running
module subscriptions that might be using the core message types. If a
message is received by that subscription in that window, it will
attempt to use a message type that has been cleaned up.
To solve this problem, this patch introduces ast_register_cleanup().
This function operates identically to ast_register_atexit(), except
that cleanup calls are not invoked on an immediate shutdown. All of
the core message type and topic cleanup was moved from atexit handlers
to cleanup handlers.
This ensures that core type and topic cleanup only happens if the
modules that used them are first unloaded.
This patch also changes the ast_assert() when accessing a cleaned up
or uninitialized message type to an error log message. Message type
functions are actually NULL safe across the board, so the assert was a
bit heavy handed. Especially for anyone with DO_CRASH enabled.
Review: https://reviewboard.asterisk.org/r/2562/
Modified:
trunk/include/asterisk.h
trunk/include/asterisk/security_events.h
trunk/include/asterisk/stasis.h
trunk/include/asterisk/stasis_bridging.h
trunk/include/asterisk/stasis_channels.h
trunk/main/app.c
trunk/main/asterisk.c
trunk/main/bridging.c
trunk/main/channel.c
trunk/main/devicestate.c
trunk/main/named_acl.c
trunk/main/presencestate.c
trunk/main/security_events.c
trunk/main/stasis.c
trunk/main/stasis_bridging.c
trunk/main/stasis_cache.c
trunk/main/stasis_channels.c
trunk/main/test.c
Modified: trunk/include/asterisk.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk.h?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/include/asterisk.h (original)
+++ trunk/include/asterisk.h Thu May 30 12:05:53 2013
@@ -90,6 +90,22 @@
int ast_register_atexit(void (*func)(void));
/*!
+ * \since 12
+ * \brief Register a function to be executed before Asterisk gracefully exits.
+ *
+ * If Asterisk is immediately shutdown (core stop now, or sending the TERM
+ * signal), the callback is not run. When the callbacks are run, they are run in
+ * sequence with ast_register_atexit() callbacks, in the reverse order of
+ * registration.
+ *
+ * \param func The callback function to use.
+ *
+ * \retval 0 on success.
+ * \retval -1 on error.
+ */
+int ast_register_cleanup(void (*func)(void));
+
+/*!
* \brief Unregister a function registered with ast_register_atexit().
* \param func The callback function to unregister.
*/
Modified: trunk/include/asterisk/security_events.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/security_events.h?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/include/asterisk/security_events.h (original)
+++ trunk/include/asterisk/security_events.h Thu May 30 12:05:53 2013
@@ -87,12 +87,6 @@
int ast_security_stasis_init(void);
/*!
- * \brief removes stasis topic/event types for \ref ast_security_topic and \ref ast_security_event_type
- * \since 12
- */
-void ast_security_stasis_cleanup(void);
-
-/*!
* \brief Get the list of required IEs for a given security event sub-type
*
* \param[in] event_type security event sub-type
Modified: trunk/include/asterisk/stasis.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/stasis.h?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/include/asterisk/stasis.h (original)
+++ trunk/include/asterisk/stasis.h Thu May 30 12:05:53 2013
@@ -633,6 +633,12 @@
/*! @{ */
/*!
+ * \internal
+ * \brief Log a message about invalid attempt to access a type.
+ */
+void stasis_log_bad_type_access(const char *name);
+
+/*!
* \brief Boiler-plate removing macro for defining message types.
*
* \param name Name of message type.
@@ -641,7 +647,9 @@
#define STASIS_MESSAGE_TYPE_DEFN(name) \
static struct stasis_message_type *_priv_ ## name; \
struct stasis_message_type *name(void) { \
- ast_assert(_priv_ ## name != NULL); \
+ if (_priv_ ## name == NULL) { \
+ stasis_log_bad_type_access(#name); \
+ } \
return _priv_ ## name; \
}
@@ -663,6 +671,15 @@
/*!
* \brief Boiler-plate removing macro for cleaning up message types.
*
+ * Note that if your type is defined in core instead of a loadable module, you
+ * should call message type cleanup from an ast_register_cleanup() handler
+ * instead of an ast_register_atexit() handler.
+ *
+ * The reason is that during an immediate shutdown, loadable modules (which may
+ * refer to core message types) are not unloaded. While the atexit handlers are
+ * run, there's a window of time where a module subscription might reference a
+ * core message type after it's been cleaned up. Which is bad.
+ *
* \param name Name of message type.
* \since 12
*/
Modified: trunk/include/asterisk/stasis_bridging.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/stasis_bridging.h?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/include/asterisk/stasis_bridging.h (original)
+++ trunk/include/asterisk/stasis_bridging.h Thu May 30 12:05:53 2013
@@ -220,11 +220,6 @@
struct ast_json *ast_bridge_snapshot_to_json(const struct ast_bridge_snapshot *snapshot);
/*!
- * \brief Dispose of the stasis bridging topics and message types
- */
-void ast_stasis_bridging_shutdown(void);
-
-/*!
* \brief Initialize the stasis bridging topic and message types
* \retval 0 on success
* \retval -1 on failure
Modified: trunk/include/asterisk/stasis_channels.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/stasis_channels.h?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/include/asterisk/stasis_channels.h (original)
+++ trunk/include/asterisk/stasis_channels.h Thu May 30 12:05:53 2013
@@ -462,11 +462,6 @@
const struct ast_channel_snapshot *new_snapshot);
/*!
- * \brief Dispose of the stasis channel topics and message types
- */
-void ast_stasis_channels_shutdown(void);
-
-/*!
* \brief Initialize the stasis channel topic and message types
*/
void ast_stasis_channels_init(void);
Modified: trunk/main/app.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/app.c?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/main/app.c (original)
+++ trunk/main/app.c Thu May 30 12:05:53 2013
@@ -2816,7 +2816,7 @@
return msg;
}
-static void app_exit(void)
+static void app_cleanup(void)
{
ao2_cleanup(mwi_topic_all);
mwi_topic_all = NULL;
@@ -2829,6 +2829,8 @@
int app_init(void)
{
+ ast_register_atexit(app_cleanup);
+
if (STASIS_MESSAGE_TYPE_INIT(ast_mwi_state_type) != 0) {
return -1;
}
@@ -2848,7 +2850,6 @@
return -1;
}
- ast_register_atexit(app_exit);
return 0;
}
Modified: trunk/main/asterisk.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/asterisk.c?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/main/asterisk.c (original)
+++ trunk/main/asterisk.c Thu May 30 12:05:53 2013
@@ -347,6 +347,7 @@
struct ast_atexit {
void (*func)(void);
+ int is_cleanup;
AST_LIST_ENTRY(ast_atexit) list;
};
@@ -1111,7 +1112,7 @@
/*! \brief Initialize the system level items for \ref stasis */
static int stasis_system_topic_init(void)
{
- ast_register_atexit(stasis_system_topic_cleanup);
+ ast_register_cleanup(stasis_system_topic_cleanup);
system_topic = stasis_topic_create("ast_system");
if (!system_topic) {
@@ -1170,13 +1171,13 @@
publish_system_message("FullyBooted", json_object);
}
-static void ast_run_atexits(void)
+static void ast_run_atexits(int run_cleanups)
{
struct ast_atexit *ae;
AST_LIST_LOCK(&atexits);
while ((ae = AST_LIST_REMOVE_HEAD(&atexits, list))) {
- if (ae->func) {
+ if (ae->func && (!ae->is_cleanup || run_cleanups)) {
ae->func();
}
ast_free(ae);
@@ -1198,7 +1199,7 @@
AST_LIST_TRAVERSE_SAFE_END;
}
-int ast_register_atexit(void (*func)(void))
+static int register_atexit(void (*func)(void), int is_cleanup)
{
struct ast_atexit *ae;
@@ -1207,6 +1208,7 @@
return -1;
}
ae->func = func;
+ ae->is_cleanup = is_cleanup;
AST_LIST_LOCK(&atexits);
__ast_unregister_atexit(func);
@@ -1214,6 +1216,16 @@
AST_LIST_UNLOCK(&atexits);
return 0;
+}
+
+int ast_register_atexit(void (*func)(void))
+{
+ return register_atexit(func, 0);
+}
+
+int ast_register_cleanup(void (*func)(void))
+{
+ return register_atexit(func, 1);
}
void ast_unregister_atexit(void (*func)(void))
@@ -1980,8 +1992,9 @@
{
int active_channels;
RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
-
- if (niceness >= SHUTDOWN_NICE) {
+ int run_cleanups = niceness >= SHUTDOWN_NICE;
+
+ if (run_cleanups) {
ast_module_shutdown();
}
@@ -2021,7 +2034,7 @@
active_channels ? "uncleanly" : "cleanly", num);
ast_verb(0, "Executing last minute cleanups\n");
- ast_run_atexits();
+ ast_run_atexits(run_cleanups);
ast_debug(1, "Asterisk ending (%d).\n", num);
if (ast_socket > -1) {
@@ -4330,7 +4343,6 @@
}
if (ast_security_stasis_init()) { /* Initialize Security Stasis Topic and Events */
- ast_security_stasis_cleanup();
printf("%s", term_quit());
exit(1);
}
Modified: trunk/main/bridging.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/bridging.c?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/main/bridging.c (original)
+++ trunk/main/bridging.c Thu May 30 12:05:53 2013
@@ -6247,26 +6247,24 @@
bridges = NULL;
ao2_cleanup(bridge_manager);
bridge_manager = NULL;
- ast_stasis_bridging_shutdown();
}
int ast_bridging_init(void)
{
+ ast_register_atexit(bridge_shutdown);
+
if (ast_stasis_bridging_init()) {
- bridge_shutdown();
return -1;
}
bridge_manager = bridge_manager_create();
if (!bridge_manager) {
- bridge_shutdown();
return -1;
}
bridges = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_MUTEX,
AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE, bridge_sort_cmp, NULL);
if (!bridges) {
- bridge_shutdown();
return -1;
}
@@ -6275,6 +6273,5 @@
/* BUGBUG need AMI action equivalents to the CLI commands. */
ast_cli_register_multiple(bridge_cli, ARRAY_LEN(bridge_cli));
- ast_register_atexit(bridge_shutdown);
return 0;
}
Modified: trunk/main/channel.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/channel.c?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/main/channel.c (original)
+++ trunk/main/channel.c Thu May 30 12:05:53 2013
@@ -8603,9 +8603,6 @@
static void channels_shutdown(void)
{
-
- ast_stasis_channels_shutdown();
-
free_channelvars();
ast_data_unregister(NULL);
Modified: trunk/main/devicestate.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/devicestate.c?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/main/devicestate.c (original)
+++ trunk/main/devicestate.c Thu May 30 12:05:53 2013
@@ -772,7 +772,7 @@
return device_state->cache_id;
}
-static void devstate_exit(void)
+static void devstate_cleanup(void)
{
ao2_cleanup(device_state_topic_all);
device_state_topic_all = NULL;
@@ -784,6 +784,8 @@
int devstate_init(void)
{
+ ast_register_cleanup(devstate_cleanup);
+
if (STASIS_MESSAGE_TYPE_INIT(ast_device_state_message_type) != 0) {
return -1;
}
@@ -807,6 +809,5 @@
return -1;
}
- ast_register_atexit(devstate_exit);
return 0;
}
Modified: trunk/main/named_acl.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/named_acl.c?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/main/named_acl.c (original)
+++ trunk/main/named_acl.c Thu May 30 12:05:53 2013
@@ -360,7 +360,7 @@
/*! \brief Message type for named ACL changes */
STASIS_MESSAGE_TYPE_DEFN(ast_named_acl_change_type);
-static void acl_stasis_shutdown(void)
+static void acl_stasis_cleanup(void)
{
STASIS_MESSAGE_TYPE_CLEANUP(ast_named_acl_change_type);
}
@@ -371,7 +371,7 @@
*/
static void ast_acl_stasis_init(void)
{
- ast_register_atexit(acl_stasis_shutdown);
+ ast_register_cleanup(acl_stasis_cleanup);
STASIS_MESSAGE_TYPE_INIT(ast_named_acl_change_type);
}
Modified: trunk/main/presencestate.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/presencestate.c?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/main/presencestate.c (original)
+++ trunk/main/presencestate.c Thu May 30 12:05:53 2013
@@ -321,6 +321,8 @@
int ast_presence_state_engine_init(void)
{
+ ast_register_cleanup(presence_state_engine_cleanup);
+
if (STASIS_MESSAGE_TYPE_INIT(ast_presence_state_message_type) != 0) {
return -1;
}
@@ -334,7 +336,6 @@
if (!presence_state_topic_cached) {
return -1;
}
- ast_register_atexit(presence_state_engine_cleanup);
return 0;
}
Modified: trunk/main/security_events.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/security_events.c?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/main/security_events.c (original)
+++ trunk/main/security_events.c Thu May 30 12:05:53 2013
@@ -54,8 +54,18 @@
/*! \brief Message type for security events */
STASIS_MESSAGE_TYPE_DEFN(ast_security_event_type);
+static void security_stasis_cleanup(void)
+{
+ ao2_cleanup(security_topic);
+ security_topic = NULL;
+
+ STASIS_MESSAGE_TYPE_CLEANUP(ast_security_event_type);
+}
+
int ast_security_stasis_init(void)
{
+ ast_register_cleanup(security_stasis_cleanup);
+
security_topic = stasis_topic_create("ast_security");
if (!security_topic) {
return -1;
@@ -65,19 +75,8 @@
return -1;
}
- if (ast_register_atexit(ast_security_stasis_cleanup)) {
- return -1;
- }
return 0;
-}
-
-void ast_security_stasis_cleanup(void)
-{
- STASIS_MESSAGE_TYPE_CLEANUP(ast_security_event_type);
-
- ao2_cleanup(security_topic);
- security_topic = NULL;
}
static const struct {
Modified: trunk/main/stasis.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/stasis.c?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/main/stasis.c (original)
+++ trunk/main/stasis.c Thu May 30 12:05:53 2013
@@ -619,11 +619,21 @@
return topic_pool_entry->topic;
}
+void stasis_log_bad_type_access(const char *name)
+{
+ ast_log(LOG_ERROR, "Use of %s() before init/after destruction\n", name);
+}
+
/*! \brief Cleanup function */
static void stasis_exit(void)
{
ast_threadpool_shutdown(pool);
pool = NULL;
+}
+
+/*! \brief Cleanup function for graceful shutdowns */
+static void stasis_cleanup(void)
+{
STASIS_MESSAGE_TYPE_CLEANUP(stasis_subscription_change_type);
}
@@ -640,6 +650,8 @@
.max_size = 200
};
+ /* Be sure the types are cleaned up after the message bus */
+ ast_register_cleanup(stasis_cleanup);
ast_register_atexit(stasis_exit);
if (pool) {
Modified: trunk/main/stasis_bridging.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/stasis_bridging.c?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/main/stasis_bridging.c (original)
+++ trunk/main/stasis_bridging.c Thu May 30 12:05:53 2013
@@ -319,7 +319,7 @@
return ast_json_ref(json_chan);
}
-void ast_stasis_bridging_shutdown(void)
+static void stasis_bridging_cleanup(void)
{
ao2_cleanup(bridge_topic_all);
bridge_topic_all = NULL;
@@ -347,6 +347,8 @@
int ast_stasis_bridging_init(void)
{
+ ast_register_cleanup(stasis_bridging_cleanup);
+
STASIS_MESSAGE_TYPE_INIT(ast_bridge_snapshot_type);
STASIS_MESSAGE_TYPE_INIT(ast_bridge_merge_message_type);
STASIS_MESSAGE_TYPE_INIT(ast_channel_entered_bridge_type);
Modified: trunk/main/stasis_cache.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/stasis_cache.c?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/main/stasis_cache.c (original)
+++ trunk/main/stasis_cache.c Thu May 30 12:05:53 2013
@@ -446,7 +446,7 @@
return caching_topic;
}
-static void stasis_cache_exit(void)
+static void stasis_cache_cleanup(void)
{
STASIS_MESSAGE_TYPE_CLEANUP(stasis_cache_clear_type);
STASIS_MESSAGE_TYPE_CLEANUP(stasis_cache_update_type);
@@ -454,7 +454,7 @@
int stasis_cache_init(void)
{
- ast_register_atexit(stasis_cache_exit);
+ ast_register_cleanup(stasis_cache_cleanup);
if (STASIS_MESSAGE_TYPE_INIT(stasis_cache_clear_type) != 0) {
return -1;
Modified: trunk/main/stasis_channels.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/stasis_channels.c?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/main/stasis_channels.c (original)
+++ trunk/main/stasis_channels.c Thu May 30 12:05:53 2013
@@ -575,7 +575,7 @@
strcmp(old_snapshot->caller_name, new_snapshot->caller_name) == 0;
}
-void ast_stasis_channels_shutdown(void)
+static void stasis_channels_cleanup(void)
{
channel_topic_all_cached = stasis_caching_unsubscribe_and_join(channel_topic_all_cached);
ao2_cleanup(channel_topic_all);
@@ -601,6 +601,8 @@
void ast_stasis_channels_init(void)
{
+ ast_register_cleanup(stasis_channels_cleanup);
+
STASIS_MESSAGE_TYPE_INIT(ast_channel_snapshot_type);
STASIS_MESSAGE_TYPE_INIT(ast_channel_dial_type);
STASIS_MESSAGE_TYPE_INIT(ast_channel_varset_type);
Modified: trunk/main/test.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/test.c?view=diff&rev=390122&r1=390121&r2=390122
==============================================================================
--- trunk/main/test.c (original)
+++ trunk/main/test.c Thu May 30 12:05:53 2013
@@ -1005,6 +1005,8 @@
int ast_test_init(void)
{
#ifdef TEST_FRAMEWORK
+ ast_register_cleanup(test_cleanup);
+
/* Create stasis topic */
test_suite_topic = stasis_topic_create("test_suite_topic");
if (!test_suite_topic) {
@@ -1017,7 +1019,6 @@
/* Register cli commands */
ast_cli_register_multiple(test_cli, ARRAY_LEN(test_cli));
- ast_register_atexit(test_cleanup);
#endif
return 0;
More information about the asterisk-commits
mailing list