cacheddir: disable cache (avoid memory leak)
authorZac Medico <zmedico@gentoo.org>
Thu, 27 Jun 2013 19:38:39 +0000 (12:38 -0700)
committerZac Medico <zmedico@gentoo.org>
Thu, 27 Jun 2013 19:38:39 +0000 (12:38 -0700)
The global dircache is no longer supported, since it could
be a memory leak for API consumers. Any cacheddir callers
should use higher-level caches instead, when necessary.

pym/_emerge/clear_caches.py
pym/portage/util/listdir.py

index 7b7c5eced6a9c5733dd6e5421723804885978e3a..513df626ff0a68322a5f06f8d0185113b284b9c6 100644 (file)
@@ -1,8 +1,7 @@
-# Copyright 1999-2010 Gentoo Foundation
+# Copyright 1999-2013 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 import gc
-from portage.util.listdir import dircache
 
 def clear_caches(trees):
        for d in trees.values():
@@ -15,5 +14,4 @@ def clear_caches(trees):
                        pass
                else:
                        d["vartree"].dbapi._linkmap._clear_cache()
-       dircache.clear()
        gc.collect()
index b78ed19fec45ad0ad030efe4c04c4fa55dc1cee1..61a025ae79a9ae8beca3a4219635e1a4d15d2258 100644 (file)
@@ -5,32 +5,26 @@ __all__ = ['cacheddir', 'listdir']
 
 import errno
 import stat
-import time
 
 from portage import os
 from portage.const import VCS_DIRS
 from portage.exception import DirectoryNotFound, PermissionDenied, PortageException
-from portage.util import normalize_path, writemsg
-
+from portage.util import normalize_path
+
+# The global dircache is no longer supported, since it could
+# be a memory leak for API consumers. Any cacheddir callers
+# should use higher-level caches instead, when necessary.
+# TODO: Remove dircache variable after stable portage does
+# not use is (keep it for now, in case API consumers clear
+# it manually).
 dircache = {}
-cacheHit = 0
-cacheMiss = 0
-cacheStale = 0
 
 def cacheddir(my_original_path, ignorecvs, ignorelist, EmptyOnError, followSymlinks=True):
-       global cacheHit,cacheMiss,cacheStale
        mypath = normalize_path(my_original_path)
-       if mypath in dircache:
-               cacheHit += 1
-               cached_mtime, list, ftype = dircache[mypath]
-       else:
-               cacheMiss += 1
-               cached_mtime, list, ftype = -1, [], []
+       cached_mtime, list, ftype = -1, [], []
        try:
                pathstat = os.stat(mypath)
-               if stat.S_ISDIR(pathstat[stat.ST_MODE]):
-                       mtime = pathstat.st_mtime
-               else:
+               if not stat.S_ISDIR(pathstat.st_mode):
                        raise DirectoryNotFound(mypath)
        except EnvironmentError as e:
                if e.errno == PermissionDenied.errno:
@@ -39,10 +33,7 @@ def cacheddir(my_original_path, ignorecvs, ignorelist, EmptyOnError, followSymli
                return [], []
        except PortageException:
                return [], []
-       # Python retuns mtime in seconds, so if it was changed in the last few seconds, it could be invalid
-       if mtime != cached_mtime or time.time() - mtime < 4:
-               if mypath in dircache:
-                       cacheStale += 1
+       else:
                try:
                        list = os.listdir(mypath)
                except EnvironmentError as e:
@@ -68,7 +59,6 @@ def cacheddir(my_original_path, ignorecvs, ignorelist, EmptyOnError, followSymli
                                        ftype.append(3)
                        except (IOError, OSError):
                                ftype.append(3)
-               dircache[mypath] = mtime, list, ftype
 
        ret_list = []
        ret_ftype = []
@@ -84,7 +74,6 @@ def cacheddir(my_original_path, ignorecvs, ignorelist, EmptyOnError, followSymli
                        ret_list.append(list[x])
                        ret_ftype.append(ftype[x])
 
-       writemsg("cacheddirStats: H:%d/M:%d/S:%d\n" % (cacheHit, cacheMiss, cacheStale),10)
        return ret_list, ret_ftype
 
 def listdir(mypath, recursive=False, filesonly=False, ignorecvs=False, ignorelist=[], followSymlinks=True,