From: Tim Peters Date: Sat, 16 Feb 2002 07:26:27 +0000 (+0000) Subject: SF bug #516372: test_thread: unhandled exc. in thread X-Git-Tag: v2.3c1~6666 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=20882dd1742310c8cf6858354ecb150810adb95c;p=python SF bug #516372: test_thread: unhandled exc. in thread Fix exit races in test_thread.py and test_threaded_import.py. I suspect the bug is provokable only under Linux (where child threads seem to get lots of cycles before they get killed after the main thread exits), or on multi-processor machines running other OSes. Bugfix candidate. --- diff --git a/Lib/test/test_thread.py b/Lib/test/test_thread.py index 02da94e75d..a45fb2fd5b 100644 --- a/Lib/test/test_thread.py +++ b/Lib/test/test_thread.py @@ -97,10 +97,14 @@ def task2(ident): if verbose: print 'task', ident, 'leaving barrier', i mutex.acquire() - running = running - 1 - if running == 0: - done.release() + running -= 1 + # Must release mutex before releasing done, else the main thread can + # exit and set mutex to None as part of global teardown; then + # mutex.release() raises AttributeError. + finished = running == 0 mutex.release() + if finished: + done.release() print '\n*** Barrier Test ***' if done.acquire(0): diff --git a/Lib/test/test_threaded_import.py b/Lib/test/test_threaded_import.py index e022c5fe92..d9f3d70573 100644 --- a/Lib/test/test_threaded_import.py +++ b/Lib/test/test_threaded_import.py @@ -17,9 +17,13 @@ def task(): x = random.randrange(1, 3) critical_section.acquire() N -= 1 - if N == 0: - done.release() + # Must release critical_section before releasing done, else the main + # thread can exit and set critical_section to None as part of global + # teardown; then critical_section.release() raises AttributeError. + finished = N == 0 critical_section.release() + if finished: + done.release() # Tricky: When regrtest imports this module, the thread running regrtest # grabs the import lock and won't let go of it until this module returns.