classification
Title: __import__ is not thread-safe on Python 3
Type: Stage:
Components: Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Martin Laus, Valentyn Tymofieiev, brett.cannon, eric.snow, gjb1002, ncoghlan, repalviglator, ryan.petrello
Priority: normal Keywords:

Created on 2019-11-21 19:11 by Valentyn Tymofieiev, last changed 2020-08-26 20:27 by repalviglator.

Files
File name Uploaded Description Edit
import_module_not_found.py Valentyn Tymofieiev, 2019-11-21 19:11
import_deadlock.py Valentyn Tymofieiev, 2019-11-21 19:13
issue38884.zip Valentyn Tymofieiev, 2019-11-21 22:30
Messages (13)
msg357198 - (view) Author: Valentyn Tymofieiev (Valentyn Tymofieiev) Date: 2019-11-21 19:11
Attached import_module_not_found.py consistently fails for me on Python 3.7.5 and earlier Python 3 versions that I have tried with 

  File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "import_module_not_found.py", line 8, in t2
    return __import__("tensorflow.estimator", level=0)
ModuleNotFoundError: No module named 'tensorflow.estimator

Threads in this example finish successfully if executed sequentially.

I have not tried higher versions of Python, but I cannot reproduce this on Python 2.7. 

Is this an expected behavior?

Thank you.
msg357199 - (view) Author: Valentyn Tymofieiev (Valentyn Tymofieiev) Date: 2019-11-21 19:13
Attached import_deadlock.py fails with   

...
File "<frozen importlib._bootstrap>", line 980, in _find_and_load
  File "<frozen importlib._bootstrap>", line 149, in __enter__
  File "<frozen importlib._bootstrap>", line 94, in acquire
_frozen_importlib._DeadlockError: deadlock detected by _ModuleLock('tensorflow_transform.tf_metadata.metadata_io')

under the same conditions.
msg357202 - (view) Author: Valentyn Tymofieiev (Valentyn Tymofieiev) Date: 2019-11-21 19:31
Possibly related: https://bugs.python.org/issue35943
msg357214 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2019-11-21 20:20
Can you test this with a package that isn't tensorflow (e.g. something else in the stdlib or something created manually; basically a smaller reproducer)? It's hard to diagnose if this is really Python or not without a smaller reproducer as tensorflow might be doing some things behind the scenes that would break this due to it being an extension module.
msg357227 - (view) Author: Valentyn Tymofieiev (Valentyn Tymofieiev) Date: 2019-11-21 22:30
Attaching a minimal repro with simple project hierarchy.
msg357228 - (view) Author: Valentyn Tymofieiev (Valentyn Tymofieiev) Date: 2019-11-21 22:31
Repro: unpack issue38884.zip
python input_deadlock.py

(fails on Python 3.7, but not on 2.7).
msg357230 - (view) Author: Valentyn Tymofieiev (Valentyn Tymofieiev) Date: 2019-11-21 22:33
input_deadlock.py in issue38884.zip has a left-over comment:
# Requires pip install tensorflow==2.0.0 tensorflow-transform==0.15.0 
Please ignore that.
msg357234 - (view) Author: Valentyn Tymofieiev (Valentyn Tymofieiev) Date: 2019-11-21 23:01
The behavior changes between Python 3.2 (no deadlock) and Python 3.3 (deadlock). Deadlock also reproducible on Python 3.8.
msg357464 - (view) Author: Valentyn Tymofieiev (Valentyn Tymofieiev) Date: 2019-11-25 22:49
@brett.cannon, looks like I inadvertently dropped you from nosy list, so in case you missed the updates - please take a look at issue38884.zip. Thank you.
msg360068 - (view) Author: Valentyn Tymofieiev (Valentyn Tymofieiev) Date: 2020-01-15 17:28
Just checking - is issue38884.zip an acceptable repro for this issue?
Thanks.
msg360069 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2020-01-15 18:44
@valentyn just assume it's good enough unless some asks otherwise. People are probably too busy right now to be looking at this and so you might be waiting a while for a more definitive answer.
msg362746 - (view) Author: Ryan Petrello (ryan.petrello) Date: 2020-02-26 22:09
I believe I'm also encountering some version of this bug while importing code from the kombu library within a multi-threaded context.

For what it's worth, I'm able to reproduce the reported deadlock (https://bugs.python.org/file48737/issue38884.zip) using python3.8 on RHEL8 and CentOS 8 builds.

More details about what I'm encountering here: https://github.com/ansible/awx/issues/5617
https://github.com/ansible/awx/issues/5617#issuecomment-591618205

My intended workaround for now is to just not use a thread (we'll see if it helps):

https://github.com/ansible/awx/pull/6093
msg375958 - (view) Author: Kyle Mulka (repalviglator) Date: 2020-08-26 20:27
Was able to reproduce with Python 3.9.0rc1 using issue38884.zip. macOS 10.15.5
History
Date User Action Args
2020-08-26 20:27:52repalviglatorsetmessages: + msg375958
2020-08-26 20:22:18repalviglatorsetnosy: + repalviglator

versions: + Python 3.9
2020-04-17 14:48:34Martin Laussetnosy: + Martin Laus
2020-03-31 11:12:19gjb1002setnosy: + gjb1002
2020-02-26 22:09:39ryan.petrellosetnosy: + ryan.petrello
messages: + msg362746
2020-01-15 18:44:24brett.cannonsetmessages: + msg360069
2020-01-15 17:28:51Valentyn Tymofieievsetmessages: + msg360068
2019-11-25 22:49:12Valentyn Tymofieievsetmessages: + msg357464
2019-11-25 22:43:41Valentyn Tymofieievsetnosy: + brett.cannon, ncoghlan, eric.snow
2019-11-25 22:42:20Valentyn Tymofieievsetnosy: + Valentyn Tymofieiev, - brett.cannon
2019-11-25 22:42:00Valentyn Tymofieievsetnosy: + brett.cannon, - Valentyn Tymofieiev
2019-11-22 03:08:51Valentyn Tymofieievsetversions: + Python 3.8
2019-11-21 23:01:41Valentyn Tymofieievsetmessages: + msg357234
2019-11-21 22:33:02Valentyn Tymofieievsetmessages: + msg357230
2019-11-21 22:31:34Valentyn Tymofieievsetmessages: + msg357228
2019-11-21 22:30:19Valentyn Tymofieievsetfiles: + issue38884.zip
nosy: - brett.cannon, ncoghlan, eric.snow
messages: + msg357227

2019-11-21 20:20:18brett.cannonsetmessages: + msg357214
2019-11-21 20:11:53brett.cannonsetnosy: + brett.cannon, ncoghlan, eric.snow
2019-11-21 19:31:56Valentyn Tymofieievsetmessages: + msg357202
2019-11-21 19:13:24Valentyn Tymofieievsetfiles: + import_deadlock.py

messages: + msg357199
2019-11-21 19:11:54Valentyn Tymofieievcreate