You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
importlib: python -m test test_pkg fails semi-randomly #78381
Traceback (most recent call last):
File "/home/cpython/Lib/test/test_pkg.py", line 180, in test_4self.run_code(s)
File "/home/cpython/Lib/test/test_pkg.py", line 69, in run_codeexec(textwrap.dedent(code), globals(), {"self": self})
File "<string>", line 2, in <module>
File "/tmp/tmpfxqne6o1/t4.py", line 1, in <module>RuntimeError: Shouldnt load t4.py
and I do agree that it is somewhat random. while i have some situations where I can reproduce this, i have others where I can rerun exactly the same binary and single process single thread process running just test_pkg.py and nothing else where the behavior differs between runs in terms of which of test_4 and test_7 fail or not.
I can make the test reliable... but I wouldn't say I fully understand the ultimate cause of the problem.
The reliability fix for test_pkg is to stop using test.support.modules_setup() and test.support.modules_cleanup() in the setUp() and tearDown() methods.
these test support functions are semi scary. they attempt to backup and replace sys.modules contents with special case code in the cleanup function to try and avoid doing that to things that are still necessary.
running python -vvv when I could get test_pkg to fail led me looking at code paths that were being executed mid-test that i'd expect to be executed only on process startup. locale.getpreferredencoding triggering a _bootlocale import, etc. I don't understand why it cause the problem though.
Did you attempt to use the 3-line change I posted earlier? I stepped through to test line-by-line to find the offending piece of code. And it was indeed the open() call causing the test-tree to be processed prior to it being completed. Thus making the .py file seen before the directory of same name would exist.
this is also present in 3.5, but it'll be up to the 3.5 release manager to cherry pick the test_pkg.py reliability fix into the 3.5 branch if it impacts them during the release process.
I still feel like there is an underlying issue within the import system that use of test.support.modules_cleanup somehow triggers that I never managed to really understand.
but the particular issue in this bug that people were seeing frequently is fixed.
Activity
vstinner commentedon Jul 23, 2018
vstinner@WIN C:\vstinner\python\master>python -X utf8 -m test -v test_pkg
First differing element 6:
'__spec__'
'__path__'
Second list contains 1 additional elements.
First extra element 7:
'__spec__'
['__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
+ '__path__',
'__spec__']
----------------------------------------------------------------------
vstinner commentedon Jul 23, 2018
I'm able to reproduce the bug on Linux as well. I able to reproduce the bug on 3.7 and master, but the bug seems random. Example on Linux with master:
vstinner@apu$ ./python -X utf8 -m test test_pkg
Run tests sequentially
0:00:00 load avg: 1.30 [1/1] test_pkg
== Tests result: SUCCESS ==
1 test OK.
Total duration: 73 ms
Tests result: SUCCESS
vstinner@apu$ ./python -X utf8 -m test test_pkg
Run tests sequentially
0:00:00 load avg: 1.35 [1/1] test_pkg
== Tests result: SUCCESS ==
1 test OK.
First differing element 6:
'__spec__'
'__path__'
Second list contains 1 additional elements.
First extra element 7:
'__spec__'
['__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
+ '__path__',
'__spec__']
test_pkg failed
== Tests result: FAILURE ==
1 test failed:
test_pkg
Total duration: 74 ms
Tests result: FAILURE
[-][Windows] python -X utf8 -m test test_pkg fails[/-][+]python -X utf8 -m test test_pkg fails[/+][-]python -X utf8 -m test test_pkg fails[/-][+]importlib: python -m test test_pkg -m test_7 fails randomly[/+]vstinner commentedon Jul 23, 2018
The test creates the following files:
/tmp/tmpsobc8gzf/t7.py
/tmp/tmpsobc8gzf/t7
/tmp/tmpsobc8gzf/t7/init.py
/tmp/tmpsobc8gzf/t7/sub.py
/tmp/tmpsobc8gzf/t7/sub
/tmp/tmpsobc8gzf/t7/sub/init.py
/tmp/tmpsobc8gzf/t7/sub/.py
/tmp/tmpsobc8gzf/t7/sub/subsub
/tmp/tmpsobc8gzf/t7/sub/subsub/init.py
/tmp/tmpsobc8gzf contains t7.py file and t7/ subdirectory.
The test pass when the directory is loaded:
vstinner@apu$ ./python -m test test_pkg -m test_7
Run tests sequentially
0:00:00 load avg: 0.96 [1/1] test_pkg
MODULE <module 't7' from '/tmp/tmp4f6zuep7/t7/init.py'>
== Tests result: SUCCESS ==
1 test OK.
Total duration: 51 ms
Tests result: SUCCESS
But fail when it gets the t7.py file:
First differing element 6:
'__spec__'
'__path__'
Second list contains 1 additional elements.
First extra element 7:
'__spec__'
['__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
+ '__path__',
'__spec__']
test_pkg failed
== Tests result: FAILURE ==
1 test failed:
test_pkg
Total duration: 49 ms
Tests result: FAILURE
vstinner commentedon Jul 23, 2018
I'm also able to reproduce the bug on Python 3.6.
Note: -X utf8 option is unrelated to this issue.
tirkarthi commentedon Jul 23, 2018
Almost always fails 8/10 times.
cpython git:(master) $ ./python -m unittest -v test.test_pkg
test_1 (test.test_pkg.TestPkg) ... ok
test_2 (test.test_pkg.TestPkg) ... ok
test_3 (test.test_pkg.TestPkg) ... ok
test_4 (test.test_pkg.TestPkg) ... ERROR
test_5 (test.test_pkg.TestPkg) ... ok
test_6 (test.test_pkg.TestPkg) ... ok
test_7 (test.test_pkg.TestPkg) ... FAIL
test_8 (test.test_pkg.TestPkg) ... ok
======================================================================
ERROR: test_4 (test.test_pkg.TestPkg)
----------------------------------------------------------------------
======================================================================
FAIL: test_7 (test.test_pkg.TestPkg)
----------------------------------------------------------------------
First differing element 6:
'__spec__'
'__path__'
Second list contains 1 additional elements.
First extra element 7:
'__spec__'
['__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
+ '__path__',
'__spec__']
----------------------------------------------------------------------
Ran 8 tests in 0.040s
FAILED (failures=1, errors=1)
vstinner commentedon Jul 23, 2018
I removed Windows experts from the nosy list, since the issue is related to importlib, no Windows.
brettcannon commentedon Aug 17, 2018
I can't reproduce with master on macOS.
vladima commentedon Aug 27, 2018
I've tried to repro this on Mac, Windows box and Windows VM - works fine for all cases.
17 remaining items
gpshead commentedon Sep 12, 2018
and I do agree that it is somewhat random. while i have some situations where I can reproduce this, i have others where I can rerun exactly the same binary and single process single thread process running just test_pkg.py and nothing else where the behavior differs between runs in terms of which of test_4 and test_7 fail or not.
[-]importlib: python -m test test_pkg fails[/-][+]importlib: python -m test test_pkg fails semi-randomly[/+]gpshead commentedon Sep 12, 2018
kinda poking at ideas here, but from a lunch conversation could this be related to the filesystem iteration order within the temp directories.
assigned to me while i investigate possibilities.
gpshead commentedon Sep 12, 2018
I can make the test reliable... but I wouldn't say I fully understand the ultimate cause of the problem.
The reliability fix for test_pkg is to stop using test.support.modules_setup() and test.support.modules_cleanup() in the setUp() and tearDown() methods.
these test support functions are semi scary. they attempt to backup and replace sys.modules contents with special case code in the cleanup function to try and avoid doing that to things that are still necessary.
running python -vvv when I could get test_pkg to fail led me looking at code paths that were being executed mid-test that i'd expect to be executed only on process startup. locale.getpreferredencoding triggering a _bootlocale import, etc. I don't understand why it cause the problem though.
PR to at least make the test reliable coming.
jkloth commentedon Sep 13, 2018
Did you attempt to use the 3-line change I posted earlier? I stepped through to test line-by-line to find the offending piece of code. And it was indeed the open() call causing the test-tree to be processed prior to it being completed. Thus making the .py file seen before the directory of same name would exist.
gpshead commentedon Sep 13, 2018
this is also present in 3.5, but it'll be up to the 3.5 release manager to cherry pick the test_pkg.py reliability fix into the 3.5 branch if it impacts them during the release process.
miss-islington commentedon Sep 13, 2018
New changeset 4ae8ece by Miss Islington (bot) (Gregory P. Smith) in branch 'master':
bpo-34200: Fix non-determinism of test_pkg (GH-9248)
4ae8ece
miss-islington commentedon Sep 13, 2018
New changeset 90f7d45 by Miss Islington (bot) in branch '3.7':
bpo-34200: Fix non-determinism of test_pkg (GH-9248)
90f7d45
miss-islington commentedon Sep 13, 2018
New changeset 3e3d4a4 by Miss Islington (bot) in branch '3.6':
bpo-34200: Fix non-determinism of test_pkg (GH-9248)
3e3d4a4
gpshead commentedon Sep 13, 2018
I still feel like there is an underlying issue within the import system that use of test.support.modules_cleanup somehow triggers that I never managed to really understand.
but the particular issue in this bug that people were seeing frequently is fixed.
vstinner commentedon Sep 13, 2018
Thank you for the fix Gregory!