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

larry.hastings python-checkins at python.org
Fri Aug 2 04:39:14 CEST 2013


http://hg.python.org/cpython/rev/bf68711bc939
changeset:   84967:bf68711bc939
branch:      3.3
parent:      84955:a381721299a3
user:        Larry Hastings <larry at hastings.org>
date:        Thu Aug 01 19:34:46 2013 -0700
summary:
  Issue #17899: Fix rare file descriptor leak in os.listdir().
(Done as separate patch from trunk as the code has diverged quite a bit.)

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


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@
 Core and Builtins
 -----------------
 
+- Issue #17899: Fix rare file descriptor leak in os.listdir().
+
 - Issue #18552: Check return value of PyArena_AddPyObject() in
   obj2ast_object().
 
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -3443,7 +3443,9 @@
     path_t path;
     PyObject *list = NULL;
     static char *keywords[] = {"path", NULL};
+#ifdef HAVE_FDOPENDIR
     int fd = -1;
+#endif /* HAVE_FDOPENDIR */
 
 #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
     PyObject *v;
@@ -3732,6 +3734,13 @@
 
     if (dirp == NULL) {
         list = path_error("listdir", &path);
+#ifdef HAVE_FDOPENDIR
+        if (fd != -1) {
+            Py_BEGIN_ALLOW_THREADS
+            close(fd);
+            Py_END_ALLOW_THREADS
+        }
+#endif /* HAVE_FDOPENDIR */
         goto exit;
     }
     if ((list = PyList_New(0)) == NULL) {
@@ -3774,8 +3783,10 @@
 exit:
     if (dirp != NULL) {
         Py_BEGIN_ALLOW_THREADS
+#ifdef HAVE_FDOPENDIR
         if (fd > -1)
             rewinddir(dirp);
+#endif /* HAVE_FDOPENDIR */
         closedir(dirp);
         Py_END_ALLOW_THREADS
     }

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


More information about the Python-checkins mailing list