[issue30929] AttributeErrors after import in multithreaded environment

Evgeny Boytsov report at bugs.python.org
Fri Jul 14 08:05:10 EDT 2017


New submission from Evgeny Boytsov:

Hello everybody!

We are using Python 3.4 running at CentOS 7 x64 and experiencing some problems with simulatenous import of modules from different threads of execution.
The attached archive contains simple example, which demonstrates the issue. 

There is python package called "mod". It contains two sub-packages called "foo" and "bar". They, in turn, contain modules "foo" and "bar" respectively. 

Another package called "threader" contains function "run_threads()", which runs two threads of execution (using threading) and joins them.
First thread of execution imports "mod.foo.foo" from "mod" and calls its function. The second thread does the same, but for module "mod.bar.bar".

Finally, there is module "test.py" which calls "threader.run_threads()" and when it returns, tries to import "mod.foo.foo" and call its function.

To reproduce the bug you need to run "test.py" for several times.

Most of the launches complete successfully. But sometimes we encounter AttributeError during import like that:

'module' object has no attribute 'foo'


We've done some investigation in order to clarify this behaviour and added import hook (you can see it in test.py), which logs all import that take place. 
When the launch completes successfully, the order of imports looks like that:

$ python3 test.py 
[py][import] threader
[py][import] threading
[py][import] time
[py][import] traceback
[py][import] linecache
[py][import] tokenize
[py][import] collections
[py][import] _collections
[py][import] operator
[py][import] _operator
[py][import] keyword
[py][import] heapq
[py][import] itertools
[py][import] _heapq
[py][import] reprlib
[py][import] re
[py][import] sre_compile
[py][import] _sre
[py][import] sre_parse
[py][import] sre_constants
[py][import] _locale
[py][import] copyreg
[py][import] token
[py][import] mod
[py][import] mod.foo
[py][import] mod.bar
[py][import] mod.foo.foo
[py][import] mod.bar.bar
print from foo
print from bar

When the issue arises we see the following:

$ python3 test.py 
[py][import] threader
[py][import] threading
[py][import] time
[py][import] traceback
[py][import] linecache
[py][import] tokenize
[py][import] collections
[py][import] _collections
[py][import] operator
[py][import] _operator
[py][import] keyword
[py][import] heapq
[py][import] itertools
[py][import] _heapq
[py][import] reprlib
[py][import] re
[py][import] sre_compile
[py][import] _sre
[py][import] sre_parse
[py][import] sre_constants
[py][import] _locale
[py][import] copyreg
[py][import] token
[py][import] mod
[py][import] mod.foo
[py][import] mod
[py][import] mod.bar
[py][import] mod.foo.foo
[py][import] mod.bar.bar
thread of foo: import error 'module' object has no attribute 'foo'
print from bar


That is when the issue arises there are two imports of package "mod".

And the most confusing thing about this scenario is that even after completion of "run_threads()" interpreter can not import "mod.foo.foo" and gives the same AttributeErrors in for-loop inside test.py.

----------
components: Interpreter Core
files: mod.zip
messages: 298349
nosy: boytsovea
priority: normal
severity: normal
status: open
title: AttributeErrors after import in multithreaded environment
type: behavior
versions: Python 3.4
Added file: http://bugs.python.org/file47014/mod.zip

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30929>
_______________________________________


More information about the Python-bugs-list mailing list