home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
70.77k stars 29.53k forks source link

` async_listen_once` event start/cancel #111796

Closed Anto79-ops closed 5 months ago

Anto79-ops commented 6 months ago

The problem

hello,

updating to 2024.3.0b0 and this logs appears

2024-02-28 15:45:20.592 ERROR (MainThread) [homeassistant.core] Unable to remove unknown job listener (<Job onetime listen homeassistant_started functools.partial(<bound method ConfigEntry._async_setup_again of <ConfigEntry entry_id=daea4f025b75e7641e83b7572864cc2a version=1 domain=hue title=Hue Bridge ecb5fa9d04a6 state=ConfigEntryState.SETUP_RETRY unique_id=ecb5fa9d04a6>>, <HomeAssistant NOT_RUNNING>) HassJobType.Callback <_OneTimeListener functools:functools.partial(<bound method ConfigEntry._async_setup_again of <ConfigEntry entry_id=daea4f025b75e7641e83b7572864cc2a version=1 domain=hue title=Hue Bridge ecb5fa9d04a6 state=ConfigEntryState.SETUP_RETRY unique_id=ecb5fa9d04a6>>, <HomeAssistant RUNNING>)>>, None, False)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 1407, in _async_remove_listener
    self._listeners[event_type].remove(filterable_job)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: 'homeassistant_started'

CC @bd

What version of Home Assistant Core has the issue?

2024.3.0b0

What was the last working version of Home Assistant Core?

2024.2.x

What type of installation are you running?

Home Assistant OS

Integration causing the issue

No response

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

bdraco commented 6 months ago

Its likely

homeassistant/config_entries.py-                self._async_cancel_retry_setup = hass.bus.async_listen_once(
homeassistant/config_entries.py:                    EVENT_HOMEASSISTANT_STARTED,
homeassistant/config_entries.py-                    functools.partial(self._async_setup_again, hass),
homeassistant/config_entries.py-                )

or

homeassistant/helpers/entity_platform.py-                    self._async_cancel_retry_setup = hass.bus.async_listen_once(
homeassistant/helpers/entity_platform.py:                        EVENT_HOMEASSISTANT_STARTED, setup_again
homeassistant/helpers/entity_platform.py-                    )

Being canceled after its fired

bdraco commented 5 months ago

There is a race because the coro is scheduled in the event loop the call can't unset self._async_cancel_retry_setup soon enough because the task doesn't run eagerly