[Asterisk-code-review] Fix deadlock handling subscribe req during res parking reload (asterisk[13])

George Joseph asteriskteam at digium.com
Tue Feb 5 11:37:35 CST 2019


George Joseph has submitted this change and it was merged. ( https://gerrit.asterisk.org/10701 )

Change subject: Fix deadlock handling subscribe req during res_parking reload
......................................................................

Fix deadlock handling subscribe req during res_parking reload

Split destroy_hint method to separate hint removal and extension hint
state changed callback, the latter now called via stasis.
This avoids deadlock between res_parking reload that is removing the
parking lot and the related hint and subscribe requests coming for the
same parking lot.

ASTERISK-28173

Change-Id: I5b03c3455b3b12b6f83cea4cc34f4b4b20444f7e
---
M main/pbx.c
1 file changed, 55 insertions(+), 30 deletions(-)

Approvals:
  Joshua C. Colp: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved; Approved for Submit



diff --git a/main/pbx.c b/main/pbx.c
index 93bd9e6..b4a5448 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -373,6 +373,7 @@
 };
 
 STASIS_MESSAGE_TYPE_DEFN_LOCAL(hint_change_message_type);
+STASIS_MESSAGE_TYPE_DEFN_LOCAL(hint_remove_message_type);
 
 #define HINTDEVICE_DATA_LENGTH 16
 AST_THREADSTORAGE(hintdevice_data);
@@ -3560,6 +3561,30 @@
 		return;
 	}
 
+	if (hint_remove_message_type() == stasis_message_type(msg)) {
+		/* The extension has already been destroyed */
+		struct ast_state_cb *state_cb;
+		struct ao2_iterator cb_iter;
+		struct ast_hint *hint = stasis_message_data(msg);
+
+		ao2_lock(hint);
+		hint->laststate = AST_EXTENSION_DEACTIVATED;
+		ao2_unlock(hint);
+
+		cb_iter = ao2_iterator_init(hint->callbacks, 0);
+		for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
+			execute_state_callback(state_cb->change_cb,
+			        hint->context_name,
+			        hint->exten_name,
+			        state_cb->data,
+			        AST_HINT_UPDATE_DEVICE,
+			        hint,
+			        NULL);
+		}
+		ao2_iterator_destroy(&cb_iter);
+		return;
+	}
+
 	if (ast_device_state_message_type() != stasis_message_type(msg)) {
 		return;
 	}
@@ -3809,34 +3834,7 @@
 	struct ast_hint *hint = obj;
 	int i;
 
-	if (hint->callbacks) {
-		struct ast_state_cb *state_cb;
-		const char *context_name;
-		const char *exten_name;
-
-		if (hint->exten) {
-			context_name = ast_get_context_name(ast_get_extension_context(hint->exten));
-			exten_name = ast_get_extension_name(hint->exten);
-			hint->exten = NULL;
-		} else {
-			/* The extension has already been destroyed */
-			context_name = hint->context_name;
-			exten_name = hint->exten_name;
-		}
-		hint->laststate = AST_EXTENSION_DEACTIVATED;
-		while ((state_cb = ao2_callback(hint->callbacks, OBJ_UNLINK, NULL, NULL))) {
-			/* Notify with -1 and remove all callbacks */
-			execute_state_callback(state_cb->change_cb,
-				context_name,
-				exten_name,
-				state_cb->data,
-				AST_HINT_UPDATE_DEVICE,
-				hint,
-				NULL);
-			ao2_ref(state_cb, -1);
-		}
-		ao2_ref(hint->callbacks, -1);
-	}
+	ao2_cleanup(hint->callbacks);
 
 	for (i = 0; i < AST_VECTOR_SIZE(&hint->devices); i++) {
 		char *device = AST_VECTOR_GET(&hint->devices, i);
@@ -3847,6 +3845,27 @@
 	ast_free(hint->last_presence_message);
 }
 
+/*! \brief Publish a hint removed event  */
+static int publish_hint_remove(struct ast_hint *hint)
+{
+	struct stasis_message *message;
+
+	if (!hint_remove_message_type()) {
+		return -1;
+	}
+
+	if (!(message = stasis_message_create(hint_remove_message_type(), hint))) {
+		ao2_ref(hint, -1);
+		return -1;
+	}
+
+	stasis_publish(ast_device_state_topic_all(), message);
+
+	ao2_ref(message, -1);
+
+	return 0;
+}
+
 /*! \brief Remove hint from extension */
 static int ast_remove_hint(struct ast_exten *e)
 {
@@ -3877,6 +3896,8 @@
 	hint->exten = NULL;
 	ao2_unlock(hint);
 
+	publish_hint_remove(hint);
+
 	ao2_ref(hint, -1);
 
 	return 0;
@@ -3983,8 +4004,7 @@
 		return -1;
 	}
 
-	/* The message is going to be published to two topics so the hint needs two refs */
-	if (!(message = stasis_message_create(hint_change_message_type(), ao2_bump(hint)))) {
+	if (!(message = stasis_message_create(hint_change_message_type(), hint))) {
 		ao2_ref(hint, -1);
 		return -1;
 	}
@@ -8341,6 +8361,7 @@
 	}
 	stasis_subscription_accept_message_type(device_state_sub, ast_device_state_message_type());
 	stasis_subscription_accept_message_type(device_state_sub, hint_change_message_type());
+	stasis_subscription_accept_message_type(device_state_sub, hint_remove_message_type());
 	stasis_subscription_set_filter(device_state_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
 
 	if (!(presence_state_sub = stasis_subscribe(ast_presence_state_topic_all(), presence_state_cb, NULL))) {
@@ -8697,6 +8718,7 @@
 static void pbx_shutdown(void)
 {
 	STASIS_MESSAGE_TYPE_CLEANUP(hint_change_message_type);
+	STASIS_MESSAGE_TYPE_CLEANUP(hint_remove_message_type);
 
 	if (hints) {
 		ao2_container_unregister("hints");
@@ -8773,6 +8795,9 @@
 	if (STASIS_MESSAGE_TYPE_INIT(hint_change_message_type) != 0) {
 		return -1;
 	}
+	if (STASIS_MESSAGE_TYPE_INIT(hint_remove_message_type) != 0) {
+		return -1;
+	}
 
 	return (hints && hintdevices && statecbs) ? 0 : -1;
 }

-- 
To view, visit https://gerrit.asterisk.org/10701
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-MessageType: merged
Gerrit-Change-Id: I5b03c3455b3b12b6f83cea4cc34f4b4b20444f7e
Gerrit-Change-Number: 10701
Gerrit-PatchSet: 5
Gerrit-Owner: Giuseppe Sucameli <sucameli at netresults.it>
Gerrit-Reviewer: Friendly Automation (1000185)
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Giuseppe Sucameli <sucameli at netresults.it>
Gerrit-Reviewer: Joshua C. Colp <jcolp at digium.com>
Gerrit-Reviewer: Matthew Fredrickson <creslin at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190205/01e35afd/attachment.html>


More information about the asterisk-code-review mailing list