Fix CPU-pegging busy-spin in Stop()#180
Conversation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
📝 WalkthroughWalkthroughThe change introduces a 10-millisecond sleep in the Stop method's loop when running processes are detected. Previously, the loop would spin tightly whilst rechecking the process count. The addition of the sleep reduces CPU overhead during the waiting period between iterations. Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes This is a straightforward addition of a single sleep statement with minimal logic complexity. The change is homogeneous, affecting only one location, and introduces no structural modifications or conditional logic. Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
workflow.go (1)
350-351: Prefer the workflow clock over wall-clock sleep.Line 351 uses
time.Sleep, which bypassesWorkflow.clockand makesStop()harder to drive deterministically in tests. Given this type already abstracts time throughclock.Clock(and the interface providesSleep(time.Duration)), it would be cleaner to sleep via the injected clock here too.♻️ Proposed change
- time.Sleep(10 * time.Millisecond) + w.clock.Sleep(10 * time.Millisecond)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@workflow.go` around lines 350 - 351, The code uses time.Sleep(10 * time.Millisecond) which bypasses the workflow's injected clock; replace that call with the workflow clock's Sleep method (e.g., w.clock.Sleep(10 * time.Millisecond) or wf.clock.Sleep(...)) so tests can drive time deterministically via Workflow.clock; ensure the package still imports the clock package as needed and remove or keep the time import accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@workflow.go`:
- Around line 350-351: The code uses time.Sleep(10 * time.Millisecond) which
bypasses the workflow's injected clock; replace that call with the workflow
clock's Sleep method (e.g., w.clock.Sleep(10 * time.Millisecond) or
wf.clock.Sleep(...)) so tests can drive time deterministically via
Workflow.clock; ensure the package still imports the clock package as needed and
remove or keep the time import accordingly.
Problem
Stop()polls process states in a tightfor {}loop with no yield or sleep, consuming 100% of a CPU core during shutdown.Why
In services with multiple workflows, this multiplies — each
Stop()call pins a core until all goroutines exit.Fix
Add a short sleep between poll iterations to yield the CPU while still shutting down promptly.
🤖 Generated with Claude Code
Summary by CodeRabbit