Skip to content

InProcessRuntime CPU Hot Loop Issue #7078

@JohnBrandt00

Description

@JohnBrandt00

What happened?

Describe the bug

The InProcessRuntime.RunAsync() method in Microsoft.AutoGen.Core contains a hot loop that continuously checks the message delivery queue without any delay when the queue is empty. This results in 100% CPU usage on one processor core, even when the runtime is idle.

To Reproduce

Steps to reproduce the behavior:

  1. Create an InProcessRuntime as a hosted service
builder.Services.AddHostedService<InProcessRuntime>
  1. Observe CPU usage with no messages being processed
  2. CPU will be at ~100% on one core

Expected behavior

CPU usage should be minimal when idle.

Screenshots

In development, could see high CPU usage while an InProcess runtime was registered

Image

Additional context

The issue is in the RunAsync method in dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs which continuously loops without any delay:

private async Task RunAsync(CancellationToken cancellation)
{
    Dictionary<Guid, Task> pendingTasks = new();
    while (!cancellation.IsCancellationRequested && shouldContinue())
    {
        // Get a unique task id
        Guid taskId;
        do
        {
            taskId = Guid.NewGuid();
        } while (pendingTasks.ContainsKey(taskId));

        // Process message...
        // No delay when queue is empty, causing hot loop
    }
}

Which packages was the bug in?

  • .NET Core (Microsoft.AutoGen.Core)

AutoGen library version

  • .NET 0.4.0 (main branch)

Proposed Solution

Add a small delay when the queue is empty to prevent the hot loop while maintaining reasonable message processing latency:

while (!cancellation.IsCancellationRequested && shouldContinue())
{
    if (this.messageDeliveryQueue.IsEmpty)
    {
        // Wait a bit before checking again to avoid hot loop
        await Task.Delay(100, cancellation);
        continue;
    }
    
    // Process message...
}

This approach:

  • Reduces CPU usage from 100% to near-zero when idle
  • Maintains reasonable responsiveness (checks 10 times per second)
  • Properly respects cancellation token
  • No external dependencies or complex signaling needed

Which packages was the bug in?

.NET Core (Microsoft.AutoGen.Core)

AutoGen library version.

.NET dev (main branch)

Other library version.

No response

Model used

gpt4o, gpt5

Model provider

Azure OpenAI

Other model provider

No response

Python version

None

.NET version

.NET 8

Operating system

Ubuntu

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions