Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using get_default_context() for ssl context to avoid blocking call #791

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

iprak
Copy link

@iprak iprak commented Sep 6, 2024

This addresses the I/O blocking warning as recommended in https://developers.home-assistant.io/docs/asyncio_blocking_operations/#load_default_certs
Closes #790

@iprak
Copy link
Author

iprak commented Sep 9, 2024

@ollo69 Please review

@voradortc
Copy link

You should also remove line 15 (import ssl) as it's no longer required.

@@ -27,6 +26,7 @@
import uuid

import aiohttp
from homeassistant.util.ssl import get_default_context
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not make sense because core_async should not depend on home assistant. If in the future this will be implemented as a separate library, cannot have this dependencies.
There is already a way to directly use the aiohttp session created in ha, probably the best solution is to use this as default option (at this moment is available as advanced option during initial configuration)
To implement this in the library in not blocking way we should implement the ha get_default_context and not import the HA method.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the implementation of get_default_context in homeassistant.util.ssl and I couldn't see any significant difference with what the current ha-smartthinq-sensors code is doing 🤔
Maybe the @cache decorator helps somehow?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be, you should check what @cache decorator do...

Copy link

@fgimenezm fgimenezm Sep 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked. It basically adds a thin wrapper to cache the responses if there is another call to the same function with the same parameters https://docs.python.org/3/library/functools.html#functools.cache
but you are kind of doing that already with _get_session()

Looks like HA is not complaining about using ssl.create_default_context(), it's complaining about where it's being used. Unless the session can be created somewhere else outside of the event loop and then re-used here it's going be complicated.

Also, about this PR... isn't it changing the SSL ciphers of the default context for all HA? Isn't that risky?

Do we still need the _LG_SSL_CIPHERS ? Maybe they updated the servers and the defaults work now

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no cipher set in get_default_context so that part should be the same.

Yes, the warning can be resolved by creating the context outside the event loop but I don't think that can be done in the life cycle of an integration initialization and without referencing ha.

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 load_default_certs in HA 2024.9
4 participants