summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apt/cache.py2
-rw-r--r--tests/test_apt_cache.py17
2 files changed, 19 insertions, 0 deletions
diff --git a/apt/cache.py b/apt/cache.py
index adc936ef..beebde8a 100644
--- a/apt/cache.py
+++ b/apt/cache.py
@@ -142,6 +142,8 @@ class Cache(object):
"""
if progress is None:
progress = apt.progress.base.OpProgress()
+ # close old cache on (re)open
+ self.close()
self.op_progress = progress
self._run_callbacks("cache_pre_open")
diff --git a/tests/test_apt_cache.py b/tests/test_apt_cache.py
index c850cec2..22c4e871 100644
--- a/tests/test_apt_cache.py
+++ b/tests/test_apt_cache.py
@@ -100,6 +100,23 @@ class TestAptCache(TestCase):
self.assertEqual(fds_before_open, fds_after_close)
self.assertEqual(unclosed_fd, set())
+ def test_cache_open_twice_leaks_fds(self):
+ cache = apt.Cache()
+ fds_before_open = get_open_file_descriptors()
+ cache.open()
+ fds_after_open_twice = get_open_file_descriptors()
+ self.assertEqual(fds_before_open, fds_after_open_twice)
+
+ @if_sources_list_is_readable
+ def test_cache_delete_leasks_fds(self):
+ fds_before_open = get_open_file_descriptors()
+ cache = apt.Cache()
+ del cache
+ import gc
+ gc.collect()
+ fds_after_delete = get_open_file_descriptors()
+ self.assertEqual(fds_before_open, fds_after_delete)
+
@if_sources_list_is_readable
def test_cache_close_download_fails(self):
cache = apt.Cache()