[PATCH 10/10] rlm_python: 10-block-threads-around-python-c-api-calls.patch

Paul P Komkoff Jr i at stingr.net
Mon Jan 30 23:27:27 CET 2006


Block threads around python C api calls

---
commit cd221cd2a83d00df8ef7d0d05783766d0764aeee
tree e631d404351b6c224f67d4f52d4bdb028847ff56
parent bc293834b5669396a77ba07a20ca9f60c9c34050
author <stingray at boxster64.stingr.net> Sun, 29 Jan 2006 16:07:57 +0300
committer <stingray at boxster64.stingr.net> Sun, 29 Jan 2006 16:07:57 +0300

 src/modules/rlm_python/rlm_python.c |   26 +++++++++++++++++++++-----
 1 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/src/modules/rlm_python/rlm_python.c b/src/modules/rlm_python/rlm_python.c
--- a/src/modules/rlm_python/rlm_python.c
+++ b/src/modules/rlm_python/rlm_python.c
@@ -33,6 +33,11 @@
 #include "modules.h"
 #include "conffile.h"
 
+#define Pyx_BLOCK_THREADS       {PyGILState_STATE __gstate = PyGILState_Ensure();
+#define Pyx_UNBLOCK_THREADS     PyGILState_Release(__gstate);}
+
+
+
 static const char rcsid[] = "$Id: rlm_python.c,v 1.6 2004/02/26 19:04:34 aland Exp $";
 
 /*
@@ -188,6 +193,8 @@ static void python_error() {
     PyObject        *pStr1 = NULL;
     PyObject        *pStr2 = NULL;
 
+    Pyx_BLOCK_THREADS
+
     PyErr_Fetch(&pType, &pValue, &pTraceback);
     if (pType == NULL || pValue == NULL)
         goto failed;
@@ -201,6 +208,8 @@ failed:
     Py_XDECREF(pType);
     Py_XDECREF(pValue);
     Py_XDECREF(pTraceback);
+
+    Pyx_UNBLOCK_THREADS
 }
 
 static int python_init()
@@ -233,8 +242,11 @@ failed:
 }
 
 static int python_destroy() {
+    Pyx_BLOCK_THREADS
     Py_XDECREF(radiusd_module);
     Py_Finalize();
+    Pyx_UNBLOCK_THREADS
+
     return 0;
 }
 
@@ -324,6 +336,9 @@ static int python_function(REQUEST *requ
                 for (vp = request->packet->vps; vp; vp = vp->next)
                         tuplelen++;
         }
+
+        gstate = PyGILState_Ensure();
+
         if (tuplelen == 0) {
                 Py_INCREF(Py_None);
                 pArgs = Py_None;
@@ -355,12 +370,8 @@ static int python_function(REQUEST *requ
         }
 
         /* Call Python function. */
-	gstate = PyGILState_Ensure();
-
         pRet = PyObject_CallFunctionObjArgs(pFunc, pArgs, NULL);
 
-	PyGILState_Release(gstate);
-
 	if (pRet == NULL)
                 goto failed;
 
@@ -414,11 +425,14 @@ static int python_function(REQUEST *requ
 okay:
         Py_DECREF(pArgs);
         Py_DECREF(pRet);
+	PyGILState_Release(gstate);
         return ret;
 failed:
         python_error();
         Py_XDECREF(pArgs);
         Py_XDECREF(pRet);
+        PyGILState_Release(gstate);
+
         return -1;
 }
 
@@ -451,19 +465,21 @@ static int python_load_function(char *mo
 	PyGILState_Release(gstate);
         return 0;
 failed:
-	PyGILState_Release(gstate);
         python_error();
         radlog(L_ERR, "rlm_python:%s: failed to import python function '%s.%s'", funcname, module, func);
         Py_XDECREF(*pFunc);
         *pFunc = NULL;
         Py_XDECREF(*pModule);
+        PyGILState_Release(gstate);
         *pModule = NULL;
         return -1;
 }
 
 static void python_objclear(PyObject **ob) {
         if (*ob != NULL) {
+		Pyx_BLOCK_THREADS
                 Py_DECREF(*ob);
+                Pyx_UNBLOCK_THREADS
                 *ob = NULL;
         }
 }
-- 
Paul P 'Stingray' Komkoff Jr // http://stingr.net/key <- my pgp key
 This message represents the official view of the voices in my head



More information about the Freeradius-Devel mailing list