- 
                Notifications
    You must be signed in to change notification settings 
- Fork 7.8k
Description
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:
- Create an InProcessRuntimeas a hosted service
builder.Services.AddHostedService<InProcessRuntime>- Observe CPU usage with no messages being processed
- 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
 
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