[Asterisk-code-review] res pjsip outbound registration.c: Make use a serializer shu... (asterisk[13])

Richard Mudgett asteriskteam at digium.com
Mon Jun 22 14:15:26 CDT 2015


Richard Mudgett has uploaded a new change for review.

  https://gerrit.asterisk.org/691

Change subject: res_pjsip_outbound_registration.c: Make use a serializer shutdown group.
......................................................................

res_pjsip_outbound_registration.c: Make use a serializer shutdown group.

The client_state objects contain a serializer used to send the outbound
REGISTER messages.  Once all those message transactions are complete then
the module can shutdown.

ASTERISK-24907 #close
Reported by: Kevin Harwell

Change-Id: Ibb2fe558f98190f2a06da830e0fadfa25516f547
---
M res/res_pjsip_outbound_registration.c
1 file changed, 51 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/91/691/1

diff --git a/res/res_pjsip_outbound_registration.c b/res/res_pjsip_outbound_registration.c
index 22636a6..8ad4990 100644
--- a/res/res_pjsip_outbound_registration.c
+++ b/res/res_pjsip_outbound_registration.c
@@ -34,6 +34,7 @@
 #include "asterisk/cli.h"
 #include "asterisk/stasis_system.h"
 #include "asterisk/threadstorage.h"
+#include "asterisk/threadpool.h"
 #include "res_pjsip/include/res_pjsip_private.h"
 
 /*** DOCUMENTATION
@@ -312,6 +313,12 @@
 	struct sip_outbound_registration_client_state *client_state;
 };
 
+/*! Time needs to be long enough for a transaction to timeout if nothing replies. */
+#define MAX_UNLOAD_TIMEOUT_TIME		35	/* Seconds */
+
+/*! Shutdown group to monitor sip_outbound_registration_client_state serializers. */
+static struct ast_serializer_shutdown_group *shutdown_group;
+
 /*! \brief Default number of state container buckets */
 #define DEFAULT_STATE_BUCKETS 53
 static AO2_GLOBAL_OBJ_STATIC(current_states);
@@ -529,12 +536,15 @@
 
 	entry->id = 0;
 
-	ao2_ref(client_state, +1);
+	/*
+	 * Transfer client_state reference to serializer task so the
+	 * nominal path will not dec the client_state ref in this
+	 * pjproject callback thread.
+	 */
 	if (ast_sip_push_task(client_state->serializer, handle_client_registration, client_state)) {
 		ast_log(LOG_WARNING, "Scheduled outbound registration could not be executed.\n");
 		ao2_ref(client_state, -1);
 	}
-	ao2_ref(client_state, -1);
 }
 
 /*! \brief Helper function which sets up the timer to re-register in a specific amount of time */
@@ -814,7 +824,11 @@
 	}
 	response->code = param->code;
 	response->expiration = param->expiration;
-	/* Transfer client_state reference to response. */
+	/*
+	 * Transfer client_state reference to response so the
+	 * nominal path will not dec the client_state ref in this
+	 * pjproject callback thread.
+	 */
 	response->client_state = client_state;
 
 	ast_debug(1, "Received REGISTER response %d(%.*s)\n",
@@ -833,6 +847,11 @@
 		pjsip_rx_data_clone(param->rdata, 0, &response->rdata);
 	}
 
+	/*
+	 * Transfer response reference to serializer task so the
+	 * nominal path will not dec the response ref in this
+	 * pjproject callback thread.
+	 */
 	if (ast_sip_push_task(client_state->serializer, handle_registration_response, response)) {
 		ast_log(LOG_WARNING, "Failed to pass incoming registration response to threadpool\n");
 		ao2_cleanup(response);
@@ -884,7 +903,7 @@
 		return NULL;
 	}
 
-	state->client_state->serializer = ast_sip_create_serializer();
+	state->client_state->serializer = ast_sip_create_serializer_group(shutdown_group);
 	if (!state->client_state->serializer) {
 		ao2_cleanup(state);
 		return NULL;
@@ -1214,7 +1233,8 @@
 		return -1;
 	}
 
-	if (ast_sip_push_task_synchronous(NULL, sip_outbound_registration_regc_alloc, new_state)) {
+	if (ast_sip_push_task_synchronous(new_state->client_state->serializer,
+		sip_outbound_registration_regc_alloc, new_state)) {
 		return -1;
 	}
 
@@ -1785,6 +1805,8 @@
 
 static int unload_module(void)
 {
+	int remaining;
+
 	ast_manager_unregister("PJSIPShowRegistrationsOutbound");
 	ast_manager_unregister("PJSIPUnregister");
 	ast_manager_unregister("PJSIPRegister");
@@ -1802,6 +1824,25 @@
 
 	ao2_global_obj_release(current_states);
 
+	/* Wait for things in flight to complete. */
+	ast_debug(1, "Waiting for things to settle to unload.\n");
+	remaining = ast_serializer_shutdown_group_join(shutdown_group, MAX_UNLOAD_TIMEOUT_TIME);
+	if (remaining) {
+		/*
+		 * NOTE: We probably have a ref leak if the count
+		 * cannot reach zero after a few minutes of trying
+		 * to unload.
+		 */
+		ast_log(LOG_NOTICE, "Incomplete unload.  Try again later.  (%d remaining).\n",
+			remaining);
+		return -1;
+	}
+
+	ast_debug(1, "Successful shutdown.\n");
+
+	ao2_cleanup(shutdown_group);
+	shutdown_group = NULL;
+
 	return 0;
 }
 
@@ -1811,6 +1852,11 @@
 
 	CHECK_PJSIP_MODULE_LOADED();
 
+	shutdown_group = ast_serializer_shutdown_group_alloc();
+	if (!shutdown_group) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
+
 	/* Create outbound registration states container. */
 	new_states = ao2_container_alloc(DEFAULT_STATE_BUCKETS,
 		registration_state_hash, registration_state_cmp);

-- 
To view, visit https://gerrit.asterisk.org/691
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibb2fe558f98190f2a06da830e0fadfa25516f547
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Richard Mudgett <rmudgett at digium.com>



More information about the asterisk-code-review mailing list