FIX: Prevent critical TypeError in token counter with robust null check (v4)

The token counter was crashing during model loading/unloading (race condition) because it was attempting to access attributes on an object that was momentarily None.

This fix introduces a sequential and robust null check (sd_models.model_data is None or sd_models.model_data.sd_model is None) at the start of the function to safely return 0 tokens during unstable states.
pull/369/head
ねおん 2025-11-26 02:07:14 +09:00 committed by GitHub
parent 1b2cce5433
commit 077daf3f6e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 3 additions and 4 deletions

View File

@ -4,10 +4,9 @@ from functools import partial, reduce
def get_token_counter(text, steps):
# Check if the model is fully loaded to prevent TypeError during model switching.
# Checks both sd_model and its subcomponent (cond_stage_model).
if sd_models.model_data.sd_model is None or \
sd_models.model_data.sd_model.cond_stage_model is None:
# FIX: Robust Null Check to prevent TypeError during model loading/unloading.
# Checks for the existence of model_data and its property sd_model sequentially.
if sd_models.model_data is None or sd_models.model_data.sd_model is None:
return {"token_count": 0, "max_length": 0}
# copy from modules.ui.py