Skip to content
Closed
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,4 @@
Release the GIL during fstat() calls, avoiding hang of all threads when
calling imp.load_dynamic(), imp.load_source(), mmap.mmap(),
mmapobject.size(), os.fdopen(), os.urandom(), and random.seed(). Patch by
Nir Soffer.
18 changes: 16 additions & 2 deletions Modules/mmapmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,13 @@ mmap_size_method(mmap_object *self,
#ifdef UNIX
{
struct stat buf;
if (-1 == fstat(self->fd, &buf)) {
int res;

Py_BEGIN_ALLOW_THREADS
res = fstat(self->fd, &buf);
Py_END_ALLOW_THREADS

if (-1 == res) {
PyErr_SetFromErrno(mmap_module_error);
return NULL;
}
Expand Down Expand Up @@ -1097,6 +1103,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
{
#ifdef HAVE_FSTAT
struct stat st;
int fstat_result;
#endif
mmap_object *m_obj;
Py_ssize_t map_size;
Expand Down Expand Up @@ -1170,7 +1177,14 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
fsync(fd);
}
# endif
if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {

if (fd != -1) {
Py_BEGIN_ALLOW_THREADS
fstat_result = fstat(fd, &st);
Py_END_ALLOW_THREADS
}

if (fd != -1 && fstat_result == 0 && S_ISREG(st.st_mode)) {
if (map_size == 0) {
if (st.st_size == 0) {
PyErr_SetString(PyExc_ValueError,
Expand Down
8 changes: 7 additions & 1 deletion Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6920,7 +6920,13 @@ posix_fdopen(PyObject *self, PyObject *args)
struct stat buf;
const char *msg;
PyObject *exc;
if (fstat(fd, &buf) == 0 && S_ISDIR(buf.st_mode)) {
int res;

Py_BEGIN_ALLOW_THREADS
res = fstat(fd, &buf);
Py_END_ALLOW_THREADS

if (res == 0 && S_ISDIR(buf.st_mode)) {
PyMem_FREE(mode);
msg = strerror(EISDIR);
exc = PyObject_CallFunction(PyExc_IOError, "(iss)",
Expand Down
4 changes: 4 additions & 0 deletions Python/dynload_shlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
if (fp != NULL) {
int i;
struct stat statb;

Py_BEGIN_ALLOW_THREADS
fstat(fileno(fp), &statb);
Py_END_ALLOW_THREADS

for (i = 0; i < nhandles; i++) {
if (statb.st_dev == handles[i].dev &&
statb.st_ino == handles[i].ino) {
Expand Down
7 changes: 6 additions & 1 deletion Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -1054,14 +1054,19 @@ static PyObject *
load_source_module(char *name, char *pathname, FILE *fp)
{
struct stat st;
int fstat_result;
FILE *fpc;
char *buf;
char *cpathname;
PyCodeObject *co = NULL;
PyObject *m;
time_t mtime;

if (fstat(fileno(fp), &st) != 0) {
Py_BEGIN_ALLOW_THREADS
fstat_result = fstat(fileno(fp), &st);
Py_END_ALLOW_THREADS

if (fstat_result != 0) {
PyErr_Format(PyExc_RuntimeError,
"unable to get file status from '%s'",
pathname);
Expand Down
11 changes: 9 additions & 2 deletions Python/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,18 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
int fd;
Py_ssize_t n;
struct stat st;
int fstat_result;
int attr;

if (size <= 0)
return 0;

if (urandom_cache.fd >= 0) {
/* Does the fd point to the same thing as before? (issue #21207) */
if (fstat(urandom_cache.fd, &st)
Py_BEGIN_ALLOW_THREADS
fstat_result = fstat(urandom_cache.fd, &st);
Py_END_ALLOW_THREADS
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 Expand Up @@ -257,7 +261,10 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
fd = urandom_cache.fd;
}
else {
if (fstat(fd, &st)) {
Py_BEGIN_ALLOW_THREADS
fstat_result = fstat(fd, &st);
Py_END_ALLOW_THREADS
if (fstat_result) {
PyErr_SetFromErrno(PyExc_OSError);
close(fd);
return -1;
Expand Down