Fix high CPU usage when idle (#2001)#2044
Open
Huangting-xy wants to merge 1 commit intosipeed:mainfrom
Open
Conversation
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]>
759efe9 to
f7708bd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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:
Missing
gokeyword (line 416)func() { ... }go, so the function body was never executedBusy-wait loop (default branch)
The select statement had a
defaultcase withtime.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
gokeyword before the anonymous function to spawn message processing goroutinedefaultbranch to make select fully blockingctx.Done()is cancelledInboundChan()Test plan
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.