Skip to content

Fix TaskQueue worker thread 100% CPU spin when idle#1899

Open
Halcyonhal9 wants to merge 2 commits intokvcache-ai:mainfrom
Halcyonhal9:fix-taskqueue-cpu-spin
Open

Fix TaskQueue worker thread 100% CPU spin when idle#1899
Halcyonhal9 wants to merge 2 commits intokvcache-ai:mainfrom
Halcyonhal9:fix-taskqueue-cpu-spin

Conversation

@Halcyonhal9
Copy link

Summary

Fixes #1858 and #1898. The TaskQueue::worker() thread busy-spins at 100% CPU when no tasks are queued, wasting an entire core even when the server is fully idle. sync() has the same problem — pure spin on an atomic load.

This replaces the busy-wait loops with a std::condition_variable wait/notify pattern:

  • worker(): cv.wait() with predicate when queue is empty (was: unconditional spin-trylock loop)
  • sync(): cv.wait() with predicate on pending count (was: while(pending > n); spin)
  • enqueue(): mutex lock-unlock + cv.notify_one() to wake the idle worker
  • ~TaskQueue(): mutex-guarded done flag + cv.notify_all() for clean shutdown

The mutex only guards CV sleep/wake transitions — the lock-free linked-list enqueue path is preserved. The lock-unlock serialization pattern prevents the classic lost-wakeup race where a notification fires between a predicate check and the wait() call.

Before fix: sglang::schedul thread at 99.8% CPU when idle.
After fix: All threads at 0.0% CPU when idle.

Test plan

  • run_suite.py --hw cpu --suite default — 9/9 tests pass (accuracy + benchmarks)
  • Manual inference smoke test with MiniMax-M2.5 via sglang — responses correct
  • pidstat -t confirms 0% CPU on all threads when idle (was 99.8%)
  • CPU usage returns to ~0% after completing inference requests
  • Server starts and shuts down cleanly (no CV deadlock)

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the efficiency of the TaskQueue by replacing CPU-intensive busy-wait loops with std::condition_variable for thread synchronization. This change eliminates the problem of the worker thread consuming 100% CPU when idle, leading to a substantial reduction in resource consumption and a more energy-efficient operation of the server.

Highlights

  • Eliminated 100% CPU Busy-Spin: Replaced busy-wait loops in the TaskQueue::worker() thread and sync() method with std::condition_variable wait/notify patterns to prevent 100% CPU usage when idle.
  • Worker Thread Efficiency: The worker() method now uses cv.wait() with a predicate when the task queue is empty, ensuring the thread sleeps instead of busy-waiting.
  • Synchronous Task Waiting: The sync() method was refactored to use cv.wait() with a predicate on the pending task count, allowing it to efficiently wait for tasks to complete without spinning.
  • Task Enqueue Notification: The enqueue() method now includes a cv.notify_one() call after adding a task, waking a single idle worker thread to process the new task.
  • Graceful Shutdown: The ~TaskQueue() destructor now acquires a mutex and uses cv.notify_all() to unblock any waiting threads, ensuring a clean shutdown.
  • Lock-Free Enqueue Path Preservation: The mutex is strategically used only for condition variable sleep/wake transitions, preserving the existing lock-free linked-list enqueue path for performance.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the TaskQueue to use std::mutex and std::condition_variable for synchronization, replacing a spin-lock in the sync method and introducing a waiting mechanism in the worker thread. The review identifies a potential deadlock in the sync method if the done flag is not checked in the wait predicate. It also points out that the empty lock scope in enqueue introduces a performance bottleneck and suggests using cv.wait_for in the worker function to avoid locking in enqueue and prevent lost notifications.

…ck during destruction

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KTransformers TaskQueue coordinator thread spins at 100% CPU when idle

1 participant