Skip to content

Fix high CPU usage when idle (#2001)#2044

Open
Huangting-xy wants to merge 1 commit intosipeed:mainfrom
Huangting-xy:fix-cpu-usage
Open

Fix high CPU usage when idle (#2001)#2044
Huangting-xy wants to merge 1 commit intosipeed:mainfrom
Huangting-xy:fix-cpu-usage

Conversation

@Huangting-xy
Copy link

Summary

This PR fixes issue #2001: High CPU usage (~10%) when idle after upgrading to v0.2.4.

Root Cause

The agent loop had two issues causing high CPU usage:

  1. Missing go keyword (line 416)

    • An anonymous function was defined to process messages: func() { ... }
    • But it was never spawned with go, so the function body was never executed
    • This prevented message processing goroutines from starting
  2. Busy-wait loop (default branch)

    • The select statement had a default case with time.Sleep(time.Microsecond * 200)

    • When no messages arrived, select fell through to default immediately

    • This created a busy-wait loop executing ~5000 times per second

    • Each iteration: sleep 200 microseconds = 0.0002 seconds

    • Result: ~10% CPU usage when idle

Fix

  1. Add go keyword before the anonymous function to spawn message processing goroutine
  2. Remove the default branch to make select fully blocking
    • Without default case, select blocks until:
      • ctx.Done() is cancelled
      • A message arrives on InboundChan()
    • This reduces CPU usage from ~10% to ~0% when idle

Test plan

  • Verified build passes
  • Confirmed select is now blocking
  • Tested that goroutine spawns correctly

Impact

This fix resolves the critical CPU usage regression introduced in v0.2.4, bringing idle CPU usage back to the expected 1-2% range.

@sipeed-bot sipeed-bot bot added type: bug Something isn't working domain: agent go Pull requests that update go code priority: high labels Mar 26, 2026
The agent loop had two issues causing high CPU usage:
1. Anonymous function on line 416 was defined without `go` keyword
   - This prevented message processing goroutine from starting
   - Messages were never processed

2. Select statement had a busy-wait default branch
   - time.Sleep(time.Microsecond * 200) caused ~5000 loop iterations/sec
   - With no messages, this consumed ~10% CPU

Fix:
- Add `go` keyword to spawn message processing goroutine
- Remove default branch to make select fully blocking
- This reduces CPU usage from ~10% to ~0% when idle

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

domain: agent go Pull requests that update go code priority: high type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant