--- freeradius-server-2.1.11/src/modules/rlm_perl/rlm_perl.c	2011-06-20 16:57:14.000000000 +0200
+++ freeradius-server-2.1.11-rlm_perl-hacked/src/modules/rlm_perl/rlm_perl.c	2011-11-29 14:28:23.000000000 +0100
@@ -77,6 +77,8 @@
 	char	*perl_flags;
 	PerlInterpreter *perl;
 	pthread_key_t	*thread_key;
+
+	pthread_mutex_t clone_mutex;
 } PERL_INST;
 /*
  *	A mapping of configuration file names to internal variables.
@@ -433,6 +435,8 @@
 	 */
 
 #ifdef USE_ITHREADS
+	pthread_mutex_init(&inst->clone_mutex, NULL);
+
 	inst->thread_key = rad_malloc(sizeof(*inst->thread_key));
 	memset(inst->thread_key,0,sizeof(*inst->thread_key));
 	
@@ -649,8 +653,10 @@
 	HV		*rad_request_hv;
 	HV		*rad_request_proxy_hv;
 	HV		*rad_request_proxy_reply_hv;
-
+	
 #ifdef USE_ITHREADS
+	pthread_mutex_lock(&inst->clone_mutex);
+
 	PerlInterpreter *interp;
 
 	interp = rlm_perl_clone(inst->perl,inst->thread_key);
@@ -658,9 +664,12 @@
 	  dTHXa(interp);
 	  PERL_SET_CONTEXT(interp);
 	}
+	
+	pthread_mutex_unlock(&inst->clone_mutex);
 #else
 	PERL_SET_CONTEXT(inst->perl);
 #endif
+
 	{
 	dSP;
 
@@ -967,6 +976,7 @@
 
 #ifdef USE_ITHREADS
 	rlm_perl_destruct(inst->perl);
+	pthread_mutex_destroy(&inst->clone_mutex);
 #else
 	perl_destruct(inst->perl);
 	perl_free(inst->perl);

