Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 1 | # This is a variant of the very old (early 90's) file |
| 2 | # Demo/threads/bug.py. It simply provokes a number of threads into |
| 3 | # trying to import the same module "at the same time". |
| 4 | # There are no pleasant failure modes -- most likely is that Python |
| 5 | # complains several times about module random having no attribute |
| 6 | # randrange, and then Python hangs. |
| 7 | |
| 8 | import thread |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame^] | 9 | from test.test_support import verbose, TestSkipped, TestFailed |
Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 10 | |
| 11 | critical_section = thread.allocate_lock() |
| 12 | done = thread.allocate_lock() |
| 13 | |
| 14 | def task(): |
| 15 | global N, critical_section, done |
| 16 | import random |
| 17 | x = random.randrange(1, 3) |
| 18 | critical_section.acquire() |
| 19 | N -= 1 |
Tim Peters | 20882dd | 2002-02-16 07:26:27 +0000 | [diff] [blame] | 20 | # Must release critical_section before releasing done, else the main |
| 21 | # thread can exit and set critical_section to None as part of global |
| 22 | # teardown; then critical_section.release() raises AttributeError. |
| 23 | finished = N == 0 |
Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 24 | critical_section.release() |
Tim Peters | 20882dd | 2002-02-16 07:26:27 +0000 | [diff] [blame] | 25 | if finished: |
| 26 | done.release() |
Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 27 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame^] | 28 | def test_import_hangers(): |
| 29 | import sys |
| 30 | if verbose: |
| 31 | print "testing import hangers ...", |
| 32 | |
| 33 | from test import threaded_import_hangers |
| 34 | |
| 35 | try: |
| 36 | if threaded_import_hangers.errors: |
| 37 | raise TestFailed(threaded_import_hangers.errors) |
| 38 | elif verbose: |
| 39 | print "OK." |
| 40 | finally: |
| 41 | # In case this test is run again, make sure the helper module |
| 42 | # gets loaded from scratch again. |
| 43 | del sys.modules['test.threaded_import_hangers'] |
| 44 | |
Tim Peters | d974221 | 2001-05-22 18:28:25 +0000 | [diff] [blame] | 45 | # Tricky: When regrtest imports this module, the thread running regrtest |
| 46 | # grabs the import lock and won't let go of it until this module returns. |
| 47 | # All other threads attempting an import hang for the duration. Since |
| 48 | # this test spawns threads that do little *but* import, we can't do that |
| 49 | # successfully until after this module finishes importing and regrtest |
| 50 | # regains control. To make this work, a special case was added to |
| 51 | # regrtest to invoke a module's "test_main" function (if any) after |
| 52 | # importing it. |
Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 53 | |
Tim Peters | d974221 | 2001-05-22 18:28:25 +0000 | [diff] [blame] | 54 | def test_main(): # magic name! see above |
Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 55 | global N, done |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 56 | |
| 57 | import imp |
| 58 | if imp.lock_held(): |
| 59 | # This triggers on, e.g., from test import autotest. |
| 60 | raise TestSkipped("can't run when import lock is held") |
| 61 | |
Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 62 | done.acquire() |
Tim Peters | d974221 | 2001-05-22 18:28:25 +0000 | [diff] [blame] | 63 | for N in (20, 50) * 3: |
| 64 | if verbose: |
| 65 | print "Trying", N, "threads ...", |
Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 66 | for i in range(N): |
| 67 | thread.start_new_thread(task, ()) |
| 68 | done.acquire() |
Tim Peters | d974221 | 2001-05-22 18:28:25 +0000 | [diff] [blame] | 69 | if verbose: |
| 70 | print "OK." |
Michael W. Hudson | fcc09bb | 2004-08-03 10:45:59 +0000 | [diff] [blame] | 71 | done.release() |
Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 72 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame^] | 73 | test_import_hangers() |
| 74 | |
Tim Peters | d974221 | 2001-05-22 18:28:25 +0000 | [diff] [blame] | 75 | if __name__ == "__main__": |
| 76 | test_main() |