8000 [3.6] bpo-33021: Release the GIL during fstat() calls (GH-6019) by miss-islington · Pull Request #6160 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Release the GIL during fstat() calls, avoiding hang of all threads when
calling mmap.mmap(), os.urandom(), and random.seed(). Patch by Nir Soffer.
11 changes: 9 additions & 2 deletions Modules/mmapmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,7 @@ static PyObject *
new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
{
struct _Py_stat_struct status;
int fstat_result = -1;
mmap_object *m_obj;
Py_ssize_t map_size;
off_t offset = 0;
Expand Down Expand Up @@ -1143,8 +1144,14 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
if (fd != -1)
(void)fcntl(fd, F_FULLFSYNC);
#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 (status.st_size == 0) {
PyErr_SetString(PyExc_ValueError,
Expand Down
7 changes: 6 additions & 1 deletion Python/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,15 @@ dev_urandom(char *buffer, Py_ssize_t size, int raise)

if (raise) {
struct _Py_stat_struct st;
int fstat_result;

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) */
if (_Py_fstat_noraise(urandom_cache.fd, &st)
if (fstat_result
|| st.st_dev != urandom_cache.st_dev
|| st.st_ino != urandom_cache.st_ino) {
/* Something changed: forget the cached fd (but don't close it,
Expand Down
0