[PR #4600] [CLOSED] Fix Ollama race condition bug #4684

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/4600
Author: @shatfield4
Created: 10/30/2025
Status: Closed

Base: masterHead: 4599-bug-ollama-race-condition-bug


📝 Commits (2)

  • c08c99f patch ollama race condition bug + new model download protection + add tests
  • 5569181 Merge branch 'master' into 4599-bug-ollama-race-condition-bug

📊 Changes

4 files changed (+361 additions, -28 deletions)

View changed files

server/__tests__/utils/AiProviders/ollama/index.test.js (+190 -0)
server/__tests__/utils/agents/aibitat/providers/ollama.test.js (+82 -0)
📝 server/utils/AiProviders/ollama/index.js (+81 -26)
📝 server/utils/agents/aibitat/providers/ollama.js (+8 -2)

📄 Description

Pull Request Type

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

Relevant Issues

resolves #4599

What is in this change?

  • Patches Ollama race condition bug (very similar to #4597)
  • 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 Ollama 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 Ollama 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 Ollama 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 Ollama AFTER the backend server was started and fetches context windows again from the Ollama 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 Ollama 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/4600 **Author:** [@shatfield4](https://github.com/shatfield4) **Created:** 10/30/2025 **Status:** ❌ Closed **Base:** `master` ← **Head:** `4599-bug-ollama-race-condition-bug` --- ### 📝 Commits (2) - [`c08c99f`](https://github.com/Mintplex-Labs/anything-llm/commit/c08c99fe929807dd3e1a4668b6ecfd45dfca9376) patch ollama race condition bug + new model download protection + add tests - [`5569181`](https://github.com/Mintplex-Labs/anything-llm/commit/5569181ca768b0f10e76b1e70a1cbf5dac08f83d) Merge branch 'master' into 4599-bug-ollama-race-condition-bug ### 📊 Changes **4 files changed** (+361 additions, -28 deletions) <details> <summary>View changed files</summary> ➕ `server/__tests__/utils/AiProviders/ollama/index.test.js` (+190 -0) ➕ `server/__tests__/utils/agents/aibitat/providers/ollama.test.js` (+82 -0) 📝 `server/utils/AiProviders/ollama/index.js` (+81 -26) 📝 `server/utils/agents/aibitat/providers/ollama.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 #4599 ### What is in this change? <!-- Describe the changes in this PR that are impactful to the repo. --> - Patches Ollama race condition bug (very similar to #4597) - 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 Ollama 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 Ollama 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 Ollama 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 Ollama AFTER the backend server was started and fetches context windows again from the Ollama 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 Ollama 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 #4600] Fix Ollama race condition bug to [PR #4600] [CLOSED] Fix Ollama race condition bug 2026-06-05 15:19:49 -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#4684