Skip to content

Fix CPU spin when defunct runner is still in use#260

Closed
ericcurtin wants to merge 1 commit intomainfrom
investigate-high-cpu-usage
Closed

Fix CPU spin when defunct runner is still in use#260
ericcurtin wants to merge 1 commit intomainfrom
investigate-high-cpu-usage

Conversation

@ericcurtin
Copy link
Copy Markdown
Contributor

@ericcurtin ericcurtin commented Oct 19, 2025

User reported this problem on windows.

Summary by Sourcery

Bug Fixes:

  • Adjust idleCheckDuration to only trigger immediate checks for unused defunct runners, avoiding tight loops when runners are still active

User reported this problem on windows.

Signed-off-by: Eric Curtin <eric.curtin@docker.com>
Copilot AI review requested due to automatic review settings October 19, 2025 12:02
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Oct 19, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

The idleCheckDuration function now defers immediate defunct checks for runners that are still referenced, preventing tight CPU loops when a defunct runner remains in use.

Class diagram for updated loader idleCheckDuration logic

classDiagram
    class loader {
        runners map[int]RunnerInfo
        slots map[int]Slot
        references map[int]int
        timestamps map[int]time.Time
        idleCheckDuration() time.Duration
    }
    class RunnerInfo {
        slot int
    }
    class Slot {
        done chan_struct
    }
    loader --> RunnerInfo
    loader --> Slot
    loader --> time.Time
Loading

Flow diagram for idleCheckDuration defunct runner handling

flowchart TD
    A["Start idleCheckDuration()"] --> B["Iterate over runners"]
    B --> C["Check if runner is defunct (process exited)"]
    C --> D["Is runner unused? (references == 0)"]
    D -- Yes --> E["If defunct, return 0 (immediate check)"]
    D -- No --> F["If defunct but still in use, do not return 0"]
    F --> G["Continue to next runner"]
    E --> H["End"]
    G --> H
Loading

File-Level Changes

Change Details Files
defer defunct checks until a runner is unused to prevent CPU spin
  • Introduce a defunct flag set when a runner’s done channel is closed
  • Wrap immediate return in an unused-runner check (references == 0)
  • Ensure immediate 0-duration only for defunct and unused runners
  • Update comments to clarify handling of defunct vs. in-use runners
pkg/inference/scheduling/loader.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a CPU spinning issue that occurs when a defunct runner (whose process has exited) is still being used, causing tight loops of immediate scheduling checks. The fix differentiates between defunct runners that are unused (which need immediate checking) versus those still in use (which can wait until they become unused).

  • Modified the idle check duration logic to avoid immediate returns when defunct runners are still in use
  • Added logic to track defunct status and only trigger immediate checks for unused defunct runners
  • Prevents CPU spinning by avoiding unnecessary immediate scheduling for in-use defunct runners

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @ericcurtin, 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 resolves a significant performance problem, specifically a CPU spin observed on Windows, which arose when the system attempted to repeatedly check and clean up runner processes that had terminated but were still being referenced. The core change refines the logic for determining when to immediately check for idle runners, ensuring that defunct runners are only subject to immediate cleanup if they are truly unused. This prevents the system from entering a tight loop of checks, thereby reducing CPU load and improving overall stability.

Highlights

  • CPU Spin Fix: Addressed a CPU spinning issue that occurred when a defunct runner (a process that has exited) was still considered 'in use' by the system, particularly reported on Windows.
  • Conditional Defunct Runner Check: Modified the idleCheckDuration function to only trigger an immediate cleanup check for defunct runners if they are no longer actively referenced (i.e., unused). This prevents continuous, unnecessary checks that led to high CPU utilization.
  • Improved Resource Management: Ensured that defunct runners that are still in use will now be properly handled and cleaned up only when their references drop to zero, optimizing resource management and preventing performance degradation.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@ericcurtin
Copy link
Copy Markdown
Contributor Author

@xenoscopic @doringeman do you think this will fix #259 ?

Copy link
Copy Markdown
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 correctly fixes a high CPU usage issue that occurs when a runner becomes defunct while still in use. The change prevents the scheduler from entering a tight loop by only scheduling an immediate check for defunct runners that are also unused. I've suggested a minor refactoring to improve the code's clarity and structure, but the core logic of your fix is sound.

@doringeman
Copy link
Copy Markdown
Contributor

Have you been able to reproduce the issue @ericcurtin?

@doringeman
Copy link
Copy Markdown
Contributor

I don't think it will fix it. It'll postpone the eviction of a runner that's unusable if there are more references to it.

@ericcurtin
Copy link
Copy Markdown
Contributor Author

I didn't reproduce, closing...

@ericcurtin ericcurtin closed this Oct 20, 2025
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.

3 participants