Anonymous committed 30d2333

Prevent spurious leaks when running -R. There may be more
issues that crop up from time to time, but this change seems to have been
pretty stable (no spurious warnings) for about a week.

Other modules which use threads may require similar use of
threading_setup/threading_cleanup from test_support.

Comments (0)

Files changed (4)


     if sys.platform == 'linux2':
+    thread_info = test_support.threading_setup()
+    test_support.threading_cleanup(*thread_info)
 if __name__ == "__main__":


     if verbose:
         print 'doctest (%s) ... %d tests with zero failures' % (module.__name__, t)
     return f, t
+# Threading support to prevent reporting refleaks when running -R
+def threading_setup():
+    import threading
+    return len(threading._active), len(threading._limbo)
+def threading_cleanup(num_active, num_limbo):
+    import threading
+    import time
+    _MAX_COUNT = 10
+    count = 0
+    while len(threading._active) != num_active and count < _MAX_COUNT:
+        count += 1
+        time.sleep(0.1)
+    count = 0
+    while len(threading._limbo) != num_limbo and count < _MAX_COUNT:
+        count += 1
+        time.sleep(0.1)


 import thread # If this fails, we can't test this module
 import threading
-from test.test_support import TestFailed
+from test.test_support import TestFailed, threading_setup, threading_cleanup
 import StringIO
 from traceback import print_exc
 import tempfile
 def test_main():
     threads = []
+    thread_info = threading_setup()
     print "Creating"
     for i in range(NUM_THREADS):
     if errors:
         raise TestFailed(msg)
+    threading_cleanup(*thread_info)
 if __name__ == "__main__":
     import sys, getopt
 # Note: test_XXX (none currently) really leak, but are disabled
 # so we don't send spam.  Any test which really leaks should only 
 # be listed here if there are also test cases under Lib/test/leakers.
+LEAKY_TESTS="test_(XXX)"  # Currently no tests should report spurious leaks.
 # Skip these tests altogether when looking for leaks.  These tests
 # do not need to be stored above in LEAKY_TESTS too.