[PR #369] [MERGED] Fix: Use try-except to stabilize token counter during model access #362

Closed
opened 2026-11-07 17:43:46 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Physton/sd-webui-prompt-all-in-one/pull/369
Author: @neon-aiart
Created: 11/26/2025
Status: ✅ Merged
Merged: 11/26/2025
Merged by: @Physton

Base: main ← Head: fix/token-counter-v4


📝 Commits (2)

  • 077daf3 FIX: Prevent critical TypeError in token counter with robust null check (v4)
  • 88be653 Fix: Use try-except to stabilize token counter during model access

📊 Changes

1 file changed (+30 additions, -29 deletions)

View changed files

📝 scripts/physton_prompt/get_token_counter.py (+30 -29)

📄 Description

This commit fixes a critical server crash issue that occurs when the token counter API is triggered during model instability.

🐛 Bug Details

The crash occurs when the token counter API is called (e.g., by user interaction like using a word/tag completion extension) while the model is switching or loading (e.g., via the dropdown menu or XYZ Plot).

  • Cause: The model object (sd_models.model_data.sd_model) exists in memory but its internal component, cond_stage_model, is momentarily None or in an incomplete state due to aggressive memory management in Forge/reForge.
  • Crash Location: Accessing the .cond_stage_model attribute triggers a TypeError: argument of type 'NoneType' is not iterable within PyTorch's internal __getattr__ method, which standard Python if checks cannot safely bypass.

🛠️ Solution and Rationale

Previous attempts using sequential if checks (e.g., if A is None or A.B is None) failed because the crash happened inside PyTorch's attribute lookup process, bypassing Python's short-circuit evaluation.

  • Solution: The entire critical section of the get_token_counter function is wrapped in a try-except block as the final and most robust defense.
  • Action: If a model access error occurs due to instability, the function safely returns {"token_count": 0, "max_length": 0} instead of crashing the server.

🕹️ Reproduction Steps

  1. Start a model load/switch (e.g., selecting a different model from the top-left dropdown).
  2. While the model is loading (monitor "Loading model..." in logs), actively change the prompt content (e.g., using a word/tag completion extension's add/remove button) to repeatedly trigger the token counter API.
  3. Server crashes immediately.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Physton/sd-webui-prompt-all-in-one/pull/369 **Author:** [@neon-aiart](https://github.com/neon-aiart) **Created:** 11/26/2025 **Status:** ✅ Merged **Merged:** 11/26/2025 **Merged by:** [@Physton](https://github.com/Physton) **Base:** `main` ← **Head:** `fix/token-counter-v4` --- ### 📝 Commits (2) - [`077daf3`](https://github.com/Physton/sd-webui-prompt-all-in-one/commit/077daf3f6e0f8dcf720206df4003160e3787417a) FIX: Prevent critical TypeError in token counter with robust null check (v4) - [`88be653`](https://github.com/Physton/sd-webui-prompt-all-in-one/commit/88be6531e8c7cc29d8d8436912b1014a71500960) Fix: Use try-except to stabilize token counter during model access ### 📊 Changes **1 file changed** (+30 additions, -29 deletions) <details> <summary>View changed files</summary> 📝 `scripts/physton_prompt/get_token_counter.py` (+30 -29) </details> ### 📄 Description This commit fixes a critical server crash issue that occurs when the token counter API is triggered during model instability. ## 🐛 Bug Details The crash occurs when the token counter API is called (e.g., by user interaction like using a word/tag completion extension) while the model is switching or loading (e.g., via the dropdown menu or XYZ Plot). * **Cause:** The model object (`sd_models.model_data.sd_model`) exists in memory but its internal component, `cond_stage_model`, is momentarily `None` or in an incomplete state due to aggressive memory management in Forge/reForge. * **Crash Location:** Accessing the `.cond_stage_model` attribute triggers a `TypeError: argument of type 'NoneType' is not iterable` within PyTorch's internal `__getattr__` method, which standard Python `if` checks cannot safely bypass. ## 🛠️ Solution and Rationale Previous attempts using sequential `if` checks (e.g., `if A is None or A.B is None`) failed because the crash happened inside PyTorch's attribute lookup process, bypassing Python's short-circuit evaluation. * **Solution:** The entire critical section of the `get_token_counter` function is wrapped in a **`try-except`** block as the final and most robust defense. * **Action:** If a model access error occurs due to instability, the function safely returns `{"token_count": 0, "max_length": 0}` instead of crashing the server. ## 🕹️ Reproduction Steps 1. Start a model load/switch (e.g., selecting a different model from the top-left dropdown). 2. While the model is loading (monitor "Loading model..." in logs), **actively change the prompt content** (e.g., using a word/tag completion extension's add/remove button) to repeatedly trigger the token counter API. 3. Server crashes immediately. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 17:43:46 +07:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Physton/sd-webui-prompt-all-in-one#362
No description provided.