opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix(tui): add retry backoff for event subscription to prevent CPU spin

Open SunandRiver opened this issue 6 days ago • 4 comments

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:

  1. Run opencode spawn
  2. Kill the spawn process (Ctrl+C or kill)
  3. 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

SunandRiver avatar Jan 09 '26 11:01 SunandRiver