portdbapi: cleanup when base module is reloaded
authorZac Medico <zmedico@gentoo.org>
Sat, 4 Jun 2011 01:38:40 +0000 (18:38 -0700)
committerZac Medico <zmedico@gentoo.org>
Sat, 4 Jun 2011 01:38:40 +0000 (18:38 -0700)
This prevents memory leaks via portdbapi.portdbapi_instances when the
module is reloaded. Also, there's no need for ebuild(1) to call
close_portdbapi_caches() since it's now handled automatically during
the reload.

bin/ebuild
pym/portage/__init__.py

index e3e8c6b347d2f17638e6fd270f06d5b98b31bbf4..9ad17e83d8af09fadcb43089f99b4c3ff7373ba2 100755 (executable)
@@ -147,7 +147,6 @@ if ebuild_portdir != vdb_path and \
                        encoding=_encodings['content'], errors='strict')
 
        print("Appending %s to PORTDIR_OVERLAY..." % ebuild_portdir)
-       portage.close_portdbapi_caches()
        imp.reload(portage)
 
 # Constrain eclass resolution to the master(s)
index 9e8298ef7f6c5aa5a59687733d8a258c9cf08b45..515a9a7a271e70e37b0fa6d5e815ecd7d95fcbbd 100644 (file)
@@ -576,6 +576,35 @@ if VERSION == 'HEAD':
                        return VERSION
        VERSION = _LazyVersion()
 
+if "_legacy_globals_constructed" in globals():
+       # The module has been reloaded, so perform any relevant cleanup
+       # and prevent memory leaks.
+       if "db" in _legacy_globals_constructed:
+               try:
+                       db
+               except NameError:
+                       pass
+               else:
+                       if isinstance(db, dict) and db:
+                               for _x in db.values():
+                                       try:
+                                               if "porttree" in _x.lazy_items:
+                                                       continue
+                                       except (AttributeError, TypeError):
+                                               continue
+                                       try:
+                                               _x = _x["porttree"].dbapi
+                                       except (AttributeError, KeyError):
+                                               continue
+                                       if not isinstance(_x, portdbapi):
+                                               continue
+                                       _x.close_caches()
+                                       try:
+                                               portdbapi.portdbapi_instances.remove(_x)
+                                       except ValueError:
+                                               pass
+                               del _x
+
 class _LegacyGlobalProxy(proxy.objectproxy.ObjectProxy):
 
        __slots__ = ('_name',)