opencode
opencode copied to clipboard
fix(tui): add retry backoff for event subscription to prevent CPU spin
Problem
When the server shuts down unexpectedly, the TUI attach client enters a tight retry loop causing 100% CPU usage. This happens because the event subscription loop in sdk.tsx lacks error handling and backoff.
Reproduction:
- Run
opencode spawn - Kill the spawn process (Ctrl+C or
kill) - The attach subprocess continues running at 100% CPU, trying to reconnect indefinitely
Solution
- Add try-catch around
sdk.event.subscribe() - Implement exponential backoff: 1s → 2s → 4s → ... → 30s max
- Add random jitter to prevent thundering herd
- Give up after 10 consecutive failures with error message
Testing
Manually tested by killing the server process and observing the attach client behavior - it now backs off exponentially and eventually exits gracefully instead of spinning the CPU.
Fixes #7645