Kaydet (Commit) 4dbc95e2 authored tarafından Larry Hastings's avatar Larry Hastings

Issue #17899: Fix rare file descriptor leak in os.listdir().

üst ffff7631
...@@ -10,6 +10,8 @@ What's New in Python 3.4.0 Alpha 1? ...@@ -10,6 +10,8 @@ What's New in Python 3.4.0 Alpha 1?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #17899: Fix rare file descriptor leak in os.listdir().
- Issue #10241: Clear extension module dict copies at interpreter shutdown. - Issue #10241: Clear extension module dict copies at interpreter shutdown.
Patch by Neil Schemenauer, minimally modified. Patch by Neil Schemenauer, minimally modified.
......
...@@ -3420,12 +3420,13 @@ exit: ...@@ -3420,12 +3420,13 @@ exit:
static PyObject * static PyObject *
_posix_listdir(path_t *path, PyObject *list) _posix_listdir(path_t *path, PyObject *list)
{ {
int fd = -1;
PyObject *v; PyObject *v;
DIR *dirp = NULL; DIR *dirp = NULL;
struct dirent *ep; struct dirent *ep;
int return_str; /* if false, return bytes */ int return_str; /* if false, return bytes */
#ifdef HAVE_FDOPENDIR
int fd = -1;
#endif
errno = 0; errno = 0;
#ifdef HAVE_FDOPENDIR #ifdef HAVE_FDOPENDIR
...@@ -3467,6 +3468,13 @@ _posix_listdir(path_t *path, PyObject *list) ...@@ -3467,6 +3468,13 @@ _posix_listdir(path_t *path, PyObject *list)
if (dirp == NULL) { if (dirp == NULL) {
list = path_error(path); list = path_error(path);
#ifdef HAVE_FDOPENDIR
if (fd != -1) {
Py_BEGIN_ALLOW_THREADS
close(fd);
Py_END_ALLOW_THREADS
}
#endif
goto exit; goto exit;
} }
if ((list = PyList_New(0)) == NULL) { if ((list = PyList_New(0)) == NULL) {
...@@ -3509,8 +3517,10 @@ _posix_listdir(path_t *path, PyObject *list) ...@@ -3509,8 +3517,10 @@ _posix_listdir(path_t *path, PyObject *list)
exit: exit:
if (dirp != NULL) { if (dirp != NULL) {
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
#ifdef HAVE_FDOPENDIR
if (fd > -1) if (fd > -1)
rewinddir(dirp); rewinddir(dirp);
#endif
closedir(dirp); closedir(dirp);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
} }
......
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