This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author boytsovea
Recipients boytsovea
Date 2017-07-14.12:05:08
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1500033910.01.0.46976407762.issue30929@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2017-07-14 12:05:10boytsoveasetrecipients: + boytsovea
2017-07-14 12:05:10boytsoveasetmessageid: <1500033910.01.0.46976407762.issue30929@psf.upfronthosting.co.za>
2017-07-14 12:05:09boytsovealinkissue30929 messages
2017-07-14 12:05:08boytsoveacreate