fix: reuse httpx.AsyncClient to prevent blocking SSL calls#1
Open
Teraformerrr wants to merge 1 commit intofingltd:mainfrom
Open
fix: reuse httpx.AsyncClient to prevent blocking SSL calls#1Teraformerrr wants to merge 1 commit intofingltd:mainfrom
Teraformerrr wants to merge 1 commit intofingltd:mainfrom
Conversation
Each API call previously created a new httpx.AsyncClient(), which initializes a new SSL context and calls load_verify_locations() - a blocking I/O operation. When used inside an async event loop (such as Home Assistant), this blocks the entire loop and causes timeouts. This fix reuses a single AsyncClient instance across requests, adds a close() method for cleanup, and adds async context manager support.
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.
What does this PR do?
Reuses a single
httpx.AsyncClientinstance instead of creating a new one per API call.Problem
Each call to
get_devices(),get_contacts(), orget_agent_info()creates a newhttpx.AsyncClient(). Every new client initializes a fresh SSL context, which callsssl.SSLContext.load_verify_locations()— a synchronous blocking I/O operation that reads certificate files from disk.When used inside an async event loop (e.g., Home Assistant), this blocks the entire event loop and causes:
Detected blocking call to load_verify_locationswarningsRelated Home Assistant issues:
Changes
httpx.AsyncClientacross all requestsclose()method for proper client cleanup__aenter__/__aexit__for async context manager support