[PR #4597] [CLOSED] Fix LM Studio race condition bug #4683

Closed
opened 2026-02-22 18:36:19 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Mintplex-Labs/anything-llm/pull/4597
Author: @shatfield4
Created: 10/30/2025
Status: Closed

Base: masterHead: 4572-bug-lmstudio-provided-llm-stopped-working-with-anything-llm-after-upgrading-to-190


📝 Commits (1)

  • 7a41fc4 patch race condition bug + new model download protection + add tests

📊 Changes

4 files changed (+324 additions, -24 deletions)

View changed files

server/__tests__/utils/AiProviders/lmStudio/index.test.js (+152 -0)
server/__tests__/utils/agents/aibitat/providers/lmstudio.test.js (+85 -0)
📝 server/utils/AiProviders/lmStudio/index.js (+79 -22)
📝 server/utils/agents/aibitat/providers/lmstudio.js (+8 -2)

📄 Description

Pull Request Type

  • feat
  • 🐛 fix
  • ♻️ refactor
  • 💄 style
  • 🔨 chore
  • 📝 docs

Relevant Issues

resolves #4572

What is in this change?

  • Patches LM Studio race condition bug
  • Bug occurs in the constructor when we begin fetching the context windows in the background (using .then in constructor does not wait for completion before instantiating the class) and there is no check to ensure this.limits was initialized before a chat can be sent
  • This race condition bug happens when a user has lots of models in LM Studio and the API has a delay in returning the context windows causing the chat to be sent before we can get a response back from the context window LM Studio API endpoint (this.limits is not initialized)
  • Adds awaited ensureModelCached guard function to all chat methods and agent provider chat methods to block a chat from happening until we either get a response back from the LM Studio API with correct context window OR fallback to 4096 as default context window size (ensures this.limits is initialized properly)
  • Adds checks for models that may have been downloaded from LM Studio AFTER the backend server was started and fetches context windows again from the LM Studio API if the model is not already cached instead of just defaulting back to 4096 immediately when the selected model doesn't exist in cache
  • Adds test cases for both LM Studio LLM and agent providers

Additional Information

Developer Validations

  • I ran yarn lint from the root of the repo & committed changes
  • Relevant documentation has been updated
  • I have tested my code functionality
  • Docker build succeeds locally

🔄 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/Mintplex-Labs/anything-llm/pull/4597 **Author:** [@shatfield4](https://github.com/shatfield4) **Created:** 10/30/2025 **Status:** ❌ Closed **Base:** `master` ← **Head:** `4572-bug-lmstudio-provided-llm-stopped-working-with-anything-llm-after-upgrading-to-190` --- ### 📝 Commits (1) - [`7a41fc4`](https://github.com/Mintplex-Labs/anything-llm/commit/7a41fc4dbdb16da08851e223c34944baad6cb48b) patch race condition bug + new model download protection + add tests ### 📊 Changes **4 files changed** (+324 additions, -24 deletions) <details> <summary>View changed files</summary> ➕ `server/__tests__/utils/AiProviders/lmStudio/index.test.js` (+152 -0) ➕ `server/__tests__/utils/agents/aibitat/providers/lmstudio.test.js` (+85 -0) 📝 `server/utils/AiProviders/lmStudio/index.js` (+79 -22) 📝 `server/utils/agents/aibitat/providers/lmstudio.js` (+8 -2) </details> ### 📄 Description ### Pull Request Type <!-- For change type, change [ ] to [x]. --> - [ ] ✨ feat - [x] 🐛 fix - [ ] ♻️ refactor - [ ] 💄 style - [ ] 🔨 chore - [ ] 📝 docs ### Relevant Issues <!-- Use "resolves #xxx" to auto resolve on merge. Otherwise, please use "connect #xxx" --> resolves #4572 ### What is in this change? <!-- Describe the changes in this PR that are impactful to the repo. --> - Patches LM Studio race condition bug - Bug occurs in the constructor when we begin fetching the context windows in the background (using `.then` in constructor does not wait for completion before instantiating the class) and there is no check to ensure `this.limits` was initialized before a chat can be sent - This race condition bug happens when a user has lots of models in LM Studio and the API has a delay in returning the context windows causing the chat to be sent before we can get a response back from the context window LM Studio API endpoint (`this.limits` is not initialized) - Adds awaited `ensureModelCached` guard function to all chat methods and agent provider chat methods to block a chat from happening until we either get a response back from the LM Studio API with correct context window OR fallback to 4096 as default context window size (ensures `this.limits` is initialized properly) - Adds checks for models that may have been downloaded from LM Studio AFTER the backend server was started and fetches context windows again from the LM Studio API if the model is not already cached instead of just defaulting back to 4096 immediately when the selected model doesn't exist in cache - Adds test cases for both LM Studio LLM and agent providers ### Additional Information <!-- Add any other context about the Pull Request here that was not captured above. --> ### Developer Validations <!-- All of the applicable items should be checked. --> - [x] I ran `yarn lint` from the root of the repo & committed changes - [x] Relevant documentation has been updated - [x] I have tested my code functionality - [x] Docker build succeeds locally --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-22 18:36:19 -05:00
yindo closed this issue 2026-02-22 18:36:19 -05:00
yindo changed title from [PR #4597] Fix LM Studio race condition bug to [PR #4597] [CLOSED] Fix LM Studio race condition bug 2026-06-05 15:19:48 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mintplex-Labs/anything-llm#4683