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

Blocking call to open inside event loop warnings on 2024.6 dev #217

Open
cogneato opened this issue May 17, 2024 · 4 comments
Open

Blocking call to open inside event loop warnings on 2024.6 dev #217

cogneato opened this issue May 17, 2024 · 4 comments

Comments

@cogneato
Copy link

Description:

Just a heads up, Home Assistant has been trying to highlight event loop blocking issues across the board in order to improve performance and responsiveness. These warnings were not appearing in 2024.5.3 but are showing up in the current dev build for a couple core integrations and extended_openai on my installation.

Environment:

• Core Version: core-2024.6.0.dev202405170216
• System: HAOS

Issue Details:

I see the following blocking calls inside the event loop related to the extended_openai_conversation custom integration:

Blocking Call to platform.py:

Detected blocking call to open inside the event loop by custom integration 'extended_openai_conversation' at custom_components/extended_openai_conversation/helpers.py, line 150: await client.models.list(timeout=10) (offender: /usr/local/lib/python3.12/platform.py, line 199: with open(executable, 'rb') as f:), please create a bug report at https://github.com/jekalmin/extended_openai_conversation/issues

Blocking Call to distro.py:

Detected blocking call to open inside the event loop by custom integration 'extended_openai_conversation' at custom_components/extended_openai_conversation/helpers.py, line 150: await client.models.list(timeout=10) (offender: /usr/local/lib/python3.12/site-packages/distro/distro.py, line 1099: with open(self.os_release_file, encoding="utf-8") as release_file:), please create a bug report at https://github.com/jekalmin/extended_openai_conversation/issues

Steps to Reproduce:

  • Update to the latest dev build of Home Assistant (core-2024.6.0.dev202405170216).
@jleinenbach
Copy link
Contributor

This diff for the helpers.py file seems to solve the problem. I'm not a python programmer. So please check it.
The import part belongs to the beginning of the file.

+ import asyncio
+ import concurrent.futures

async def validate_authentication(
    hass: HomeAssistant,
    api_key: str,
    base_url: str,
    api_version: str,
    organization: str = None,
    skip_authentication=False,
) -> None:
    if skip_authentication:
        return

    if is_azure(base_url):
        client = AsyncAzureOpenAI(
            api_key=api_key,
            azure_endpoint=base_url,
            api_version=api_version,
            organization=organization,
        )
    else:
        client = AsyncOpenAI(
            api_key=api_key, base_url=base_url, organization=organization
        )

-    await client.models.list(timeout=10)
+    def list_models_with_timeout():
+        return client.models.list(timeout=10)
+
+    loop = asyncio.get_event_loop()
+    with concurrent.futures.ThreadPoolExecutor() as pool:
+        await loop.run_in_executor(pool, list_models_with_timeout)

As an alternative, search for the function with this name and replace it with:

async def validate_authentication(
    hass: HomeAssistant,
    api_key: str,
    base_url: str,
    api_version: str,
    organization: str = None,
    skip_authentication=False,
) -> None:
    if skip_authentication:
        return

    if is_azure(base_url):
        client = AsyncAzureOpenAI(
            api_key=api_key,
            azure_endpoint=base_url,
            api_version=api_version,
            organization=organization,
        )
    else:
        client = AsyncOpenAI(
            api_key=api_key, base_url=base_url, organization=organization
        )

    def list_models_with_timeout():
        return client.models.list(timeout=10)

    loop = asyncio.get_event_loop()
    with concurrent.futures.ThreadPoolExecutor() as pool:
        await loop.run_in_executor(pool, list_models_with_timeout)

@jleinenbach
Copy link
Contributor

I can still see a warning with a huge list of integrations, including extended_openai_conversation that begins with:

2024-05-21 11:40:19.463 WARNING (MainThread) [homeassistant.core] Something is blocking Home Assistant from wrapping up the start up phase. We're going to continue anyway. Please report the following info at http]s://github.com/home-assistant/core/issues:
[huge list]

@styphonthal
Copy link

I am still having similar issue, running on HaOS, HA core 2024.6.2

2024-06-16 10:12:33.973 WARNING (MainThread) [homeassistant.util.loop] Detected blocking call to open inside the event loop by custom integration 'extended_openai_conversation' at custom_components/extended_openai_conversation/helpers.py, line 150: await client.models.list(timeout=10) (offender: /usr/local/lib/python3.12/site-packages/distro/distro.py, line 1099: with open(self.os_release_file, encoding="utf-8") as release_file:), please create a bug report at https://github.com/jekalmin/extended_openai_conversation/issues
Traceback (most recent call last):
File "", line 198, in _run_module_as_main
File "", line 88, in _run_code
File "/usr/src/homeassistant/homeassistant/main.py", line 223, in
sys.exit(main())
File "/usr/src/homeassistant/homeassistant/main.py", line 209, in main
exit_code = runner.run(runtime_conf)
File "/usr/src/homeassistant/homeassistant/runner.py", line 190, in run
return loop.run_until_complete(setup_and_run_hass(runtime_config))
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 672, in run_until_complete
self.run_forever()
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 639, in run_forever
self._run_once()
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 1988, in _run_once
handle._run()
File "/usr/local/lib/python3.12/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/usr/src/homeassistant/homeassistant/setup.py", line 165, in async_setup_component
result = await _async_setup_component(hass, domain, config)
File "/usr/src/homeassistant/homeassistant/setup.py", line 447, in async_setup_component
await asyncio.gather(
File "/usr/src/homeassistant/homeassistant/setup.py", line 449, in
create_eager_task(
File "/usr/src/homeassistant/homeassistant/util/async
.py", line 37, in create_eager_task
return Task(coro, loop=loop, name=name, eager_start=True)
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 742, in async_setup_locked
await self.async_setup(hass, integration=integration)
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 594, in async_setup
result = await component.async_setup_entry(hass, self)
File "/config/custom_components/extended_openai_conversation/init.py", line 100, in async_setup_entry
await validate_authentication(
File "/config/custom_components/extended_openai_conversation/helpers.py", line 150, in validate_authentication
await client.models.list(timeout=10)

@styphonthal
Copy link

This error is still present as of 6/30/24

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

No branches or pull requests

3 participants