Skip to content

fix(glances): use dedicated httpx client with longer timeout and surface API error messages#169689

Draft
serl wants to merge 2 commits intohome-assistant:devfrom
serl:fix/glances-http-timeout-and-error-message
Draft

fix(glances): use dedicated httpx client with longer timeout and surface API error messages#169689
serl wants to merge 2 commits intohome-assistant:devfrom
serl:fix/glances-http-timeout-and-error-message

Conversation

@serl
Copy link
Copy Markdown

@serl serl commented May 3, 2026

Proposed change

The Glances integration was using the shared httpx client via get_async_client, which is hardcoded to a 5-second timeout. On hosts where Glances takes more than 5 seconds to respond (reported in #169113 with worst-case fetches around 5 seconds), every refresh would time out and the integration would constantly flap between available and unavailable.

This PR switches Glances to its own dedicated create_async_httpx_client(hass, verify_ssl=..., timeout=DEFAULT_TIMEOUT). DEFAULT_TIMEOUT is set to 30 seconds — well below the 60-second DEFAULT_SCAN_INTERVAL, and roughly 6x the user's reported worst-case fetch time.

While investigating, a secondary bug surfaced: coordinator.py did raise UpdateFailed from err (no message), which caused DataUpdateCoordinator's ERROR log line to be empty whenever a fetch failed — making it very hard for users to diagnose the timeouts in the first place. Fixed by passing str(err) to UpdateFailed.

A new test (test_update_error_includes_message) covers the coordinator update path (only initial setup was previously tested) and asserts the underlying error message is propagated to UpdateFailed.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

cc @engrbm87 (codeowner)

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

…ace API error messages

The integration shared the global Home Assistant httpx client which
defaults to a 5s timeout. On busy hosts a typical Glances fetch already
takes 3.8-4.9s, so any momentary slowness tripped the timeout and the
sensors went unavailable.

Use create_async_httpx_client to get a dedicated client with a 30s
timeout (mutating the shared client's timeout would affect every other
integration).

Also propagate the underlying GlancesApiError message into UpdateFailed
so the coordinator's ERROR log line is no longer empty when a fetch
fails.

Closes home-assistant#169113

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 3, 2026 13:46
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @serl

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented May 3, 2026

Hey there @engrbm87, mind taking a look at this pull request as it has been labeled with an integration (glances) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of glances can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign glances Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copy link
Copy Markdown
Contributor

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 updates the Glances integration to use its own httpx client with a longer timeout and to preserve underlying API error text in coordinator failures, so slow Glances instances stop flapping unavailable and users get actionable logs.

Changes:

  • Switch Glances setup from the shared Home Assistant httpx client to a dedicated client configured with a 30-second timeout.
  • Propagate GlancesApiError text into UpdateFailed so coordinator error logs are no longer empty.
  • Add a coordinator test that verifies update failures keep the original exception message.

Reviewed changes

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

File Description
homeassistant/components/glances/__init__.py Creates the Glances API client with a dedicated httpx client and custom timeout during entry setup.
homeassistant/components/glances/coordinator.py Preserves the original API exception text when converting fetch failures to UpdateFailed.
homeassistant/components/glances/const.py Adds the new default timeout constant used by the integration client.
tests/components/glances/test_init.py Adds coverage for the coordinator update-failure path and imported test helpers/constants.

Comment on lines +69 to +70
httpx_client = create_async_httpx_client(
hass, verify_ssl=entry_data[CONF_VERIFY_SSL], timeout=DEFAULT_TIMEOUT
Comment on lines +58 to +67
async def test_update_error_includes_message(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_api: MagicMock,
) -> None:
"""Test that the underlying API error message is propagated to UpdateFailed."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_INPUT)
entry.add_to_hass(hass)

await hass.config_entries.async_setup(entry.entry_id)
… test

Address PR review feedback:

- Cache the per-verify_ssl httpx client in hass.data so config entry
  reloads reuse the same client instead of leaking a new one (and a new
  EVENT_HOMEASSISTANT_CLOSE handler) on every reload.
- Add a regression test that asserts setup constructs the client with
  the configured DEFAULT_TIMEOUT and that a reload hits the cache
  (no second client created).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

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

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Glances integration fails intermittently due to 5-second HTTP timeout

2 participants