[Python-checkins] cpython: Issue #17899: Fix rare file descriptor leak in os.listdir().

larry.hastings python-checkins at python.org
Fri Aug 2 03:21:51 CEST 2013


http://hg.python.org/cpython/rev/e51cbc45f4ca
changeset:   84966:e51cbc45f4ca
user:        Larry Hastings <larry at hastings.org>
date:        Thu Aug 01 18:18:56 2013 -0700
summary:
  Issue #17899: Fix rare file descriptor leak in os.listdir().

files:
  Misc/NEWS             |   2 ++
  Modules/posixmodule.c |  14 ++++++++++++--
  2 files changed, 14 insertions(+), 2 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@
 Core and Builtins
 -----------------
 
+- Issue #17899: Fix rare file descriptor leak in os.listdir().
+
 - Issue #10241: Clear extension module dict copies at interpreter shutdown.
   Patch by Neil Schemenauer, minimally modified.
 
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -3420,12 +3420,13 @@
 static PyObject *
 _posix_listdir(path_t *path, PyObject *list)
 {
-    int fd = -1;
-
     PyObject *v;
     DIR *dirp = NULL;
     struct dirent *ep;
     int return_str; /* if false, return bytes */
+#ifdef HAVE_FDOPENDIR
+    int fd = -1;
+#endif
 
     errno = 0;
 #ifdef HAVE_FDOPENDIR
@@ -3467,6 +3468,13 @@
 
     if (dirp == NULL) {
         list = path_error(path);
+#ifdef HAVE_FDOPENDIR
+        if (fd != -1) {
+            Py_BEGIN_ALLOW_THREADS
+            close(fd);
+            Py_END_ALLOW_THREADS
+        }
+#endif
         goto exit;
     }
     if ((list = PyList_New(0)) == NULL) {
@@ -3509,8 +3517,10 @@
 exit:
     if (dirp != NULL) {
         Py_BEGIN_ALLOW_THREADS
+#ifdef HAVE_FDOPENDIR
         if (fd > -1)
             rewinddir(dirp);
+#endif
         closedir(dirp);
         Py_END_ALLOW_THREADS
     }

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list