diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-08-22 20:43:26 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-08-22 20:43:26 (GMT) |
commit | 0723d2c78d077232d3d019c78e397f5fcb7a159b (patch) | |
tree | 6039e70ae655d99ffc17397287fcfa788f710f21 /Lib/test/test_threaded_import.py | |
parent | 8d18907a2e4b821774459d6eeeb180eed1184ec1 (diff) | |
download | cpython-0723d2c78d077232d3d019c78e397f5fcb7a159b.zip cpython-0723d2c78d077232d3d019c78e397f5fcb7a159b.tar.gz cpython-0723d2c78d077232d3d019c78e397f5fcb7a159b.tar.bz2 |
Add an import lock test for multithreaded circular imports.
(part of #9657)
Diffstat (limited to 'Lib/test/test_threaded_import.py')
-rw-r--r-- | Lib/test/test_threaded_import.py | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/Lib/test/test_threaded_import.py b/Lib/test/test_threaded_import.py index 8727150..8884fa8 100644 --- a/Lib/test/test_threaded_import.py +++ b/Lib/test/test_threaded_import.py @@ -5,12 +5,15 @@ # complains several times about module random having no attribute # randrange, and then Python hangs. +import os import imp import sys import time +import shutil import unittest -from test.support import verbose, TestFailed, import_module, run_unittest +from test.support import verbose, import_module, run_unittest, TESTFN thread = import_module('_thread') +threading = import_module('threading') def task(N, done, done_tasks, errors): try: @@ -32,6 +35,25 @@ def task(N, done, done_tasks, errors): if finished: done.release() +# Create a circular import structure: A -> C -> B -> D -> A +# NOTE: `time` is already loaded and therefore doesn't threaten to deadlock. + +circular_imports_modules = { + 'A': """if 1: + import time + time.sleep(%(delay)s) + x = 'a' + import C + """, + 'B': """if 1: + import time + time.sleep(%(delay)s) + x = 'b' + import D + """, + 'C': """import B""", + 'D': """import A""", +} class Finder: """A dummy finder to detect concurrent access to its find_module() @@ -144,10 +166,46 @@ class ThreadedImportTests(unittest.TestCase): import test.threaded_import_hangers self.assertFalse(test.threaded_import_hangers.errors) + def test_circular_imports(self): + # The goal of this test is to exercise implementations of the import + # lock which use a per-module lock, rather than a global lock. + # In these implementations, there is a possible deadlock with + # circular imports, for example: + # - thread 1 imports A (grabbing the lock for A) which imports B + # - thread 2 imports B (grabbing the lock for B) which imports A + # Such implementations should be able to detect such situations and + # resolve them one way or the other, without freezing. + # NOTE: our test constructs a slightly less trivial import cycle, + # in order to better stress the deadlock avoidance mechanism. + delay = 0.5 + os.mkdir(TESTFN) + self.addCleanup(shutil.rmtree, TESTFN) + sys.path.insert(0, TESTFN) + self.addCleanup(sys.path.remove, TESTFN) + for name, contents in circular_imports_modules.items(): + contents = contents % {'delay': delay} + with open(os.path.join(TESTFN, name + ".py"), "wb") as f: + f.write(contents.encode('utf-8')) + self.addCleanup(sys.modules.pop, name, None) + + results = [] + def import_ab(): + import A + results.append(getattr(A, 'x', None)) + def import_ba(): + import B + results.append(getattr(B, 'x', None)) + t1 = threading.Thread(target=import_ab) + t2 = threading.Thread(target=import_ba) + t1.start() + t2.start() + t1.join() + t2.join() + self.assertEqual(set(results), {'a', 'b'}) + def test_main(): run_unittest(ThreadedImportTests) - if __name__ == "__main__": test_main() |