alandtse / alexa_media_player

This is a custom component to allow control of Amazon Alexa devices in Home Assistant using the unofficial Alexa API.
Apache License 2.0
1.44k stars 275 forks source link

non thread-safe operation #2306

Closed tbclark3 closed 1 month ago

tbclark3 commented 2 months ago

IMPORTANT: Please search the issues, including closed issues, and the FAQ before opening a new issue. The template is mandatory; failure to use it will result in issue closure.

Describe the bug

Every day or two I get a runtime error related to non thread-safe operation. Sometimes HA continues and sometimes it becomes unresponsive, spewing out repeated errors. I have update interval set to 10 minutes, but I'm not sure if this error occurs during an attempted update from Amazon. It occurs whether or not an automation has recently sent audio to the speakers.

To Reproduce

Run Alexa media player integration.

Expected behavior

I expect Alexa media player to run without errors.

Screenshots

System details

Logs Please provide logs. Logger: homeassistant Source: helpers/frame.py:197 First occurred: 11:19:55 AM (1 occurrences) Last logged: 11:19:55 AM

Error doing job: Future exception was never retrieved (None) Traceback (most recent call last): File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/alexa_media/sensor.py", line 433, in _trigger_event self.hass.bus.async_fire( File "/usr/src/homeassistant/homeassistant/core.py", line 1505, in async_fire frame.report_non_thread_safe_operation("hass.bus.async_fire") File "/usr/src/homeassistant/homeassistant/helpers/frame.py", line 225, in report_non_thread_safe_operation report( File "/usr/src/homeassistant/homeassistant/helpers/frame.py", line 159, in report _report_integration(what, integration_frame, level, error_if_integration) File "/usr/src/homeassistant/homeassistant/helpers/frame.py", line 197, in _report_integration raise RuntimeError( RuntimeError: Detected that custom integration 'alexa_media' calls hass.bus.async_fire from a thread other than the event loop, which may cause Home Assistant to crash or data to corrupt. For more information, see https://developers.home-assistant.io/docs/asyncio_thread_safety/#hassbusasync_fire at custom_components/alexa_media/sensor.py, line 433: self.hass.bus.async_fire(. Please report it to the author of the 'alexa_media' custom integration. Additional context**

zSprawl commented 1 month ago

I've been seeing this every since I updated to 2024.7.2. The "corrupt" warning is quite concerning.

tbclark3 commented 1 month ago

It concerns me also, and it has actually resulted in crashes for me. The attached patch to sensor.py fixes the problem and doesn't appear to have any adverse effects after running it for about a week. sensor.py.diff.txt

danielbrunt57 commented 1 month ago

The error only occurs when alarms are dismissed or snoozed. I think the correct fix is to decorate _trigger_event(self, time_date) with @callback so it runs in the event loop and then hass.bus.async_fire is correct and HA is once again happy...