Kaydet (Commit) 4484f9dc authored tarafından Nir Soffer's avatar Nir Soffer Kaydeden (comit) Antoine Pitrou

bpo-33021: Release the GIL during fstat() calls (GH-6019)

fstat may block for long time if the file descriptor is on a
non-responsive NFS server, hanging all threads. Most fstat() calls are
handled by _Py_fstat(), releasing the GIL internally, but but
_Py_fstat_noraise() does not release the GIL, and most calls release the
GIL explicitly around it.

This patch fixes last 2 calls to _Py_fstat_no_raise(), avoiding hangs
when calling:
- mmap.mmap()
- os.urandom()
- random.seed()
üst e756f66c
Release the GIL during fstat() calls, avoiding hang of all threads when
calling mmap.mmap(), os.urandom(), and random.seed(). Patch by Nir Soffer.
...@@ -1050,6 +1050,7 @@ static PyObject * ...@@ -1050,6 +1050,7 @@ static PyObject *
new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
{ {
struct _Py_stat_struct status; struct _Py_stat_struct status;
int fstat_result;
mmap_object *m_obj; mmap_object *m_obj;
Py_ssize_t map_size; Py_ssize_t map_size;
off_t offset = 0; off_t offset = 0;
...@@ -1115,8 +1116,14 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) ...@@ -1115,8 +1116,14 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
if (fd != -1) if (fd != -1)
(void)fcntl(fd, F_FULLFSYNC); (void)fcntl(fd, F_FULLFSYNC);
#endif #endif
if (fd != -1 && _Py_fstat_noraise(fd, &status) == 0
&& S_ISREG(status.st_mode)) { if (fd != -1) {
Py_BEGIN_ALLOW_THREADS
fstat_result = _Py_fstat_noraise(fd, &status);
Py_END_ALLOW_THREADS
}
if (fd != -1 && fstat_result == 0 && S_ISREG(status.st_mode)) {
if (map_size == 0) { if (map_size == 0) {
if (status.st_size == 0) { if (status.st_size == 0) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
......
...@@ -301,10 +301,15 @@ dev_urandom(char *buffer, Py_ssize_t size, int raise) ...@@ -301,10 +301,15 @@ dev_urandom(char *buffer, Py_ssize_t size, int raise)
if (raise) { if (raise) {
struct _Py_stat_struct st; struct _Py_stat_struct st;
int fstat_result;
if (urandom_cache.fd >= 0) { if (urandom_cache.fd >= 0) {
Py_BEGIN_ALLOW_THREADS
fstat_result = _Py_fstat_noraise(urandom_cache.fd, &st);
Py_END_ALLOW_THREADS
/* Does the fd point to the same thing as before? (issue #21207) */ /* Does the fd point to the same thing as before? (issue #21207) */
if (_Py_fstat_noraise(urandom_cache.fd, &st) if (fstat_result
|| st.st_dev != urandom_cache.st_dev || st.st_dev != urandom_cache.st_dev
|| st.st_ino != urandom_cache.st_ino) { || st.st_ino != urandom_cache.st_ino) {
/* Something changed: forget the cached fd (but don't close it, /* Something changed: forget the cached fd (but don't close it,
......
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