-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deadlock caused by local import in different thread. #2925
Comments
Why are you acquiring the import lock? |
You've provided none of the information requested in our contributing documentation.Sent from my Android device with K-9 Mail. Please excuse my brevity. |
I edited original post, and added some information. |
@guojh are you acquiring the lock manually in other code or are you positing that the behaviour you're seeing is caused by the import locks? |
@sigmavirus24 No, no manually acquired lock. The bash script I pasted is all. It seems that import lock is automatically acquired by CPython interpreter during import. Call stack of CPython 2.7 import should look like this (not verified): builtin___import__ PyImport_ImportModuleLevel _PyImport_AcquireLock |
Proof of CPython (2.7) import lock: #!/bin/bash
cat > test_module_2.py <<EOF
import threading
def do_import():
print('before import')
import json # <-- program will get stuck here
print('after import')
thread = threading.Thread(target=do_import)
thread.start()
thread.join()
EOF
python -c 'import test_module_2' |
Oh, I understand. The problem is that we fire an import not at import time, which can apparently cause a deadlock. It seems to me that this would be a bug in CPython, wouldn't it? If you can deadlock the interpreter, that sounds like a bug in the CPython import logic. |
@Lukasa It seems to have been resolved in CPython 3.3. (per-module import lock instead of global import lock) |
Proof of CPython (2.7) implicit import: #!/bin/bash
cat > test_module_3.py <<EOF
import threading
def do_encode():
print('before import')
u''.encode('does-not-exist') # <-- program will get stuck here due to implicit import
print('after import')
thread = threading.Thread(target=do_encode)
thread.start()
thread.join()
EOF
python2.7 -c 'import test_module_3' I think it's not easy to solve this on CPython 2.7. |
Well, I mean, no: we just need to not do any late imports ourselves. In this case, it just means restructuring the |
That said, @Lukasa, @guojh's point is that using That said, the other work around is to defer the work in the module such that it doesn't happen at import time. |
Is this bug still around? I think I just ran into this with Python 2.7.12 and requests 2.9.1 |
@julianfnunez I don't believe anyone has written something to fix delayed imports in Requests (which I'm not certain need sweeping changes) but the other problem is that there are other ways to trigger this outside of requests as well. Requests uses stdlib functionality that uses delayed imports and such as well. What makes you think this is affecting you? |
I'm trying to do something very similar to the code snippet posted in the first comment. requests.get is locking the thread. PS: I realize this is not necessarily a problem with requests. I was just wondering if you guys did a workaround since this bug was in "Planned". |
I just found this issue by chance on internet, and thought that it would be good to clarify what the problem really is. In python 2.7 "an import should not have the side effect of spawning a new thread and then waiting for that thread in any way" (source). That is why the examples in the previous comments fail, they execute |
In an effort to clean up the issue tracker to only have issues that are still relevant to the project we've done a quick pass and decided this issue may no longer be relevant for a variety of potential reasons, including:
If you think the issue should remain open, please comment so below or open a new issue and link back to the original issue. Again, thank you for opening the issue and for the discussion, it's much appreciated. |
requests version 2.8.1, installed from pip.
Code to reproduce:
traceback from stuck point (I modified requests code to print stack):
This should be caused by deadlocking on import lock (imp.acquire_lock, imp.release_lock, imp.lock_held).
Possible duplication of:
https://github.com/kennethreitz/requests/issues/2649
The text was updated successfully, but these errors were encountered: