[PR #13467] feat: prevent system sleep while Ollama is processing requests #40094

Open
opened 2026-04-23 01:05:14 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/13467
Author: @majiayu000
Created: 12/14/2025
Status: 🔄 Open

Base: mainHead: feat/prevent-sleep-while-active


📝 Commits (1)

  • dcad560 feat: prevent system sleep while Ollama is processing requests

📊 Changes

7 files changed (+296 additions, -0 deletions)

View changed files

server/power.go (+57 -0)
server/power_darwin.go (+57 -0)
server/power_linux.go (+70 -0)
server/power_other.go (+13 -0)
server/power_test.go (+53 -0)
server/power_windows.go (+37 -0)
📝 server/sched.go (+9 -0)

📄 Description

Summary

Prevent the system from entering sleep mode while Ollama has active runners processing requests. This fixes the issue where long-running inference tasks could be interrupted by system sleep, especially when using Ollama over LAN.

Changes

Added a PowerManager module with platform-specific implementations:

Platform Implementation
Windows SetThreadExecutionState API with ES_SYSTEM_REQUIRED flag
macOS IOPMAssertion via IOKit framework
Linux systemd-inhibit subprocess (gracefully degrades if unavailable)

Files Added

  • server/power.go - Core PowerManager with reference counting
  • server/power_windows.go - Windows implementation
  • server/power_darwin.go - macOS implementation
  • server/power_linux.go - Linux implementation
  • server/power_other.go - Stub for unsupported platforms
  • server/power_test.go - Unit tests

Scheduler Integration

Modified server/sched.go to:

  • Call PreventSleep() when a runner becomes active (refCount: 0 → 1)
  • Call AllowSleep() when a runner becomes idle (refCount: 1 → 0)

How It Works

The PowerManager uses reference counting to handle multiple concurrent runners:

  • Each active request increments the reference count and prevents sleep
  • When all requests complete (refCount = 0), system sleep is allowed again
  • Thread-safe via mutex protection

Testing

  • Added unit tests for PowerManager reference counting
  • All existing scheduler tests pass
  • Manually verified on macOS (IOPMAssertion logs visible)

Example Log Output

time=... level=DEBUG msg="system sleep prevented"
time=... level=DEBUG msg="system sleep allowed"

Fixes #4072

🤖 Generated with Claude Code


🔄 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/13467 **Author:** [@majiayu000](https://github.com/majiayu000) **Created:** 12/14/2025 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/prevent-sleep-while-active` --- ### 📝 Commits (1) - [`dcad560`](https://github.com/ollama/ollama/commit/dcad56089a180abc2cacdb2e56ec8963036a3750) feat: prevent system sleep while Ollama is processing requests ### 📊 Changes **7 files changed** (+296 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `server/power.go` (+57 -0) ➕ `server/power_darwin.go` (+57 -0) ➕ `server/power_linux.go` (+70 -0) ➕ `server/power_other.go` (+13 -0) ➕ `server/power_test.go` (+53 -0) ➕ `server/power_windows.go` (+37 -0) 📝 `server/sched.go` (+9 -0) </details> ### 📄 Description ## Summary Prevent the system from entering sleep mode while Ollama has active runners processing requests. This fixes the issue where long-running inference tasks could be interrupted by system sleep, especially when using Ollama over LAN. ## Changes Added a `PowerManager` module with platform-specific implementations: | Platform | Implementation | |----------|----------------| | **Windows** | `SetThreadExecutionState` API with `ES_SYSTEM_REQUIRED` flag | | **macOS** | `IOPMAssertion` via IOKit framework | | **Linux** | `systemd-inhibit` subprocess (gracefully degrades if unavailable) | ### Files Added - `server/power.go` - Core PowerManager with reference counting - `server/power_windows.go` - Windows implementation - `server/power_darwin.go` - macOS implementation - `server/power_linux.go` - Linux implementation - `server/power_other.go` - Stub for unsupported platforms - `server/power_test.go` - Unit tests ### Scheduler Integration Modified `server/sched.go` to: - Call `PreventSleep()` when a runner becomes active (refCount: 0 → 1) - Call `AllowSleep()` when a runner becomes idle (refCount: 1 → 0) ## How It Works The PowerManager uses reference counting to handle multiple concurrent runners: - Each active request increments the reference count and prevents sleep - When all requests complete (refCount = 0), system sleep is allowed again - Thread-safe via mutex protection ## Testing - Added unit tests for PowerManager reference counting - All existing scheduler tests pass - Manually verified on macOS (IOPMAssertion logs visible) ## Example Log Output ``` time=... level=DEBUG msg="system sleep prevented" time=... level=DEBUG msg="system sleep allowed" ``` Fixes #4072 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <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-23 01:05:14 -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#40094