Browse Source

stdpy: fix direct.stdpy.threading cleanup issue after thread runs

Fixes: #164
rdb 8 years ago
parent
commit
1dc02f6a28
1 changed files with 11 additions and 4 deletions
  1. 11 4
      direct/src/stdpy/threading.py

+ 11 - 4
direct/src/stdpy/threading.py

@@ -102,7 +102,14 @@ class Thread(ThreadBase):
         self.__dict__['daemon'] = current.daemon
         self.__dict__['name'] = name
 
-        self.__thread = core.PythonThread(self.run, None, name, name)
+        def call_run():
+            # As soon as the thread is done, break the circular reference.
+            try:
+                self.run()
+            finally:
+                self.__thread = None
+
+        self.__thread = core.PythonThread(call_run, None, name, name)
         threadId = _thread._add_thread(self.__thread, weakref.proxy(self))
         self.__dict__['ident'] = threadId
 
@@ -113,12 +120,12 @@ class Thread(ThreadBase):
             _thread._remove_thread_id(self.ident)
 
     def is_alive(self):
-        return self.__thread.isStarted()
+        return self.__thread is not None and self.__thread.is_started()
 
     isAlive = is_alive
 
     def start(self):
-        if self.__thread.isStarted():
+        if self.__thread is None or self.__thread.is_started():
             raise RuntimeError
 
         if not self.__thread.start(core.TPNormal, True):
@@ -393,7 +400,7 @@ def enumerate():
     _thread._threadsLock.acquire()
     try:
         for thread, locals, wrapper in list(_thread._threads.values()):
-            if wrapper and thread.isStarted():
+            if wrapper and wrapper.is_alive():
                 tlist.append(wrapper)
         return tlist
     finally: