]> granicus.if.org Git - python/commitdiff
Followup to r61011: Also avoid the reference cycle when the Thread's target
authorJeffrey Yasskin <jyasskin@gmail.com>
Sat, 23 Feb 2008 20:40:35 +0000 (20:40 +0000)
committerJeffrey Yasskin <jyasskin@gmail.com>
Sat, 23 Feb 2008 20:40:35 +0000 (20:40 +0000)
raises an exception.

Lib/test/test_threading.py
Lib/threading.py

index 91f5a8b2df665ceb40aaf2675b45b2f1ea9f6729..dc94603472b8ad23efc102f4472f8b00e6e8a064 100644 (file)
@@ -256,23 +256,31 @@ class ThreadTests(unittest.TestCase):
 
     def test_no_refcycle_through_target(self):
         class RunSelfFunction(object):
-            def __init__(self):
+            def __init__(self, should_raise):
                 # The links in this refcycle from Thread back to self
                 # should be cleaned up when the thread completes.
+                self.should_raise = should_raise
                 self.thread = threading.Thread(target=self._run,
                                                args=(self,),
                                                kwargs={'yet_another':self})
                 self.thread.start()
 
             def _run(self, other_ref, yet_another):
-                pass
+                if self.should_raise:
+                    raise SystemExit
 
-        cyclic_object = RunSelfFunction()
+        cyclic_object = RunSelfFunction(should_raise=False)
         weak_cyclic_object = weakref.ref(cyclic_object)
         cyclic_object.thread.join()
         del cyclic_object
         self.assertEquals(None, weak_cyclic_object())
 
+        raising_cyclic_object = RunSelfFunction(should_raise=True)
+        weak_raising_cyclic_object = weakref.ref(raising_cyclic_object)
+        raising_cyclic_object.thread.join()
+        del raising_cyclic_object
+        self.assertEquals(None, weak_raising_cyclic_object())
+
 
 class ThreadingExceptionTests(unittest.TestCase):
     # A RuntimeError should be raised if Thread.start() is called
index 2f472b47aa33cc854094385050cb8e4acd73afce..005d40c63ed1fc63ea8eca5e019a4a5692483ef0 100644 (file)
@@ -442,11 +442,13 @@ class Thread(_Verbose):
         _sleep(0.000001)    # 1 usec, to let the thread run (Solaris hack)
 
     def run(self):
-        if self.__target:
-            self.__target(*self.__args, **self.__kwargs)
-        # Avoid a refcycle if the thread is running a function with an
-        # argument that has a member that points to the thread.
-        del self.__target, self.__args, self.__kwargs
+        try:
+            if self.__target:
+                self.__target(*self.__args, **self.__kwargs)
+        finally:
+            # Avoid a refcycle if the thread is running a function with
+            # an argument that has a member that points to the thread.
+            del self.__target, self.__args, self.__kwargs
 
     def __bootstrap(self):
         # Wrapper around the real bootstrap code that ignores