Skip to content

Back off Orbit websocket reconnects#416

Open
ryanr14 wants to merge 3 commits intosebr:mainfrom
ryanr14:codex/backoff-websocket-5xx
Open

Back off Orbit websocket reconnects#416
ryanr14 wants to merge 3 commits intosebr:mainfrom
ryanr14:codex/backoff-websocket-5xx

Conversation

@ryanr14
Copy link
Copy Markdown

@ryanr14 ryanr14 commented Apr 28, 2026

Summary

  • add exponential backoff for Orbit websocket reconnect attempts, capped at 5 minutes
  • handle transient websocket handshake HTTP 5xx responses as warnings instead of generic errors
  • reset the retry delay after a successful websocket connection

Why

When the Orbit websocket endpoint returns transient 5xx responses, the integration retries every 5 seconds and can flood Home Assistant logs while the upstream endpoint is unhealthy. The integration should keep retrying, but progressively slow repeated attempts until the endpoint recovers.

Related: #190

Validation

  • python3 -m py_compile custom_components/bhyve/pybhyve/websocket.py
  • ruff check custom_components/bhyve/pybhyve/websocket.py
  • ruff format --check custom_components/bhyve/pybhyve/websocket.py
  • python -m pytest tests/test_client.py -q (7 passed)
  • Applied the same patch to a Home Assistant 2026.4.4 instance, restarted Core, and confirmed the repeated 5-second Orbit websocket 502 log loop stopped after restart.

@sebr
Copy link
Copy Markdown
Owner

sebr commented Apr 29, 2026

@ryanr14 thanks for the PR - please let me know when it's ready to review (it's still in draft, so I'm assuming work still pending)

@ryanr14 ryanr14 changed the title [codex] Back off Orbit websocket reconnects Back off Orbit websocket reconnects May 1, 2026
@ryanr14 ryanr14 marked this pull request as ready for review May 1, 2026 16:14
Copilot AI review requested due to automatic review settings May 1, 2026 16:14
@ryanr14
Copy link
Copy Markdown
Author

ryanr14 commented May 1, 2026

I think it's ready for review now, @sebr.

I added the backoff behavior plus lifecycle guards so pending reconnect timers are cancelled/ignored when the websocket is intentionally stopped, and thought it would be good to add focused websocket tests for backoff, reset, and stop behavior. Codex helped me do that, to be clear. :)

I did validate with the ruff format/check and the full pytest suite against current main.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the Orbit websocket transport’s reconnect behavior to reduce log spam and load when the upstream websocket endpoint is unhealthy, while continuing to retry until recovery.

Changes:

  • Implement exponential backoff for websocket reconnect attempts, capped at 5 minutes, and reset the delay after a successful connection.
  • Treat transient websocket handshake HTTP 5xx responses as warnings (rather than generic errors).
  • Add targeted unit tests covering backoff behavior, delay reset on success, and reconnect cancellation on stop.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
custom_components/bhyve/pybhyve/websocket.py Adds reconnect backoff state/handle tracking, improves handling/logging for transient handshake failures, and resets backoff on successful connect.
tests/test_websocket.py Introduces tests validating exponential backoff, reconnect delay reset after a successful connection, and safe cancellation of pending reconnects on stop.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants