[Freeswitch-svn] [commit] r5140 - in freeswitch/trunk/src: . include mod/endpoints/mod_alsa mod/endpoints/mod_dingaling mod/endpoints/mod_iax mod/endpoints/mod_portaudio mod/endpoints/mod_sofia mod/endpoints/mod_wanpipe mod/endpoints/mod_woomera

Freeswitch SVN anthm at freeswitch.org
Thu May 10 20:27:55 EDT 2007


Author: anthm
Date: Thu May 10 20:27:55 2007
New Revision: 5140

Modified:
   freeswitch/trunk/src/include/switch_core.h
   freeswitch/trunk/src/mod/endpoints/mod_alsa/mod_alsa.c
   freeswitch/trunk/src/mod/endpoints/mod_dingaling/mod_dingaling.c
   freeswitch/trunk/src/mod/endpoints/mod_iax/mod_iax.c
   freeswitch/trunk/src/mod/endpoints/mod_portaudio/mod_portaudio.c
   freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h
   freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c
   freeswitch/trunk/src/mod/endpoints/mod_wanpipe/mod_wanpipe.c
   freeswitch/trunk/src/mod/endpoints/mod_woomera/mod_woomera.c
   freeswitch/trunk/src/switch_core_session.c

Log:
add some robustness to deal with runaway threads

Modified: freeswitch/trunk/src/include/switch_core.h
==============================================================================
--- freeswitch/trunk/src/include/switch_core.h	(original)
+++ freeswitch/trunk/src/include/switch_core.h	Thu May 10 20:27:55 2007
@@ -428,12 +428,15 @@
 SWITCH_DECLARE(switch_core_session_t *) switch_core_session_request(const switch_endpoint_interface_t
 																	*endpoint_interface, switch_memory_pool_t **pool);
 
+
+SWITCH_DECLARE(void) switch_core_session_perform_destroy(switch_core_session_t **session, const char *file, const char *func, int line);
+
 /*! 
   \brief Destroy a session and return the memory pool to the core
   \param session pointer to a pointer of the session to destroy
   \return
 */
-SWITCH_DECLARE(void) switch_core_session_destroy(switch_core_session_t **session);
+#define switch_core_session_destroy(session) switch_core_session_perform_destroy(session, __FILE__, __SWITCH_FUNC__, __LINE__)
 
 /*! 
   \brief Provide the total number of sessions
@@ -458,8 +461,9 @@
 /*! 
   \brief Launch the session thread (state machine) on a given session
   \param session the session to activate the state machine on
+  \return SWITCH_STATUS_SUCCESS if the thread was launched
 */
