Do python cleanup properly on thread exit
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 18 Jul 2013 12:16:30 +0000 (13:16 +0100)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 18 Jul 2013 13:14:14 +0000 (14:14 +0100)
src/modules/rlm_python/rlm_python.c

index 588cc41..1221464 100644 (file)
@@ -135,7 +135,7 @@ static struct {
 /*
  *     This allows us to initialise PyThreadState on a per thread basis
  */
-fr_thread_local_init(PyThreadState *, local_thread_state);     /* macro */
+fr_thread_local_setup(PyThreadState *, local_thread_state);    /* macro */
 
 
 /*
@@ -405,11 +405,10 @@ static int do_python(rlm_python_t *inst, REQUEST *request, PyObject *pFunc, char
 
 #ifdef HAVE_PTHREAD_H
        if (worker) {
+               PyThreadState *my_thread_state;
                gstate = PyGILState_Ensure();
-               PyThreadState *my_thread_state = fr_thread_local_get(local_thread_state);
+               my_thread_state = fr_thread_local_init(local_thread_state, do_python_cleanup);
                if (!my_thread_state) {
-                       pthread_key_t tls_destructor;
-
                        my_thread_state = PyThreadState_New(inst->main_thread_state->interp);
                        if (!my_thread_state) {
                                radlog(L_ERR, "Failed initialising local PyThreadState on first run");
@@ -420,21 +419,8 @@ static int do_python(rlm_python_t *inst, REQUEST *request, PyObject *pFunc, char
                        if (ret != 0) {
                                radlog(L_ERR, "Failed storing PyThreadState in TLS: %s", strerror(ret));
 
-                               goto tls_fail;
-                       }
-
-                       /*
-                        *      This should ensure do_python_cleanup gets called when the
-                        *      thread exits.
-                        */
-                       ret = pthread_key_create(&tls_destructor, do_python_cleanup);
-                       if (ret != 0) {
-                               radlog(L_ERR, "Failed setting PyThreadState destructor: %s", strerror(ret));
-
-                               tls_fail:
                                PyThreadState_Clear(my_thread_state);
                                PyThreadState_Delete(my_thread_state);
-
                                return RLM_MODULE_FAIL;
                        }
                }