diff --git a/nodes.py b/nodes.py index b2f4545..c8e3e71 100644 --- a/nodes.py +++ b/nodes.py @@ -130,8 +130,13 @@ class DynamicRAMCacheControl: def _find_executor(self): for obj in gc.get_objects(): - if obj.__class__.__name__ == 'PromptExecutor': - return obj + try: + if obj.__class__.__name__ == 'PromptExecutor': + return obj + except (ReferenceError, AttributeError): + continue + except Exception: + continue return None def _get_cache_set(self, executor): @@ -147,13 +152,17 @@ class DynamicRAMCacheControl: return cache_set def _update_cache_set(self, cache_set, new_cache): - - cache_set.outputs = new_cache - - if hasattr(cache_set, 'all') and isinstance(cache_set.all, list): - for i, item in enumerate(cache_set.all): - if i == 0: - cache_set.all[i] = new_cache + try: + cache_set.outputs = new_cache + + if hasattr(cache_set, 'all') and isinstance(cache_set.all, list): + for i, item in enumerate(cache_set.all): + if i == 0: + cache_set.all[i] = new_cache + except (ReferenceError, AttributeError): + logging.warning("[DynamicRAMCache] Failed to update cache_set: object no longer exists.") + except Exception as e: + logging.warning(f"[DynamicRAMCache] Unexpected error updating cache_set: {e}") def _switch_to_ram_pressure(self, cache_set, old_cache, caching_mod): key_class = getattr(old_cache, 'key_class', None) @@ -162,17 +171,24 @@ class DynamicRAMCacheControl: new_cache = caching_mod.RAMPressureCache(key_class) self._migrate_cache_data(old_cache, new_cache) + if getattr(new_cache, 'timestamps', None) is None: + new_cache.timestamps = {} + if getattr(new_cache, 'used_generation', None) is None: + new_cache.used_generation = {} + if getattr(new_cache, 'children', None) is None: + new_cache.children = {} + if getattr(new_cache, 'generation', None) is None: + new_cache.generation = 1 + if getattr(new_cache, 'min_generation', None) is None: + new_cache.min_generation = 0 - new_cache.timestamps = {} - new_cache.used_generation = {} - new_cache.children = {} - new_cache.generation = 1 - new_cache.min_generation = 0 - - now = time.time() - for key in new_cache.cache: - new_cache.timestamps[key] = now - new_cache.used_generation[key] = 0 + if isinstance(getattr(new_cache, 'cache', None), dict) and isinstance(new_cache.timestamps, dict) and isinstance(new_cache.used_generation, dict): + now = time.time() + for key in new_cache.cache: + if key not in new_cache.timestamps: + new_cache.timestamps[key] = now + if key not in new_cache.used_generation: + new_cache.used_generation[key] = 0 self._update_cache_set(cache_set, new_cache) @@ -188,16 +204,37 @@ class DynamicRAMCacheControl: def _migrate_cache_data(self, old_cache, new_cache): """迁移缓存核心数据""" - # Fix for 'NullCache' object has no attribute 'cache' - if hasattr(old_cache, 'cache'): - new_cache.cache = old_cache.cache - - if hasattr(old_cache, 'subcaches'): - new_cache.subcaches = old_cache.subcaches - - new_cache.dynprompt = getattr(old_cache, 'dynprompt', None) - new_cache.cache_key_set = getattr(old_cache, 'cache_key_set', None) - new_cache.initialized = getattr(old_cache, 'initialized', False) + try: + old_dict = getattr(old_cache, '__dict__', None) + new_dict = getattr(new_cache, '__dict__', None) + if isinstance(old_dict, dict) and isinstance(new_dict, dict): + new_dict.update(old_dict) + else: + old_cache_data = getattr(old_cache, 'cache', None) + if old_cache_data is not None: + new_cache.cache = old_cache_data + + old_subcaches = getattr(old_cache, 'subcaches', None) + if old_subcaches is not None: + new_cache.subcaches = old_subcaches + + new_cache.dynprompt = getattr(old_cache, 'dynprompt', None) + new_cache.cache_key_set = getattr(old_cache, 'cache_key_set', None) + new_cache.initialized = getattr(old_cache, 'initialized', False) + + if getattr(new_cache, 'cache', None) is None: + new_cache.cache = {} + if getattr(new_cache, 'subcaches', None) is None: + new_cache.subcaches = {} + + if hasattr(old_cache, 'is_changed_cache'): + new_cache.is_changed_cache = old_cache.is_changed_cache + if getattr(new_cache, 'is_changed_cache', None) is None: + new_cache.is_changed_cache = {} + except (ReferenceError, AttributeError): + logging.warning("[DynamicRAMCache] Failed to migrate cache data: source object no longer exists.") + except Exception as e: + logging.warning(f"[DynamicRAMCache] Unexpected error migrating cache data: {e}") class RAMCacheExtremeCleanup(DynamicRAMCacheControl): def __init__(self):