fix: retry discovery when OIDC endpoint is unreachable at setup#76
Open
matiasjrossi wants to merge 1 commit into
Open
fix: retry discovery when OIDC endpoint is unreachable at setup#76matiasjrossi wants to merge 1 commit into
matiasjrossi wants to merge 1 commit into
Conversation
When Home Assistant starts before the OIDC provider (or DNS) is fully available, _async_prepare_config raises while fetching the discovery document, which causes the integration to fail setup with no automatic retry — only a Home Assistant restart recovers it. This is a common race on reboots when HA is on the same host as the IdP (Authentik, Authelia, Pocket-ID, etc.). Handle the transient failure in both setup paths: - async_setup_entry now wraps the discovery call and raises ConfigEntryNotReady on failure, so Home Assistant retries with its standard exponential backoff instead of marking the entry as dead. - async_setup (YAML path) no longer aborts the integration when initial discovery fails. The YAML data is still forwarded to the config entry import flow without the discovered fields, and async_setup_entry then retries discovery via the same ConfigEntryNotReady path. Refs cavefire#62 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a startup-time race where the integration permanently fails to load if the OIDC discovery endpoint (
configure_url) is briefly unreachable when Home Assistant boots and cached endpoints are not present. At that point, only a manual HA restart recovers. Common on reboots where an HA container shares a host with its IdP and IdP container comes up just after HA. Existing entries with a healthy previous cache are not affected by the boot race because discovery isn't attempted.Same user-facing symptom as #62 ("HA starts before the IdP is ready, login broken until manual restart"), via a different code path post-#70 refactor —
_async_prepare_configraising in the YAML setup path rather thanfetch_urlssilently swallowing the exception. The shared fix is to give Home Assistant'sConfigEntryNotReadyretry loop a chance to run.Changes
custom_components/openid/__init__.py:async_setup_entry— wrap the_async_prepare_configcall and raiseConfigEntryNotReadyon failure. HA then retries with its standard exponential backoff instead of marking the entry dead.async_setup(YAML path) — no longer aborts the integration when initial discovery fails. The YAML data is still forwarded to the config entry import flow without the discovered fields, andasync_setup_entrythen retries discovery via the sameConfigEntryNotReadypath. On a subsequent HA start where YAML discovery succeeds, the import flow updates the entry with the full data as before.Symptom before vs after
Without the patch, this morning's log on my setup:
With the patch, this becomes a
WARNINGand the entry enterssetup_retryuntil the IdP responds.Manual verification
Reproduced on HA 2026.4.4 + Authentik:
.storage/core.config_entries, strippedauthorize_url,token_url,user_info_url,logout_url,use_pkcefrom the openid entry to force discovery.configure_urlat a path Authentik returns404.state=not_loaded. Integration dead until manual HA restart.state=setup_retryreason="Failed to prepare OpenID configuration: Configuration endpoint returned 404". HA's exponential-backoff retry loop engaged.Scope notes
client_id/client_secret, malformed YAML, etc.) is unchanged — those still propagate from the import flow / entry setup as before.Exceptioncatches are intentionally broad to match the existing style inasync_setupand because_async_prepare_configcan raise anything aiohttp throws (ClientError,asyncio.TimeoutError, customRuntimeErrorfrom the non-200 status check) — all of which deserve the same retry treatment.Closes #62
Disclosure: Change was authored using Claude Code, but I understand what it does, directed the tests and the implementation.