alandtse / alandtse/tesla

Blocking call to httpx.create_ssl_context() in async_setup_entry freezes entire Home Assistant instance

Open
#1,234 2 comments 4 reactions 0 assignees View on GitHub
bug triage
Dominant language
Python
Stars
780
Forks
127
PR merge metrics
No merged PRs in 30d

Description

### Describe the bug

`create_tesla_ssl_context()` (custom_components/tesla_custom/util.py, line 26)
calls `httpx.create_ssl_context()` synchronously from `async_setup_entry`
(__init__.py, line 145). Internally this calls
`ssl.SSLContext.load_verify_locations()`, which is a blocking disk I/O call.
Since this runs directly inside the async event loop (not wrapped in
`hass.async_add_executor_job()` or `asyncio.to_thread()`), it blocks HA's
entire single-threaded event loop for the duration of the call - not just
the Tesla integration.

In my case this coincided with the whole Home Assistant instance becoming
unresponsive (UI not loading / extremely slow) right after startup, requiring
a full reboot. The issue recurred on the next restart, since
`async_setup_entry` runs again on every boot.

### Log

```
Logger: homeassistant.util.loop
Source: util/loop.py:135
First occurred: 16:31:48 (1 occurrence)
Last logged: 16:31:48

Detected blocking call to load_verify_locations with args
(,
'/usr/local/lib/python3.14/site-packages/certifi/cacert.pem', None, None)
inside the event loop by custom integration 'tesla_custom' at
custom_components/tesla_custom/util.py, line 26:
ctx = httpx.create_ssl_context()
(offender: /usr/local/lib/python3.14/ssl.py, line 717:
context.load_verify_locations(cafile, capath, cadata))

Traceback (most recent call last):
File "/config/custom_components/tesla_custom/__init__.py", line 145, in async_setup_entry
tesla_ssl_context = create_tesla_ssl_context()
File "/config/custom_components/tesla_custom/util.py", line 26, in create_tesla_ssl_context
ctx = httpx.create_ssl_context()
```

### Environment

- Integration version: 3.27.0
- teslajsonpy: 3.13.2
- Home Assistant Core: will confirm in a follow-up comment
- Python: 3.14

### Suggested fix

Wrap the SSL context creation in an executor, e.g.:

tesla_ssl_context = await hass.async_add_executor_job(create_tesla_ssl_context)

### Additional context

See Home Assistant's own documentation on this:
https://developers.home-assistant.io/docs/asyncio_blocking_operations/#load_verify_locations

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.