[Freeswitch-svn] [commit] r5125 - freeswitch/branches/greenlizard/src/mod/languages/mod_python

Freeswitch SVN greenlizard at freeswitch.org
Thu May 10 14:13:26 EDT 2007


Author: greenlizard
Date: Thu May 10 14:13:25 2007
New Revision: 5125

Modified:
   freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch_python.cpp
   freeswitch/branches/greenlizard/src/mod/languages/mod_python/mod_python.c

Log:
bugfix attempt for http://jira.freeswitch.org/browse/MODLANG-15 and an unreported bug with 2 simultaneous callers dialing into python script that caused segfaults and Fatal Python error: ceval: tstate mix-up

Modified: freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch_python.cpp
==============================================================================
--- freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch_python.cpp	(original)
+++ freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch_python.cpp	Thu May 10 14:13:25 2007
@@ -9,8 +9,8 @@
     tts_name = NULL;
     voice_name = NULL;
 
-	if (session = switch_core_session_locate(uuid)) {
-		channel = switch_core_session_get_channel(session);
+       if (session = switch_core_session_locate(uuid)) {
+	        channel = switch_core_session_get_channel(session);
     }
 }
 
@@ -83,7 +83,10 @@
 		ap = &args;
     }
 
+    Py_BEGIN_ALLOW_THREADS
 	status = switch_ivr_play_file(session, NULL, file, ap);
+    Py_END_ALLOW_THREADS
+
     return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
 }
 
@@ -114,7 +117,10 @@
 		ap = &args;
     }
 
+    Py_BEGIN_ALLOW_THREADS
 	status = switch_ivr_speak_text(session, tts_name, voice_name, codec->implementation->samples_per_second, text, ap);
+    Py_END_ALLOW_THREADS
+
     return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
 }
 
@@ -129,7 +135,11 @@
 {
     switch_status_t status;
 	sanity_check(-1);
+
+    Py_BEGIN_ALLOW_THREADS
     status = switch_ivr_collect_digits_count(session, dtmf_buf,(uint32_t) len,(uint32_t) len, terminators, terminator, (uint32_t) timeout);
+    Py_END_ALLOW_THREADS
+
     return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
 }
 
@@ -137,7 +147,11 @@
 {
     switch_status_t status;
 	sanity_check(-1);
+
+    Py_BEGIN_ALLOW_THREADS
     status = switch_ivr_session_transfer(session, extension, dialplan, context);
+    Py_END_ALLOW_THREADS
+
     return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
 }
 
@@ -146,9 +160,12 @@
 {
     switch_status_t status;
 	sanity_check(-1);
+
     status = switch_play_and_get_digits( session, (uint32_t) min_digits,(uint32_t) max_digits,
             (uint32_t) max_tries, (uint32_t) timeout, 
             terminators, audio_files, bad_input_audio_files, dtmf_buf, 128, digits_regex);
+
+
     return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
 }
 

Modified: freeswitch/branches/greenlizard/src/mod/languages/mod_python/mod_python.c
==============================================================================
--- freeswitch/branches/greenlizard/src/mod/languages/mod_python/mod_python.c	(original)
+++ freeswitch/branches/greenlizard/src/mod/languages/mod_python/mod_python.c	Thu May 10 14:13:25 2007
@@ -25,6 +25,7 @@
  * 
  * Brian Fertig <brian.fertig at convergencetek.com>
  * Johny Kadarisman <jkr888 at gmail.com>
+ * Traun Leyden <tleyden at branchcut.com>
  *
  * mod_python.c -- Python Module
  *
@@ -43,6 +44,7 @@
 #include <switch.h>
 
 
+static PyThreadState *mainThreadState = NULL;
 
 void init_freeswitch(void);
 static switch_api_interface_t python_run_interface;
@@ -51,7 +53,7 @@
 
 static void eval_some_python(char *uuid, char *args)
 {
-	PyThreadState *tstate;
+	PyThreadState *tstate = NULL;
 	FILE *pythonfile = NULL;
 	char *dupargs = NULL;
 	char *argv[128] = {0};
@@ -97,16 +99,15 @@
 
 
 	if ((pythonfile = fopen(script_path, "r"))) {
-		tstate = Py_NewInterpreter();
 
+		PyEval_AcquireLock();
+		tstate = PyThreadState_New(mainThreadState->interp);
 		if (!tstate) {
 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error acquiring tstate\n");
 			goto done;
 		}
-		
-
-		PyThreadState_Clear(tstate);
-		init_freeswitch();
+		PyThreadState_Swap(tstate);
+		init_freeswitch(); 
 		PyRun_SimpleString("from freeswitch import *");
 		if (uuid) {
 			char code[128];
@@ -115,7 +116,11 @@
 		}
 		PySys_SetArgv(argc - lead, &argv[lead]);
 		PyRun_SimpleFile(pythonfile, script);
-		Py_EndInterpreter(tstate);
+		PyThreadState_Swap(NULL);
+		PyThreadState_Clear(tstate);
+		PyThreadState_Delete(tstate);
+		PyEval_ReleaseLock();
+
 		
 	} else {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error running %s\n", script_path);
@@ -134,7 +139,6 @@
 
 static void python_function(switch_core_session_t *session, char *data)
 {
-
 	eval_some_python(switch_core_session_get_uuid(session), (char *)data);
 	
 }
@@ -217,9 +221,6 @@
 	/*.directory_interface */ NULL
 };
 
-//static PyThreadState *gtstate;
-static PyThreadState *mainThreadState;
-
 SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_module_interface_t **module_interface, char *filename)
 {
 	/* connect my internal structure to the blank pointer passed to me */
@@ -231,10 +232,9 @@
 
 	mainThreadState = PyThreadState_Get();
 
-	PyEval_ReleaseLock();	
+	PyThreadState_Swap(NULL); 
 
-	eval_some_python(NULL, "init_python.py");
-	PyThreadState_Swap(NULL);
+	PyEval_ReleaseLock();	
 
 	/* indicate that the module should continue to be loaded */
 	return SWITCH_STATUS_SUCCESS;
@@ -244,19 +244,19 @@
   Called when the system shuts down*/
 SWITCH_MOD_DECLARE(switch_status_t) switch_module_shutdown(void)
 {
+    PyInterpreterState *mainInterpreterState;
+    PyThreadState *myThreadState;
 
-	PyInterpreterState *mainInterpreterState;
-	PyThreadState *myThreadState;
+    PyEval_AcquireLock();
+    mainInterpreterState = mainThreadState->interp;
+    myThreadState = PyThreadState_New(mainInterpreterState);
+    PyThreadState_Swap(myThreadState);
+    PyEval_ReleaseLock();
+
+    Py_Finalize();
+    PyEval_ReleaseLock();
+    return SWITCH_STATUS_SUCCESS;
 
-	PyEval_AcquireLock();
-	mainInterpreterState = mainThreadState->interp;
-	myThreadState = PyThreadState_New(mainInterpreterState);
-	PyThreadState_Swap(myThreadState);
-	PyEval_ReleaseLock();
-
-	Py_Finalize();
-	PyEval_ReleaseLock();
-	return SWITCH_STATUS_SUCCESS;
 }
 
 



More information about the Freeswitch-svn mailing list