This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
make S_ithread_run() call S_ithread_free() in main context
authorDave Mitchell <davem@fdisolutions.com>
Sun, 14 Jan 2007 23:58:49 +0000 (23:58 +0000)
committerDave Mitchell <davem@fdisolutions.com>
Sun, 14 Jan 2007 23:58:49 +0000 (23:58 +0000)
Fixes a race condition between detach clearing a thread's
interpreter, and S_ithread_run() freeing itself while
assuming that it's own interpreter still exists.

p4raw-id: //depot/perl@29810

ext/threads/threads.xs

index 4fc60db..80671ee 100755 (executable)
@@ -221,10 +221,6 @@ S_ithread_free(pTHX_ ithread *thread)
     }
 #endif
 
-    /* Call PerlMemShared_free() in the context of the "first" interpreter
-     * per http://www.nntp.perl.org/group/perl.perl5.porters/110772
-     */
-    aTHX = MY_POOL.main_thread.interp;
     PerlMemShared_free(thread);
 
     /* total_threads >= 1 is used to veto cleanup by the main thread,
@@ -539,6 +535,12 @@ S_ithread_run(void * arg)
         my_exit(exit_code);
     }
 
+    /* at this point the interpreter may have been freed, so call
+     * free in the context of of the 'main' interpreter. That can't have
+     * been freed, due to the veto_cleanup mechanism */
+
+    aTHX = MY_POOL.main_thread.interp;
+
     MUTEX_LOCK(&thread->mutex);
     S_ithread_free(aTHX_ thread); /* releases MUTEX */