Skip to content

Retry httpcore RemoteProtocolError in SpanPanelApi#1

Merged
NickBorgersOnLowSecurityNode merged 1 commit intomainfrom
codex/open-pr-to-resolve-issue-156
Dec 30, 2025
Merged

Retry httpcore RemoteProtocolError in SpanPanelApi#1
NickBorgersOnLowSecurityNode merged 1 commit intomainfrom
codex/open-pr-to-resolve-issue-156

Conversation

@NickBorgersOnLowSecurityNode
Copy link
Owner

@NickBorgersOnLowSecurityNode NickBorgersOnLowSecurityNode commented Dec 30, 2025

This seeks to close SpanPanel#156

Motivation

  • The integration experienced intermittent RemoteProtocolError: Server disconnected without sending a response caused by httpx/httpcore connection-pool reuse of server-closed keep-alive connections.
  • These transient protocol disconnects caused panels to be marked offline and generated aggressive retry traffic and HA event-loop pressure.
  • The intent is to recover transparently from this client-side race by recreating the client and retrying the operation once on protocol disconnects.
  • This mirrors fixes applied in other Home Assistant integrations and avoids disabling keep-alive globally.

Description

  • Add handling for httpcore.RemoteProtocolError by importing RemoteProtocolError and adding helper methods async def _recreate_client and async def _call_with_protocol_retry to recreate the SpanPanelClient and retry once.
  • Wrap critical client operations (authenticate, get_status, get_panel_state, get_circuits, get_storage_soe, set_circuit_relay, set_circuit_priority, and get_all_data) with _call_with_protocol_retry to perform a single recreate-and-retry on protocol disconnects.
  • Convert the low-level protocol retry into a SpanPanelConnectionError if the retry also fails to provide a consistent error surface.
  • Add unit tests test_get_all_data_retries_on_remote_protocol_error and test_get_all_data_remote_protocol_error_after_retry to tests/test_span_panel_api.py to validate retry and failure-after-retry behavior.

Testing

  • Added unit tests in tests/test_span_panel_api.py covering successful retry and failure-after-retry of get_all_data when httpcore.RemoteProtocolError is raised.
  • Attempted to run PYENV_VERSION=3.11.12 pyenv exec python -m pytest tests/test_span_panel_api.py in the local environment which failed with ModuleNotFoundError: No module named 'span_panel_api' due to the external span-panel-api dependency not being available here.
  • No additional automated test suites were executed in this environment because the test dependency prevented running the new tests.

Codex Task

@NickBorgers
Copy link
Collaborator

Testing Performed

I tested the changes on this branch and verified the implementation works correctly.

Test Results

API Tests (54 passed)

tests/test_span_panel_api.py - 54 passed in 0.36s

The two new tests added by this PR both pass:

  • test_get_all_data_retries_on_remote_protocol_error - Verifies that a single RemoteProtocolError is recovered by recreating the client and retrying
  • test_get_all_data_remote_protocol_error_after_retry - Verifies that persistent errors after retry are converted to SpanPanelConnectionError

Full Test Suite (221 passed, 1 skipped)
All other tests in the repository pass without regression.

Linting

ruff check - All checks passed!

Implementation Verification

The retry logic is correctly implemented:

  1. Import: RemoteProtocolError is properly imported from httpcore
  2. _recreate_client method: Safely closes existing client and creates a new one with stored parameters
  3. _call_with_protocol_retry method: Wraps operations with a single retry on protocol disconnect, converting to SpanPanelConnectionError if the retry also fails
  4. Coverage: The retry logic is applied to all critical client operations:
    • authenticate
    • get_status
    • get_panel_state
    • get_circuits
    • get_storage_soe
    • set_circuit_relay
    • set_circuit_priority
    • get_all_data

The implementation aligns with the goal of transparently recovering from transient RemoteProtocolError: Server disconnected without sending a response errors caused by httpx/httpcore connection-pool reuse of server-closed keep-alive connections.

@NickBorgersOnLowSecurityNode NickBorgersOnLowSecurityNode deleted the codex/open-pr-to-resolve-issue-156 branch December 30, 2025 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add retry logic for httpx RemoteProtocolError: Server disconnected without sending a response

2 participants