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
random.py/os.urandom robustness #41809
Comments
Python 2.4.1 now uses os.urandom() to seed the random os.urandom() will open /dev/urandom on demand, e.g. It is standard programming practice for a daemon My recommendation would be to make os.urandom() open |
Logged In: YES There are many modules that have a dependency on random, for |
Logged In: YES Just providing some feedback: I'm able to reproduce this. Importing random will cause Personally, I only clean up the file descriptors I have |
Logged In: YES To add robustness, it would be possible to catch read errors |
Logged In: YES Yeah, I was thinking the same thing. It doesn't address the |
Logged In: YES Unfortunately, catching exceptions is not sufficient - the As for not closing file descriptors you haven't opened As far as I can tell, os.urandom() is used mostly to seed One possible solution would be for os.py to offer a |
Logged In: YES The child is a copy of the parent. Therefore, if in the I suspect Stevens would make an exception to his guideline |
Logged In: YES We're facing this problem. We're thinking of patching our os.py BTW, here's the traceback we get. As you probably can guess, Traceback (most recent call last):
File "/opt/race/share/sw/common/bin/dpmd", line 27, in ?
dpmd().run()
File "Linux/CommandLineApp.py", line 336, in run
File "Linux/daemonbase.py", line 324, in main
File "Linux/server.py", line 61, in addServices
File "Linux/dpmd.py", line 293, in __init__
File "Linux/threadutils.py", line 44, in start
File "Linux/xmlrpcd.py", line 165, in createThread
File "Linux/threadutils.py", line 126, in __init__
File "/opt/race/share/sw/os/Linux_2.4_i686/python/lib/python2.4/t
empfile.py", line 423, in NamedTemporaryFile
dir = gettempdir()
File "/opt/race/share/sw/os/Linux_2.4_i686/python/lib/python2.4/t
empfile.py", line 262, in gettempdir
tempdir = _get_default_tempdir()
File "/opt/race/share/sw/os/Linux_2.4_i686/python/lib/python2.4/t
empfile.py", line 185, in _get_default_tempdir
namer = _RandomNameSequence()
File "/opt/race/share/sw/os/Linux_2.4_i686/python/lib/python2.4/t
empfile.py", line 121, in __init__
self.rng = _Random()
File "/opt/race/share/sw/os/Linux_2.4_i686/python/lib/python2.4/r
andom.py", line 96, in __init__
self.seed(x)
File "/opt/race/share/sw/os/Linux_2.4_i686/python/lib/python2.4/r
andom.py", line 110, in seed a = long(_hexlify(_urandom(16)), 16) File "/opt/race/share/sw/os/Linux_2.4_i686/python/lib/python2.4/ OSError : [Errno 9] Bad file descriptor |
Logged In: YES Modifying the system os.py is not a good idea. A better def close_fd():
# close all inherited file descriptors
start_fd = 3
# Python 2.4.1 and later use /dev/urandom to seed the
random module's RNG
# a file descriptor is kept internally as os._urandomfd
(created on demand
# the first time os.urandom() is called), and should not
be closed
try:
os.urandom(4)
urandom_fd = getattr(os, '_urandomfd', None)
except AttributeError:
urandom_fd = None
if '-close_fd' in sys.argv:
start_fd = int(sys.argv[sys.argv.index('-close_fd') + 1])
for fd in range(start_fd, 256):
if fd == urandom_fd:
continue
try:
os.close(fd)
except OSError:
pass |
Logged In: YES I recommend to close this as invalid. The daemonization code |
Logged In: YES Perhaps the best way to resolve this would be for the |
Logged In: YES Clearly broken? Hardly. Daemonization code is not the only place where it's recommend It's unreasonable to expect python programs to keep track of all Are there any other file descriptors we should know about? |
Logged In: YES Conversely, I would say that it's unreasonable to expect |
Logged In: YES I don't think so. One of the rules in libc, the standard C library, |
Logged In: YES Here's a reference: The relevant post: ============================================ On 25 Feb 2001 10:48:22 GMT Casper H.S. Dik - Network | Solaris at various times used a cached /dev/zero fd both for ) So the dilemma is that closing fds can cause problems and ====================================== If the python library had some module or global list of opened file from os import opened_fds And then it would be no problem to skip those when closing fds. Also, the proposed os.daemonize() function that knows about its Still, the most robust solution is not to cache open fds in the There are several solutions but closing this bug as invalid doesn't |
Logged In: YES We ran across this problem when we upgraded to 2.4. We use I would think, at the very least, if this were a file Either way, this bug is not invalid and needs to be addressed. My 2 cents.. |
Logged In: YES This bug is a major problem for us as well. This bug also subprocess.Popen(["ls"], close_fds=1, preexec_fn=lambda:
os.urandom(4)) I agree with lcaamano; the library should NOT cache a file Has anyone really been able to verify that the performance |
Logged In: YES Attaching a patch for Lib/os.py, fixing this on Unix. On Windows, a completely different method is used for Please review. |
Logged In: YES I'm on Windows so cannot be of much use on the patch review. |
Logged In: YES The patch is fine, please apply - both to the trunk and to 2.4. |
Logged In: YES Committed as Lib/os.py r1.87, r1.83.2.3. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: