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

When unloading the integration, runtime errors occur #1888

Closed
AdrianGarside opened this issue May 5, 2024 · 13 comments
Closed

When unloading the integration, runtime errors occur #1888

AdrianGarside opened this issue May 5, 2024 · 13 comments

Comments

@AdrianGarside
Copy link
Contributor

2024-05-05 11:36:06.537 DEBUG (MainThread) [custom_components.tuya_local] Unloading entry for device: REDACTED
2024-05-05 11:36:06.538 DEBUG (MainThread) [custom_components.tuya_local.helpers.device_config] Loaded device config simple_dual_switch.yaml
2024-05-05 11:36:06.538 DEBUG (MainThread) [custom_components.tuya_local.device] Stopping monitor loop for Dual switch
2024-05-05 11:36:06.538 ERROR (MainThread) [homeassistant.components.switch] Error while removing entity switch.dual_switch_switch_2
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1353, in async_remove
await self.__async_remove_impl(force_remove)
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1369, in __async_remove_impl
await self.async_will_remove_from_hass()
File "/config/custom_components/tuya_local/helpers/mixin.py", line 111, in async_will_remove_from_hass
await self._device.async_unregister_entity(self)
File "/config/custom_components/tuya_local/device.py", line 195, in async_unregister_entity
await self.async_stop()
File "/config/custom_components/tuya_local/device.py", line 170, in async_stop
await self._refresh_task
TypeError: object Future can't be used in 'await' expression

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 908, in async_reset
await entity.async_remove()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1358, in async_remove
self.__remove_future.set_result(None)
asyncio.exceptions.InvalidStateError: invalid state
2024-05-05 11:36:06.540 INFO (MainThread) [custom_components.tuya_local.device] Deleting device: REDACTED
2024-05-05 11:36:06.540 DEBUG (MainThread) [custom_components.tuya_local.device] Stopping monitor loop for Dual switch
2024-05-05 11:36:06.540 ERROR (MainThread) [homeassistant.config_entries] Error unloading entry Dual switch for tuya_local
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 724, in async_unload
result = await component.async_unload_entry(hass, self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/tuya_local/init.py", line 517, in async_unload_entry
await async_delete_device(hass, config)
File "/config/custom_components/tuya_local/device.py", line 655, in async_delete_device
await hass.data[DOMAIN][device_id]["device"].async_stop()
File "/config/custom_components/tuya_local/device.py", line 170, in async_stop
await self._refresh_task
TypeError: object Future can't be used in 'await' expression
2024-05-05 11:36:06.921 WARNING (MainThread) [custom_components.tuya_local.device] Dual switch receive loop has terminated

@AdrianGarside AdrianGarside added the bug Something isn't working label May 5, 2024
@sweetpants
Copy link

I'm seeing the same after upgrade to 2024.5.2 today. Here are my logs

2024-05-06 18:30:05.727 ERROR (MainThread) [homeassistant.components.lock] Error while removing entity lock.kantoor_1_child_lock
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1378, in async_remove
await self.__async_remove_impl(force_remove)
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1394, in __async_remove_impl
await self.async_will_remove_from_hass()
File "/config/custom_components/tuya_local/helpers/mixin.py", line 111, in async_will_remove_from_hass
await self._device.async_unregister_entity(self)
File "/config/custom_components/tuya_local/device.py", line 195, in async_unregister_entity
await self.async_stop()
File "/config/custom_components/tuya_local/device.py", line 170, in async_stop
await self._refresh_task
TypeError: object Future can't be used in 'await' expression

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 910, in async_reset
await entity.async_remove()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1383, in async_remove
self.__remove_future.set_result(None)
asyncio.exceptions.InvalidStateError: invalid state
2024-05-06 18:30:05.734 INFO (MainThread) [custom_components.tuya_local.device] Deleting device:
2024-05-06 18:30:05.734 DEBUG (MainThread) [custom_components.tuya_local.device] Stopping monitor loop for Kantoor 1
2024-05-06 18:30:05.735 ERROR (MainThread) [homeassistant.config_entries] Error unloading entry Kantoor 1 for tuya_local
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 772, in async_unload
result = await component.async_unload_entry(hass, self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/tuya_local/init.py", line 517, in async_unload_entry
await async_delete_device(hass, config)
File "/config/custom_components/tuya_local/device.py", line 655, in async_delete_device
await hass.data[DOMAIN][device_id]["device"].async_stop()
File "/config/custom_components/tuya_local/device.py", line 170, in async_stop
await self._refresh_task
TypeError: object Future can't be used in 'await' expression

The integration itself says: "Failed to unload"

@sweetpants
Copy link

Hmmm, this seems to be related to the reload workaround using "Home Assistant Core Integration: Reload config entry -> entity-ID". When disabled there are no errors in the log anymore.

@make-all
Copy link
Owner

make-all commented May 7, 2024

That is a trigger, not a cause. The bug will still show up whenever the integration is stopped.

@muzammilk
Copy link

I am seeing this too after upgrade to 2024.5.2 today. The integration itself says ‘Failed to unload’. Anyone has figured out any workaround ?

@AdrianGarside
Copy link
Contributor Author

AdrianGarside commented May 9, 2024

This fixes it but I don't know if it's a correct and complete fix:
Chang:

        if self._refresh_task:
            await self._refresh_task

to

        if self._refresh_task:
            await asyncio.wrap_future(self._refresh_task)

@haroldboom
Copy link

This fixes it but I don't know if it's a correct and complete fix: Chang:

        if self._refresh_task:
            await self._refresh_task

to

        if self._refresh_task:
            await asyncio.wrap_future(self._refresh_task)

Hey mate, I tried this but then my integration won’t reload devices at all

@AdrianGarside
Copy link
Contributor Author

AdrianGarside commented May 10, 2024

For me the reload is slower - sometimes quite a bit slower but it does reload the device seemingly correctly in the logs:
2024-05-09 21:48:56.796 INFO (MainThread) [custom_components.tuya_local.device] Deleting device: 24142854d8bfc0c9dcd0
2024-05-09 21:48:56.797 INFO (MainThread) [custom_components.tuya_local.device] Creating device: 24142854d8bfc0c9dcd0

And if I made local edits to the device yaml it is reflected post-reload.

@haroldboom
Copy link

For me the reload is slower - sometimes quite a bit slower but it does reload the device seemingly correctly in the logs: 2024-05-09 21:48:56.796 INFO (MainThread) [custom_components.tuya_local.device] Deleting device: 24142854d8bfc0c9dcd0 2024-05-09 21:48:56.797 INFO (MainThread) [custom_components.tuya_local.device] Creating device: 24142854d8bfc0c9dcd0

And if I made local edits to the device yaml it is reflected post-reload.

I just tried it again and it worked ok. I’ll try it for a few days and report back. Earlier when it wasn’t working was on a device that was unavailable in HA but was available in the Tuya app and it wouldn’t reload, just hung.

@make-all
Copy link
Owner

I suspect without that change, there may be a conflict causing a failure, and the delay waiting for the device to finish unloading should avoid that

@AdrianGarside
Copy link
Contributor Author

AdrianGarside commented May 10, 2024

ut that change, there may be a conflict causing a failure, and the delay waiting for the device to finish unloading should avoid that

I think with that fix it's going to actually wait on the device so if the device is hung the reload will hang too?

@poldim
Copy link

poldim commented May 10, 2024

The integration itself says: "Failed to unload"

+1 to also having this error

This fixes it but I don't know if it's a correct and complete fix: Chang:

        if self._refresh_task:
            await self._refresh_task

to

        if self._refresh_task:
            await asyncio.wrap_future(self._refresh_task)

This fixed worked for me:
image

@haroldboom
Copy link

Fix works for me also

make-all added a commit that referenced this issue May 15, 2024
async_actually_start was originally written to execute in the event
loop.  Since HA was modified to automatically run non-decorated
coroutines in the executor thread, this has been running in the wrong
thread, making some of its calls non-thread safe.

- Revert previous fix for #1871, #1872 (PR #1876), as it is not needed
when actually_start is running in the event loop, and has a side
effect of causing #1888 (a separate fix for that has been proposed on
the issue, but this revert also avoids that issue).

Issue #1917
@make-all make-all added awaiting confirmation Wating for confirmation the issue is solved unreleased Will be in next release labels May 15, 2024
@make-all
Copy link
Owner

Using the suggested fix from HA dev on #1917 instead of the previous PR #1876 fix for #1871 also seems to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

6 participants