-SWITCH_DECLARE(void) switch_core_session_thread_launch(switch_core_session_t *session);
+SWITCH_DECLARE(switch_status_t) switch_core_session_thread_launch(switch_core_session_t *session);
 
 /*! 
   \brief Retrieve a pointer to the channel object associated with a given session

Modified: freeswitch/trunk/src/mod/endpoints/mod_alsa/mod_alsa.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_alsa/mod_alsa.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_alsa/mod_alsa.c	Thu May 10 20:27:55 2007
@@ -1504,9 +1504,15 @@
 			switch_set_flag_locked(tech_pvt, TFLAG_ANSWER);
 			switch_channel_mark_answered(channel);
 			switch_channel_set_state(channel, CS_INIT);
-			switch_core_session_thread_launch(tech_pvt->session);
-			add_pvt(tech_pvt, PA_MASTER);
-			stream->write_function(stream, "SUCCESS:%s:%s\n", tech_pvt->call_id, switch_core_session_get_uuid(tech_pvt->session));
+
+			if (switch_core_session_thread_launch(tech_pvt->session) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error spawning thread\n");
+				switch_core_session_destroy(&session);
+				stream->write_function(stream, "FAIL:Thread Error!\n");
+			} else {
+				add_pvt(tech_pvt, PA_MASTER);
+				stream->write_function(stream, "SUCCESS:%s:%s\n", tech_pvt->call_id, switch_core_session_get_uuid(tech_pvt->session));
+			}
 		} else {
 			switch_core_session_destroy(&session);
 			stream->write_function(stream, "FAIL:Device Error!\n");

Modified: freeswitch/trunk/src/mod/endpoints/mod_dingaling/mod_dingaling.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_dingaling/mod_dingaling.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_dingaling/mod_dingaling.c	Thu May 10 20:27:55 2007
@@ -2720,7 +2720,12 @@
 			tech_pvt->dlsession = dlsession;
 			switch_channel_set_name(channel, "DingaLing/new");
 			switch_channel_set_state(channel, CS_INIT);
-			switch_core_session_thread_launch(session);
+			if (switch_core_session_thread_launch(session) != SWITCH_STATUS_SUCCESS) {
+				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error spawning thread\n");
+				terminate_session(&session, __LINE__, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
+                status = LDL_STATUS_FALSE;
+                goto done;
+			}
 		} else {
 			status = LDL_STATUS_FALSE;
 			goto done;

Modified: freeswitch/trunk/src/mod/endpoints/mod_iax/mod_iax.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_iax/mod_iax.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_iax/mod_iax.c	Thu May 10 20:27:55 2007
@@ -1128,7 +1128,10 @@
 							iax_accept(tech_pvt->iax_session, tech_pvt->codec);
 							iax_ring_announce(tech_pvt->iax_session);
 							switch_channel_set_state(channel, CS_INIT);
-							switch_core_session_thread_launch(session);
+							if (switch_core_session_thread_launch(session) != SWITCH_STATUS_SUCCESS) {
+								switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error spawning thread\n");
+								switch_core_session_destroy(&session);
+							}
 						}
 					}
 				}

Modified: freeswitch/trunk/src/mod/endpoints/mod_portaudio/mod_portaudio.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_portaudio/mod_portaudio.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_portaudio/mod_portaudio.c	Thu May 10 20:27:55 2007
@@ -1694,9 +1694,14 @@
 			switch_set_flag_locked(tech_pvt, TFLAG_ANSWER);
 			switch_channel_mark_answered(channel);
 			switch_channel_set_state(channel, CS_INIT);
-			switch_core_session_thread_launch(tech_pvt->session);
-			add_pvt(tech_pvt, PA_MASTER);
-			stream->write_function(stream, "SUCCESS:%s:%s\n", tech_pvt->call_id, switch_core_session_get_uuid(tech_pvt->session));
+			if (switch_core_session_thread_launch(tech_pvt->session) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error spawning thread\n");
+				switch_core_session_destroy(&session);
+				stream->write_function(stream, "FAIL:Thread Error!\n");
+			} else {
+				add_pvt(tech_pvt, PA_MASTER);
+				stream->write_function(stream, "SUCCESS:%s:%s\n", tech_pvt->call_id, switch_core_session_get_uuid(tech_pvt->session));
+			}
 		} else {
 			switch_core_session_destroy(&session);
 			stream->write_function(stream, "FAIL:Device Error!\n");

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h	Thu May 10 20:27:55 2007
@@ -242,6 +242,7 @@
 	switch_thread_rwlock_t *rwlock;
 	switch_mutex_t *flag_mutex;
 	uint32_t inuse;
+	uint32_t soft_max;
 	time_t started;
 #ifdef SWITCH_HAVE_ODBC
 	char *odbc_dsn;

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c	Thu May 10 20:27:55 2007
@@ -1703,10 +1703,17 @@
 	const char *context = NULL;
 	char network_ip[80];
 	switch_event_t *v_event = NULL;
+	uint32_t sess_count = switch_core_session_count();
+	uint32_t sess_max = switch_core_session_limit(0);
+
+	if ((profile->soft_max && sess_count >= profile->soft_max) || sess_count >= sess_max) {
+		nua_respond(nh, 480, "Maximum Calls In Progress", SIPTAG_RETRY_AFTER_STR("300"), TAG_END());
+		return;
+	}
 
 	if (!sip || !sip->sip_request || !sip->sip_request->rq_method_name) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Received an invalid packet!\n");
-		nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
+		nua_respond(nh, SIP_503_SERVICE_UNAVAILABLE, TAG_END());
 		return;
 	}
 
@@ -1906,7 +1913,33 @@
 	switch_copy_string(tech_pvt->sofia_private->uuid, switch_core_session_get_uuid(session), sizeof(tech_pvt->sofia_private->uuid));
 	nua_handle_bind(nh, tech_pvt->sofia_private);
 	tech_pvt->nh = nh;
-	switch_core_session_thread_launch(session);
+
+	if (switch_core_session_thread_launch(session) == SWITCH_STATUS_SUCCESS) {
+		return;
+	}
+
+	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "LUKE: I'm hit, but not bad.\n");
+					  
+	switch_mutex_lock(profile->flag_mutex);
+
+	profile->soft_max = sess_count - 10;
+	switch_core_session_limit(profile->soft_max);
+	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "LUKE'S VOICE: Artoo, see what you can do with it. Hang on back there....\n"
+					  "Green laserfire moves past the beeping little robot as his head turns.  "
+					  "After a few beeps and a twist of his mechanical arm,\n"
+					  "Artoo reduces the max sessions to %d thus, saving the switch from certian doom.\n", 
+					  profile->soft_max);
+
+	switch_mutex_unlock(profile->flag_mutex);
+
+	if (tech_pvt->hash_key) {
+		switch_core_hash_delete(tech_pvt->profile->chat_hash, tech_pvt->hash_key);
+	}
+
+	nua_handle_bind(nh, NULL);
+	free(tech_pvt->sofia_private);
+	switch_core_session_destroy(&session);
+	nua_respond(nh, 480, "Maximum Calls In Progress", SIPTAG_RETRY_AFTER_STR("300"), TAG_END());
 }
 
 void sofia_handle_sip_i_options(int status,

Modified: freeswitch/trunk/src/mod/endpoints/mod_wanpipe/mod_wanpipe.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_wanpipe/mod_wanpipe.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_wanpipe/mod_wanpipe.c	Thu May 10 20:27:55 2007
@@ -1562,6 +1562,12 @@
 		switch_copy_string(chanmap->map[pevent->ring.channel], switch_core_session_get_uuid(session), sizeof(chanmap->map[pevent->ring.channel]));
 		
 		switch_channel_set_state(channel, CS_INIT);
+		if (switch_core_session_thread_launch(session) != SWITCH_STATUS_SUCCESS) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error spawning thread\n");
+			switch_core_session_destroy(&session);
+			ret = 0;
+			goto done;
+		}
 		switch_core_session_thread_launch(session);
 	} else {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot Create new Inbound Channel!\n");

Modified: freeswitch/trunk/src/mod/endpoints/mod_woomera/mod_woomera.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_woomera/mod_woomera.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_woomera/mod_woomera.c	Thu May 10 20:27:55 2007
@@ -1240,7 +1240,11 @@
 						break;
 					}
 					switch_channel_set_state(channel, CS_INIT);
-					switch_core_session_thread_launch(session);
+					if (switch_core_session_thread_launch(session) != SWITCH_STATUS_SUCCESS) {
+						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error spawning thread\n");
+						switch_core_session_destroy(&session);
+						break;
+					}
 				}
 			}
 		}

Modified: freeswitch/trunk/src/switch_core_session.c
==============================================================================
--- freeswitch/trunk/src/switch_core_session.c	(original)
+++ freeswitch/trunk/src/switch_core_session.c	Thu May 10 20:27:55 2007
@@ -651,13 +651,15 @@
 }
 
 
-SWITCH_DECLARE(void) switch_core_session_destroy(switch_core_session_t **session)
+SWITCH_DECLARE(void) switch_core_session_perform_destroy(switch_core_session_t **session, const char *file, const char *func, int line)
 {
 	switch_memory_pool_t *pool;
 	switch_event_t *event;
 
-	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Close Channel %s\n", switch_channel_get_name((*session)->channel));
-
+	switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_NOTICE, "Close Channel %s [%s]\n", 
+					  switch_channel_get_name((*session)->channel),
+					  switch_channel_state_name(switch_channel_get_state((*session)->channel)));
+	
 	switch_ivr_deactivate_unicast(*session);
 	
 	switch_scheduler_del_task_group((*session)->uuid_str);
@@ -711,7 +713,7 @@
 }
 
 
-SWITCH_DECLARE(void) switch_core_session_thread_launch(switch_core_session_t *session)
+SWITCH_DECLARE(switch_status_t) switch_core_session_thread_launch(switch_core_session_t *session)
 {
 	switch_thread_t *thread;
 	switch_threadattr_t *thd_attr;;
@@ -720,10 +722,16 @@
 
 	if (!session->thread_running) {
 		switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
-		if (switch_thread_create(&thread, thd_attr, switch_core_session_thread, session, session->pool) != SWITCH_STATUS_SUCCESS) {
-			switch_core_session_destroy(&session);
+		if (switch_thread_create(&thread, thd_attr, switch_core_session_thread, session, session->pool) == SWITCH_STATUS_SUCCESS) {
+			return SWITCH_STATUS_SUCCESS;
+		} else {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot create thread!\n");
 		}
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot double-launch thread!\n");
 	}
+
+	return SWITCH_STATUS_FALSE;
 }
 
 



More information about the Freeswitch-svn mailing list