[view] fix: random segmentation fault errors caused by the cache cleaner thread

This commit is contained in:
Vinicius Moreira
2023-12-20 08:17:29 -03:00
parent a084d8bf31
commit 78618eb70c
3 changed files with 13 additions and 44 deletions

View File

@@ -37,6 +37,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixes
- AppImage
- upgrade fails when the package was initially imported, but later added to bauh's database [#321](https://github.com/vinifmor/bauh/issues/321)
- General
- random segmentation fault errors associated with threads and caching
- Web
- search not working for some typed addresses (e.g: those returning 403)

View File

@@ -18,16 +18,14 @@ from bauh.view.qt.prepare import PreparePanel
from bauh.view.qt.settings import SettingsWindow
from bauh.view.qt.window import ManageWindow
from bauh.view.util import resource, util
from bauh.view.util.cache import CacheCleaner, DefaultMemoryCacheFactory
from bauh.view.util.cache import DefaultMemoryCacheFactory
from bauh.view.util.disk import DefaultDiskCacheLoaderFactory
def new_manage_panel(app_args: Namespace, app_config: dict, logger: logging.Logger) -> Tuple[QApplication, QWidget]:
i18n = generate_i18n(app_config, resource.get_path('locale'))
cache_cleaner = CacheCleaner()
cache_factory = DefaultMemoryCacheFactory(expiration_time=int(app_config['memory_cache']['data_expiration']), cleaner=cache_cleaner)
cache_factory = DefaultMemoryCacheFactory(expiration_time=int(app_config['memory_cache']['data_expiration']))
icon_cache = cache_factory.new(int(app_config['memory_cache']['icon_expiration']))
http_client = HttpClient(logger)
@@ -83,12 +81,9 @@ def new_manage_panel(app_args: Namespace, app_config: dict, logger: logging.Logg
force_suggestions=force_suggestions,
logger=logger)
prepare = PreparePanel(context=context,
manager=manager,
i18n=i18n,
manage_window=manage_window,
app_config=app_config,
force_suggestions=force_suggestions)
cache_cleaner.start()
return app, prepare
return app, PreparePanel(context=context,
manager=manager,
i18n=i18n,
manage_window=manage_window,
app_config=app_config,
force_suggestions=force_suggestions)

View File

@@ -1,6 +1,5 @@
import datetime
import time
from threading import Lock, Thread
from threading import Lock
from typing import Optional
from bauh.api.abstract.cache import MemoryCache, MemoryCacheFactory
@@ -76,41 +75,14 @@ class DefaultMemoryCache(MemoryCache):
self.get(key)
class CacheCleaner(Thread):
def __init__(self, check_interval: int = 15):
super(CacheCleaner, self).__init__(daemon=True)
self.caches = []
self.check_interval = check_interval
def register(self, cache: MemoryCache):
if cache.is_enabled():
self.caches.append(cache)
def run(self):
if self.caches:
while True:
for cache in self.caches:
cache.clean_expired()
time.sleep(self.check_interval)
class DefaultMemoryCacheFactory(MemoryCacheFactory):
def __init__(self, expiration_time: int, cleaner: CacheCleaner = None):
def __init__(self, expiration_time: int):
"""
:param expiration_time: default expiration time for all instantiated caches
:param cleaner
"""
super(DefaultMemoryCacheFactory, self).__init__()
self.expiration_time = expiration_time
self.cleaner = cleaner
def new(self, expiration: Optional[int] = None) -> MemoryCache:
instance = DefaultMemoryCache(expiration if expiration is not None else self.expiration_time)
if self.cleaner:
self.cleaner.register(instance)
return instance
return DefaultMemoryCache(expiration if expiration is not None else self.expiration_time)