Skip to content

Home Assistant Core repeatedly restarts due to RuntimeError: Event loop is closed triggered by custom Aqara Gateway integration #361

@aemediatodd

Description

@aemediatodd

I am experiencing repeated Home Assistant Core restarts triggered by an uncaught exception inside a thread used by a custom MQTT-based integration (aqara_gateway). The exception occurs when the thread attempts to dispatch messages to Home Assistant’s event loop after the loop has already been closed, which results in Home Assistant Core exiting and the Supervisor restarting it.

Below is the exact error:

RuntimeError: Event loop is closed

Full traceback:
ERROR (paho-mqtt-client-) [root] Uncaught thread exception Traceback (most recent call last): File "/usr/local/lib/python3.13/threading.py", line 1043, in _bootstrap_inner self.run() File "/usr/local/lib/python3.13/threading.py", line 994, in run self._target(*self._args, **self._kwargs) File "/usr/local/lib/python3.13/site-packages/paho/mqtt/client.py", line 4523, in _thread_main self.loop_forever(retry_first_connection=True) File "/usr/local/lib/python3.13/site-packages/paho/mqtt/client.py", line 2297, in loop_forever rc = self._loop(timeout) File "/usr/local/lib/python3.13/site-packages/paho/mqtt/client.py", line 1686, in _loop rc = self.loop_read() File "/usr/local/lib/python3.13/site-packages/paho/mqtt/client.py", line 2100, in loop_read rc = self._packet_read() File "/usr/local/lib/python3.13/site-packages/paho_mqtt/client.py", line 3142, in _packet_read rc = self._packet_handle() File "/usr/local/lib/python3.13/site-packages/paho_mqtt/client.py", line 3808, in _packet_handle return self._handle_publish() File "/usr/local/lib/python3.13/site-packages/paho/mqtt/client.py", line 4145, in _handle_publish self._handle_on_message(message) File "/usr/local/lib/python3.13/site-packages/paho_mqtt/client.py", line 4501, in _handle_on_message on_message(self, self._userdata, message) File "/config/custom_components/aqara_gateway/core/gateway.py", line 502, in on_message self.hass.loop.call_soon_threadsafe(self._on_message, msg) File "/usr/local/lib/python3.13/asyncio/base_events.py", line 878, in call_soon_threadsafe self._check_closed() File "/usr/local/lib/python3.13/asyncio/base_events.py", line 556, in _check_closed raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

After the exception, Home Assistant Core immediately exits:
INFO: Home Assistant Core finish process exit code 0 INFO: Home Assistant Core service shutdown

This results in a loop where the Supervisor keeps restarting Home Assistant Core.

Analysis Summary

Based on the traceback and logs:
The crash originates in a background thread created by the paho-mqtt-client library used by the aqara_gateway custom component.
When MQTT messages arrive, the integration attempts to call:
hass.loop.call_soon_threadsafe(...)

But at this moment the Home Assistant event loop has already been shut down.

This raises:
RuntimeError: Event loop is closed

The exception is unhandled inside the integration and propagates to the root thread, causing Home Assistant Core to terminate cleanly with exit code 0, after which the Supervisor restarts it.

This produces a persistent restart loop whenever the integration receives MQTT messages after HA begins shutting down.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions