Commit 4879a963 authored by Antoine Pitrou's avatar Antoine Pitrou

Issue #18756: os.urandom() now uses a lazily-opened persistent file...

Issue #18756: os.urandom() now uses a lazily-opened persistent file descriptor, so as to avoid using many file descriptors when run in parallel from multiple threads.
parent 267964c8
...@@ -246,6 +246,7 @@ PyAPI_FUNC(void) _PyGC_DumpShutdownStats(void); ...@@ -246,6 +246,7 @@ PyAPI_FUNC(void) _PyGC_DumpShutdownStats(void);
PyAPI_FUNC(void) _PyGC_Fini(void); PyAPI_FUNC(void) _PyGC_Fini(void);
PyAPI_FUNC(void) PySlice_Fini(void); PyAPI_FUNC(void) PySlice_Fini(void);
PyAPI_FUNC(void) _PyType_Fini(void); PyAPI_FUNC(void) _PyType_Fini(void);
PyAPI_FUNC(void) _PyRandom_Fini(void);
PyAPI_DATA(PyThreadState *) _Py_Finalizing; PyAPI_DATA(PyThreadState *) _Py_Finalizing;
#endif #endif
......
...@@ -51,6 +51,10 @@ Core and Builtins ...@@ -51,6 +51,10 @@ Core and Builtins
Library Library
------- -------
- Issue #18756: os.urandom() now uses a lazily-opened persistent file
descriptor, so as to avoid using many file descriptors when run in
parallel from multiple threads.
- Issue #18418: After fork(), reinit all threads states, not only active ones. - Issue #18418: After fork(), reinit all threads states, not only active ones.
Patch by A. Jesse Jiryu Davis. Patch by A. Jesse Jiryu Davis.
......
...@@ -625,6 +625,7 @@ Py_Finalize(void) ...@@ -625,6 +625,7 @@ Py_Finalize(void)
PyDict_Fini(); PyDict_Fini();
PySlice_Fini(); PySlice_Fini();
_PyGC_Fini(); _PyGC_Fini();
_PyRandom_Fini();
/* Cleanup Unicode implementation */ /* Cleanup Unicode implementation */
_PyUnicode_Fini(); _PyUnicode_Fini();
......
...@@ -90,6 +90,7 @@ vms_urandom(unsigned char *buffer, Py_ssize_t size, int raise) ...@@ -90,6 +90,7 @@ vms_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
#if !defined(MS_WINDOWS) && !defined(__VMS) #if !defined(MS_WINDOWS) && !defined(__VMS)
static int urandom_fd = -1;
/* Read size bytes from /dev/urandom into buffer. /* Read size bytes from /dev/urandom into buffer.
Call Py_FatalError() on error. */ Call Py_FatalError() on error. */
...@@ -133,18 +134,30 @@ dev_urandom_python(char *buffer, Py_ssize_t size) ...@@ -133,18 +134,30 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
if (size <= 0) if (size <= 0)
return 0; return 0;
Py_BEGIN_ALLOW_THREADS if (urandom_fd >= 0)
fd = _Py_open("/dev/urandom", O_RDONLY); fd = urandom_fd;
Py_END_ALLOW_THREADS else {
if (fd < 0) Py_BEGIN_ALLOW_THREADS
{ fd = _Py_open("/dev/urandom", O_RDONLY);
if (errno == ENOENT || errno == ENXIO || Py_END_ALLOW_THREADS
errno == ENODEV || errno == EACCES) if (fd < 0)
PyErr_SetString(PyExc_NotImplementedError, {
"/dev/urandom (or equivalent) not found"); if (errno == ENOENT || errno == ENXIO ||
errno == ENODEV || errno == EACCES)
PyErr_SetString(PyExc_NotImplementedError,
"/dev/urandom (or equivalent) not found");
else
PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
if (urandom_fd >= 0) {
/* urandom_fd was initialized by another thread while we were
not holding the GIL, keep it. */
close(fd);
fd = urandom_fd;
}
else else
PyErr_SetFromErrno(PyExc_OSError); urandom_fd = fd;
return -1;
} }
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
...@@ -168,12 +181,20 @@ dev_urandom_python(char *buffer, Py_ssize_t size) ...@@ -168,12 +181,20 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
PyErr_Format(PyExc_RuntimeError, PyErr_Format(PyExc_RuntimeError,
"Failed to read %zi bytes from /dev/urandom", "Failed to read %zi bytes from /dev/urandom",
size); size);
close(fd);
return -1; return -1;
} }
close(fd);
return 0; return 0;
} }
static void
dev_urandom_close(void)
{
if (urandom_fd >= 0) {
close(urandom_fd);
urandom_fd = -1;
}
}
#endif /* !defined(MS_WINDOWS) && !defined(__VMS) */ #endif /* !defined(MS_WINDOWS) && !defined(__VMS) */
/* Fill buffer with pseudo-random bytes generated by a linear congruent /* Fill buffer with pseudo-random bytes generated by a linear congruent
...@@ -271,3 +292,11 @@ _PyRandom_Init(void) ...@@ -271,3 +292,11 @@ _PyRandom_Init(void)
#endif #endif
} }
} }
void
_PyRandom_Fini(void)
{
#if !defined(MS_WINDOWS) && !defined(__VMS)
dev_urandom_close();
#endif
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment