From 73a094a65773a3f2f9e7d626cfaa01893dbd3f88 Mon Sep 17 00:00:00 2001 From: Alvin Tang Date: Wed, 18 Mar 2026 09:06:05 +0800 Subject: [PATCH] Fix file handle leaks and redundant re-read in get_model_metadata (#7422) --- modules/models_settings.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/models_settings.py b/modules/models_settings.py index f3c9a986..dcface71 100644 --- a/modules/models_settings.py +++ b/modules/models_settings.py @@ -34,7 +34,8 @@ def get_model_metadata(model): path = model_path / 'config.json' if path.exists(): - hf_metadata = json.loads(open(path, 'r', encoding='utf-8').read()) + with open(path, 'r', encoding='utf-8') as f: + hf_metadata = json.loads(f.read()) else: hf_metadata = None @@ -93,7 +94,7 @@ def get_model_metadata(model): else: # Transformers metadata if hf_metadata is not None: - metadata = json.loads(open(path, 'r', encoding='utf-8').read()) + metadata = hf_metadata if 'pretrained_config' in metadata: metadata = metadata['pretrained_config'] @@ -134,7 +135,8 @@ def get_model_metadata(model): # 3. Fall back to tokenizer_config.json metadata if path.exists(): - metadata = json.loads(open(path, 'r', encoding='utf-8').read()) + with open(path, 'r', encoding='utf-8') as f: + metadata = json.loads(f.read()) # Only read from metadata if we haven't already loaded from .jinja or .json if template is None and 'chat_template' in metadata: