From cd7fa8027aea9658257d5289314f65ba337eda28 Mon Sep 17 00:00:00 2001 From: -LAN- Date: Mon, 22 Jul 2024 22:58:22 +0800 Subject: [PATCH] fix(api/core/model_manager.py): Avoid mutation during iteration. (#6536) --- api/core/model_manager.py | 3 +-- api/core/rag/datasource/keyword/keyword_base.py | 3 +-- api/core/rag/datasource/vdb/vector_base.py | 3 +-- api/core/rag/datasource/vdb/vector_factory.py | 3 +-- api/services/model_load_balancing_service.py | 3 +-- 5 files changed, 5 insertions(+), 10 deletions(-) diff --git a/api/core/model_manager.py b/api/core/model_manager.py index dc7556f09a..8e99ad3dec 100644 --- a/api/core/model_manager.py +++ b/api/core/model_manager.py @@ -410,10 +410,9 @@ class LBModelManager: self._model = model self._load_balancing_configs = load_balancing_configs - for load_balancing_config in self._load_balancing_configs: + for load_balancing_config in self._load_balancing_configs[:]: # Iterate over a shallow copy of the list if load_balancing_config.name == "__inherit__": if not managed_credentials: - # FIXME: Mutation to loop iterable `self._load_balancing_configs` during iteration # remove __inherit__ if managed credentials is not provided self._load_balancing_configs.remove(load_balancing_config) else: diff --git a/api/core/rag/datasource/keyword/keyword_base.py b/api/core/rag/datasource/keyword/keyword_base.py index 67bc6df6fd..b77c6562b2 100644 --- a/api/core/rag/datasource/keyword/keyword_base.py +++ b/api/core/rag/datasource/keyword/keyword_base.py @@ -38,11 +38,10 @@ class BaseKeyword(ABC): raise NotImplementedError def _filter_duplicate_texts(self, texts: list[Document]) -> list[Document]: - for text in texts: + for text in texts[:]: doc_id = text.metadata['doc_id'] exists_duplicate_node = self.text_exists(doc_id) if exists_duplicate_node: - # FIXME: Mutation to loop iterable `texts` during iteration texts.remove(text) return texts diff --git a/api/core/rag/datasource/vdb/vector_base.py b/api/core/rag/datasource/vdb/vector_base.py index 0b1d58856c..3f70e8b608 100644 --- a/api/core/rag/datasource/vdb/vector_base.py +++ b/api/core/rag/datasource/vdb/vector_base.py @@ -57,11 +57,10 @@ class BaseVector(ABC): raise NotImplementedError def _filter_duplicate_texts(self, texts: list[Document]) -> list[Document]: - for text in texts: + for text in texts[:]: doc_id = text.metadata['doc_id'] exists_duplicate_node = self.text_exists(doc_id) if exists_duplicate_node: - # FIXME: Mutation to loop iterable `texts` during iteration texts.remove(text) return texts diff --git a/api/core/rag/datasource/vdb/vector_factory.py b/api/core/rag/datasource/vdb/vector_factory.py index 509273e8ea..fad60ecf45 100644 --- a/api/core/rag/datasource/vdb/vector_factory.py +++ b/api/core/rag/datasource/vdb/vector_factory.py @@ -153,11 +153,10 @@ class Vector: return CacheEmbedding(embedding_model) def _filter_duplicate_texts(self, texts: list[Document]) -> list[Document]: - for text in texts: + for text in texts[:]: doc_id = text.metadata['doc_id'] exists_duplicate_node = self.text_exists(doc_id) if exists_duplicate_node: - # FIXME: Mutation to loop iterable `texts` during iteration texts.remove(text) return texts diff --git a/api/services/model_load_balancing_service.py b/api/services/model_load_balancing_service.py index 4f59b86c12..0983839996 100644 --- a/api/services/model_load_balancing_service.py +++ b/api/services/model_load_balancing_service.py @@ -131,9 +131,8 @@ class ModelLoadBalancingService: load_balancing_configs.insert(0, inherit_config) else: # move the inherit configuration to the first - for i, load_balancing_config in enumerate(load_balancing_configs): + for i, load_balancing_config in enumerate(load_balancing_configs[:]): if load_balancing_config.name == '__inherit__': - # FIXME: Mutation to loop iterable `load_balancing_configs` during iteration inherit_config = load_balancing_configs.pop(i) load_balancing_configs.insert(0, inherit_config)