mirror of https://github.com/python/cpython.git
Add an import lock test for multithreaded circular imports.
(part of #9657)
This commit is contained in:
parent
8d18907a2e
commit
0723d2c78d
|
@ -5,12 +5,15 @@
|
||||||
# complains several times about module random having no attribute
|
# complains several times about module random having no attribute
|
||||||
# randrange, and then Python hangs.
|
# randrange, and then Python hangs.
|
||||||
|
|
||||||
|
import os
|
||||||
import imp
|
import imp
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
import shutil
|
||||||
import unittest
|
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')
|
thread = import_module('_thread')
|
||||||
|
threading = import_module('threading')
|
||||||
|
|
||||||
def task(N, done, done_tasks, errors):
|
def task(N, done, done_tasks, errors):
|
||||||
try:
|
try:
|
||||||
|
@ -32,6 +35,25 @@ def task(N, done, done_tasks, errors):
|
||||||
if finished:
|
if finished:
|
||||||
done.release()
|
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:
|
class Finder:
|
||||||
"""A dummy finder to detect concurrent access to its find_module()
|
"""A dummy finder to detect concurrent access to its find_module()
|
||||||
|
@ -144,10 +166,46 @@ def test_import_hangers(self):
|
||||||
import test.threaded_import_hangers
|
import test.threaded_import_hangers
|
||||||
self.assertFalse(test.threaded_import_hangers.errors)
|
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():
|
def test_main():
|
||||||
run_unittest(ThreadedImportTests)
|
run_unittest(ThreadedImportTests)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
test_main()
|
test_main()
|
||||||
|
|
Loading…
Reference in New Issue