From d74b12a1674c60d6d013cb736f9619f98f68aff1 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthony.minessale@gmail.com>
Date: Mon, 13 Oct 2008 19:26:28 +0000
Subject: [PATCH] add more profile shutdown code to compensate for bug in sofia
 lib on nua_destroy

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9997 d0543943-73ff-0310-b7d9-9358b9ac24b2
---
 src/mod/endpoints/mod_sofia/mod_sofia.c  |  2 +-
 src/mod/endpoints/mod_sofia/sofia.c      |  8 ++++++++
 src/mod/endpoints/mod_sofia/sofia_glue.c | 13 +++++++++++++
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.c b/src/mod/endpoints/mod_sofia/mod_sofia.c
index d633ea3e19..415e8f8292 100644
--- a/src/mod/endpoints/mod_sofia/mod_sofia.c
+++ b/src/mod/endpoints/mod_sofia/mod_sofia.c
@@ -1633,7 +1633,7 @@ static switch_status_t cmd_profile(char **argv, int argc, switch_stream_handle_t
 	}
 
 	if (!strcasecmp(argv[1], "stop") || !strcasecmp(argv[1], "restart")) {
-		int rsec = 3;
+		int rsec = 10;
 		int diff = (int) (switch_timestamp(NULL) - profile->started);
 		int remain = rsec - diff;
 		if (diff < rsec) {
diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c
index f14e61bd4a..4ee63ca1c8 100644
--- a/src/mod/endpoints/mod_sofia/sofia.c
+++ b/src/mod/endpoints/mod_sofia/sofia.c
@@ -635,6 +635,14 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void
 		su_root_step(profile->s_root, 1000);
 	}
 
+	sofia_clear_pflag_locked(profile, PFLAG_RUNNING);
+
+	switch_core_session_hupall_matching_var("sofia_profile_name", profile->name, SWITCH_CAUSE_MANAGER_REQUEST);
+	while (profile->inuse) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Waiting for %d session(s)\n", profile->inuse);
+		su_root_step(profile->s_root, 1000);
+	}
+
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Write lock %s\n", profile->name);
 	switch_thread_rwlock_wrlock(profile->rwlock);
 	sofia_reg_unregister(profile);
diff --git a/src/mod/endpoints/mod_sofia/sofia_glue.c b/src/mod/endpoints/mod_sofia/sofia_glue.c
index 91bba930ec..a8ed8fdf31 100644
--- a/src/mod/endpoints/mod_sofia/sofia_glue.c
+++ b/src/mod/endpoints/mod_sofia/sofia_glue.c
@@ -2708,6 +2708,19 @@ void sofia_glue_restart_all_profiles(void)
         for (hi = switch_hash_first(NULL, mod_sofia_globals.profile_hash); hi; hi = switch_hash_next(hi)) {
 			switch_hash_this(hi, &var, NULL, &val);
             if ((pptr = (sofia_profile_t *) val)) {
+				int rsec = 10;
+				int diff = (int) (switch_timestamp(NULL) - pptr->started);
+				int remain = rsec - diff;
+				if (sofia_test_pflag(pptr, PFLAG_RESPAWN) || !sofia_test_pflag(pptr, PFLAG_RUNNING)) {
+					continue;
+				}
+
+				if (diff < rsec) {
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
+									  "Profile %s must be up for at least %d seconds to stop/restart.\nPlease wait %d second%s\n",
+									  pptr->name, rsec, remain, remain == 1 ? "" : "s");
+					continue;
+				}
 				sofia_set_pflag_locked(pptr, PFLAG_RESPAWN);
 				sofia_clear_pflag_locked(pptr, PFLAG_RUNNING);
 			}