Fix InProcessRuntime CPU hot loop by adding delay when queue is empty #7079
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why are these changes needed?
The
InProcessRuntime.RunAsync()
method was continuously spinning in a tight loop checking the message queue, causing 100% CPU usage on one core even when idle. This PR adds a 100ms delay when the queue is empty to significantly reduce CPU consumption while maintaining reasonable message processing latency.Related issue number
Closes #7078
Changes
RunAsync()
loopTask.Delay()
when queue is empty to prevent hot loopPerformance Impact
Technical Details
The fix uses
Task.Delay()
with the cancellation token to:Alternative approaches considered:
100ms provides a good balance between CPU efficiency and responsiveness for typical agent workflows.
Checks
Testing
The fix has been tested locally and verified to:
RunUntilIdleAsync()
methodBranch Information
fix/inprocess-runtime-cpu-hotloop
saberingroup/autogen
microsoft/autogen
main branchFiles Changed
dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs