From 3a62c1925443a1d438b8bff5730556a2a1cd71b8 Mon Sep 17 00:00:00 2001 From: John Brandt Date: Fri, 10 Oct 2025 12:38:20 -0400 Subject: [PATCH] Fix InProcessRuntime CPU hot loop by adding delay when queue is empty The RunAsync method was continuously spinning in a tight loop checking the message queue, causing 100% CPU usage on one core. Added a 100ms delay when the queue is empty to significantly reduce CPU consumption while maintaining reasonable message processing latency. --- dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs b/dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs index fc790e9f8bd3..4a17a01b581e 100644 --- a/dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs +++ b/dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs @@ -279,6 +279,13 @@ private async Task RunAsync(CancellationToken cancellation) Dictionary pendingTasks = new(); while (!cancellation.IsCancellationRequested && shouldContinue()) { + if (this.messageDeliveryQueue.IsEmpty) + { + //wait a bit before checking again to avoid hot loop + await Task.Delay(100, cancellation); + continue; + } + // Get a unique task id Guid taskId; do