fix: reuse httpx.AsyncClient to prevent blocking SSL calls#1
fix: reuse httpx.AsyncClient to prevent blocking SSL calls#1Teraformerrr 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.
|
Hi @Teraformerrr , thanks for opening this PR and for the detailed analysis of the SSL blocking issue! You were spot on, creating a new AsyncClient per request was causing the event loop to block on load_verify_locations(). We've just shipped v1.1.0 which addresses this along with several other fixes. Our approach is slightly different: instead of managing the client lifecycle internally, we now accept an optional httpx.AsyncClient parameter in the constructor: FingAgent(ip, port, key, client=get_async_client(hass)) I'm going to close this PR since the fix is already in, but really appreciate you taking the time to diagnose the root cause and reference the HA issues (home-assistant/core#164052, home-assistant/core#156513). That context was very helpful! |
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