[PR #15528] server: fix data race in PsHandler on sched.loaded map #25727

Open
opened 2026-04-19 18:22:44 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/15528
Author: @sjhddh
Created: 4/12/2026
Status: 🔄 Open

Base: mainHead: fix/ps-handler-data-race


📝 Commits (1)

  • 15ffc58 server: fix data race in PsHandler on sched.loaded map

📊 Changes

2 files changed (+143 additions, -0 deletions)

View changed files

📝 server/routes.go (+2 -0)
server/routes_ps_test.go (+141 -0)

📄 Description

PsHandler iterates s.sched.loaded without holding loadedMu. Every other access site in the scheduler properly acquires the mutex, but this handler was missed.

Under concurrent load — any GET /api/ps while a model is loading or unloading — this causes a fatal concurrent map iteration and map write panic, crashing the server.

Fix: acquire loadedMu for the duration of the map iteration in PsHandler.

Testing: added TestPsHandlerConcurrentAccess which reproduces the race (fails under go test -race without this fix) and TestPsHandler for basic correctness of the endpoint response.


🔄 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/ollama/ollama/pull/15528 **Author:** [@sjhddh](https://github.com/sjhddh) **Created:** 4/12/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/ps-handler-data-race` --- ### 📝 Commits (1) - [`15ffc58`](https://github.com/ollama/ollama/commit/15ffc5873b8e14689db94dd82731f979f50cbf59) server: fix data race in PsHandler on sched.loaded map ### 📊 Changes **2 files changed** (+143 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `server/routes.go` (+2 -0) ➕ `server/routes_ps_test.go` (+141 -0) </details> ### 📄 Description `PsHandler` iterates `s.sched.loaded` without holding `loadedMu`. Every other access site in the scheduler properly acquires the mutex, but this handler was missed. Under concurrent load — any `GET /api/ps` while a model is loading or unloading — this causes a fatal `concurrent map iteration and map write` panic, crashing the server. **Fix:** acquire `loadedMu` for the duration of the map iteration in `PsHandler`. **Testing:** added `TestPsHandlerConcurrentAccess` which reproduces the race (fails under `go test -race` without this fix) and `TestPsHandler` for basic correctness of the endpoint response. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-19 18:22:44 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#25727