Skip to content

Conversation

JohnBrandt00
Copy link

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

  • Added check for empty message delivery queue in RunAsync() loop
  • Added 100ms Task.Delay() when queue is empty to prevent hot loop
  • Delay properly respects cancellation token

Performance Impact

  • Before: 100% CPU usage on one core when idle
  • After: Near-zero CPU usage when idle
  • Message Latency: Maximum 100ms delay to detect new messages (acceptable for most scenarios)

Technical Details

The fix uses Task.Delay() with the cancellation token to:

  1. Prevent busy-waiting when no messages are present
  2. Wake up periodically (every 100ms) to check for new messages
  3. Immediately process messages when they arrive
  4. Respect cancellation for clean shutdown

Alternative approaches considered:

  • SemaphoreSlim signaling: More complex, had blocking issues
  • Shorter delay (10ms): Higher CPU, better for interactive scenarios
  • Longer delay (1000ms): Lower CPU, worse for responsiveness

100ms provides a good balance between CPU efficiency and responsiveness for typical agent workflows.

Checks

  • I've included any doc changes needed (none required - internal implementation change)
  • I've added tests (existing tests cover the functionality)
  • I've made sure all auto checks have passed

Testing

The fix has been tested locally and verified to:

  • Reduce CPU usage from 100% to <1% when idle
  • Maintain proper message processing when messages arrive
  • Respect cancellation token for clean shutdown
  • Work correctly with RunUntilIdleAsync() method

Branch Information

  • Branch name: fix/inprocess-runtime-cpu-hotloop
  • Fork: saberingroup/autogen
  • Target: microsoft/autogen main branch
  • Commit: 3a62c19

Files Changed

  • dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs
    • Added 7 lines to check for empty queue and add delay

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.
@ekzhu ekzhu requested a review from lokitoth October 10, 2025 17:10
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.

InProcessRuntime CPU Hot Loop Issue

1 participant