custom-components / nordpool

This component allows you to pull in the energy prices into Home-Assistant.
467 stars 112 forks source link

tz.gettz() causes a blocking call in HA event loop #406

Closed magicus closed 2 weeks ago

magicus commented 2 months ago

At line 110 in nordpool/aio_price.py, a call is made to tz.gettz. This in turn calls open to read from a suitable file under/usr/share/zoneinfo. This call to open is potentially blocking, and since this is done in the event loop, Home Assistant is detecting this and complaining about it.

2024-09-11 20:10:18.460 WARNING (MainThread) [homeassistant.util.loop] Detected blocking call to open with args ('/usr/share/zoneinfo/Europe/Stockholm', 'rb') inside the event loop by custom integration 'nordpool' at custom_components/nordpool/aio_price.py, line 110: zone = tz.gettz(zone) (offender: /usr/local/lib/python3.12/site-packages/dateutil/tz/tz.py, line 464: fileobj = open(fileobj, 'rb')), please create a bug report at https://github.com/custom-components/nordpool/issues
For developers, please see https://developers.home-assistant.io/docs/asyncio_blocking_operations/#open
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/usr/src/homeassistant/homeassistant/__main__.py", line 223, in <module>
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 189, in run
return loop.run_until_complete(setup_and_run_hass(runtime_config))
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 674, in run_until_complete
self.run_forever()
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 641, in run_forever
self._run_once()
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 1990, 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/helpers/entity_platform.py", line 644, in async_add_entities
await add_func(coros, entities, timeout)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 595, in _async_add_entities
await coro
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 909, in _async_add_entity
await entity.add_to_platform_finish()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1365, in add_to_platform_finish
await self.async_added_to_hass()
File "/config/custom_components/nordpool/sensor.py", line 518, in async_added_to_hass
await self.handle_new_hr()
File "/config/custom_components/nordpool/sensor.py", line 480, in handle_new_hr
today = await self._api.today(self._area, self._currency)
File "/config/custom_components/nordpool/__init__.py", line 110, in today
return await self._someday(area, currency, "today")
File "/config/custom_components/nordpool/__init__.py", line 99, in _someday
await self.update_today(None)
File "/config/custom_components/nordpool/__init__.py", line 80, in update_today
await self._update("today")
File "/config/custom_components/nordpool/__init__.py", line 73, in _update
data = await spot.hourly(end_date=dt)
File "/config/custom_components/nordpool/aio_price.py", line 228, in hourly
return await self.fetch(self.HOURLY, end_date, areas)
File "/usr/local/lib/python3.12/site-packages/backoff/_async.py", line 151, in retry
ret = await target(*args, **kwargs)
File "/config/custom_components/nordpool/aio_price.py", line 222, in fetch
return join_result_for_correct_time(raw, end_date)
File "/config/custom_components/nordpool/aio_price.py", line 110, in join_result_for_correct_time
zone = tz.gettz(zone)