[Python.NET] [PythonNet] 6/ 2 Resolve "Hang caused by multithreaded program that invokes .NET CLR"

Collector: Python for .NET Issue ... pythondotnet at python.org
Thu Mar 31 16:54:14 CEST 2005

Issue #6 Update (Resolve) "Hang caused by multithreaded program that invokes .NET CLR"
 Status Resolved, Threading/bug critical
To followup, visit:

= Resolve - Entry #2 by Brian on Mar 31, 2005 9:54 am

 Status: Pending => Resolved

This issue is resolved in beta 5 - thanks 
for the test case (I put a condensed version 
of it into the unit tests).

= Request - Entry #1 by Anonymous User on Mar 25, 2005 1:04 pm

Hang caused by multithreaded program accessing CLR methods. 

I successfully reproduced the state with a very simple application. Here is very dumb python program that uses the CLR:
from CLR.System import String
class cSharpCaller( object ):
    def __init__( self ):
        print "Created a cSharpCaller instance"
        self.__cSharpString = None
    def createCSharpString( self, str ):
        self.__cSharpString = String( str )
    def returnCSharpString( self ):
        return self.__cSharpString.ToString()

And if I test this with this (dumb) threethreaded testprogram:
import threading, thread, time
from cSharpCaller import cSharpCaller 
def start_threads(amount=5):
    for i in range(amount):
         thread = threading.Thread(target=process_thread )
         print "starting thread ", thread.getName()
def process_thread(  ):
    print "thread  started "
    for i in range( 2 ):
        print "Hi, I'm a thread"
        time.sleep( 1 )
    cs = cSharpCaller( )
    cs.createCSharpString( "created from a thread" )
    print "cs.returnCSharpString() = ", cs.returnCSharpString()
    print "thread %s ended"
start_threads( 3 )
I get this output:
starting thread  thread0
thread  started
Hi, I'm thread %s
starting thread  thread1
thread  started
Hi, I'm thread %s
starting thread  thread2
thread  started
Hi, I'm thread %s
Hi, I'm thread %s
Hi, I'm thread %s
Hi, I'm thread %s
Created a cSharpCaller instance
Created a cSharpCaller instance
cs.returnCSharpString() =
and I have to CTRL+Pause to get out of the hang. Clearly hangs on the first String.ToString() method call. 


More information about the PythonDotNet mailing list