Skip to content

Fix blocking SSL cert load on event loop (#578)#580

Merged
danieldotnl merged 1 commit into
masterfrom
fix/578-blocking-ssl-load
May 13, 2026
Merged

Fix blocking SSL cert load on event loop (#578)#580
danieldotnl merged 1 commit into
masterfrom
fix/578-blocking-ssl-load

Conversation

@danieldotnl
Copy link
Copy Markdown
Owner

@danieldotnl danieldotnl commented May 13, 2026

Summary

  • Closes Warning "Detected blocking call" with v9.0.0 #578 — HA blocking-call detector flagged load_verify_locations at scraper setup.
  • Pass HA's pre-warmed cached ssl.SSLContext (from homeassistant.util.ssl) into httpx.AsyncClient(verify=...) instead of letting httpx build a fresh context synchronously, which read certifi's CA bundle from disk on the event loop.
  • Preserves the v9 per-scraper cookie isolation (still a dedicated httpx.AsyncClient per HttpSession). Going back to get_async_client would have re-broken that.

Why this is a regression in v9.0.0

v8.0.5 used homeassistant.helpers.httpx_client.get_async_client(hass, verify_ssl), which returns HA's shared client built on the cached SSL context — no blocking call ever fired. v9.0.0 commit 8ee795a replaced the shared helper with direct httpx.AsyncClient(verify=bool, ...) to give each scraper its own cookie jar, which reintroduced the cert load on the event loop.

homeassistant.util.ssl pre-warms verifying and no-verify SSL contexts at module-load time precisely so integrations can grab a ready-to-use context without blocking I/O. We use those directly; we don't go through create_async_httpx_client because it wraps aclose with warn_use, which would warn on every HttpSession.async_close().

Test plan

  • All 467 existing tests pass (pytest tests/)
  • New regression test test_init_does_not_load_verify_locations (parametrized over verify_ssl=True/False) asserts ssl.SSLContext.load_verify_locations is not invoked during HttpSession.__init__
  • Manual: load with the reporter's YAML in a HA dev instance and confirm the Detected blocking call to load_verify_locations warning no longer fires at startup or during multiscrape.scrape service calls

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Technical Improvements

    • Enhanced SSL certificate verification handling in HTTP sessions for better integration with security infrastructure.
  • Tests

    • Added regression tests for HTTP session SSL configuration to ensure reliability.

Review Change Stack

Pass HA's pre-warmed cached ssl.SSLContext into httpx.AsyncClient instead
of letting httpx build one synchronously. The v9.0.0 cookie-isolation
refactor replaced get_async_client() with direct AsyncClient construction,
which reintroduced load_verify_locations() on the event loop.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c2bc38e4-9084-49e0-aa53-e8a7b761f7dd

📥 Commits

Reviewing files that changed from the base of the PR and between 29edc60 and 927f597.

📒 Files selected for processing (2)
  • custom_components/multiscrape/http_session.py
  • tests/test_http_session.py

📝 Walkthrough

Walkthrough

HttpSession now constructs its httpx.AsyncClient with pre-warmed SSL contexts from Home Assistant helpers instead of passing a boolean verify_ssl flag, eliminating a blocking call to load_verify_locations during event loop initialization. A regression test verifies the blocking call no longer occurs.

Changes

SSL Context Pre-warming

Layer / File(s) Summary
SSL context import and AsyncClient initialization
custom_components/multiscrape/http_session.py
Import Home Assistant SSL helpers and update HttpSession to compute ssl_ctx based on verify_ssl (pre-warmed client_context() when True, create_no_verify_ssl_context() when False), then pass the context object to httpx.AsyncClient's verify parameter.
Regression test for blocking call prevention
tests/test_http_session.py
Add ssl import and test test_init_does_not_load_verify_locations that parameterizes verify_ssl and verifies ssl.SSLContext.load_verify_locations is not called during HttpSession initialization by patching and asserting the patch was not invoked.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 No more blocking calls, the SSL context now glows,
Pre-warmed by Home Assistant's helpers, the event loop flows,
Async can breathe, the warning's gone away,
Clean SSL handshakes for a trouble-free day!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: fixing a blocking SSL certificate load issue that was occurring on the event loop during HttpSession initialization.
Linked Issues check ✅ Passed The PR successfully addresses issue #578 by replacing httpx.AsyncClient's default SSL context construction with Home Assistant's pre-warmed context, eliminating the blocking ssl.SSLContext.load_verify_locations call on the event loop.
Out of Scope Changes check ✅ Passed All changes are directly scoped to resolving the blocking SSL load issue: the http_session.py modification uses HA's pre-warmed context, and the test addition validates the fix.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/578-blocking-ssl-load

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@danieldotnl danieldotnl merged commit fe1683f into master May 13, 2026
8 of 9 checks passed
@danieldotnl danieldotnl deleted the fix/578-blocking-ssl-load branch May 13, 2026 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warning "Detected blocking call" with v9.0.0

1 participant