pynacl: import nacl.bindings hangs
I ran into an issue with another library hanging on import that appears be caused by PyNaCl. I can reproduce the issue with
$ python -c 'import nacl.bindings'
Expected Behavior: command should complete in, at most, a few seconds. Actual Behavior: commands takes roughly 90 seconds to complete
Running strace on the command shows that the import is hanging here
open("/dev/random", O_RDONLY) = 4
poll([{fd=4, events=POLLIN}], 1, 4294967295
I found this closed issue and tried following the recommendation of running pip install pynacl -I --no-binary pynacl but the issue still occurs.
https://github.com/paramiko/paramiko/issues/1023
OS: Ubuntu 14.04.5 Python Version: 2.7.6
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 2
- Comments: 23 (10 by maintainers)
If it’s inherently insecure to not read /dev/random at startup, does that mean that OpenSSH is insecure? I can spawn dozens of ssh processes and none of them will hang, which implies that they’re not reading /dev/random.
If I try to spawn two Python processes that both import nacl.bindings, at least one of them will hang, since both will try to read /dev/random, and the first one to succeed will drain off all the entropy for the next 10 minutes or so on a Google Cloud VM instance. Starting dozens of Python processes that all import nacl.bindings is impossible.
By default libsodium only uses the
/dev/randompath to determine if the CSPRNG is initialized before it starts using/dev/urandom. It also prefers to usegetrandom, which gives the same behavior for free (blocking only until the CSPRNG is ready). On normal systems this happens extremely rapidly, but there exists a weird long tail of systems that can have bad entropy for long periods of time (especially small things like raspberry pi devices). We do not want to supply bad randomness as that is a major security problem, so blocking is the right decision.This obviously results in a bad user experience, but the underlying problem is not one pynacl/libsodium can fix, we can only wait.
We are facing this issue on a Linux VPS used for test, the app is restarted after each code modification, which triggers a new init call to pynacl/libsodium. Sometimes (~30% of the time) it takes 40-90 seconds to start, because this “pause” in libsodium start.
Here’s the point when we CTRL+C when this hangs. Note that the prompt shell is given back only after the hang ending, so this is a real blocking thing.
This is not happenning with “no-binary” and compiling from source with wheel. Why this hang doesn’t happen with the program from source? what is the difference between source and precompiled whl? Precompiled and re-compiled should behave the same, so maybe investing in the differences can lead to some clues about this issue.
This one fixed it for me: