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
69.03k stars 28.28k forks source link

Fix non-thread-safe operations in amcrest #116859

Closed bdraco closed 1 week ago

bdraco commented 1 week ago

Proposed change

I do not have a way to test this. Please carefully double check this fix.

amcrest called async_dispatcher_send from a thread

fixes #116850

Type of change

Additional information

Checklist

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

To help with the load of incoming pull requests:

home-assistant[bot] commented 1 week ago

Hey there @flacjacket, mind taking a look at this pull request as it has been labeled with an integration (amcrest) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `amcrest` can trigger bot actions by commenting: - `@home-assistant close` Closes the pull request. - `@home-assistant rename Awesome new title` Renames the pull request. - `@home-assistant reopen` Reopen the pull request. - `@home-assistant unassign amcrest` Removes the current integration label and assignees on the pull request, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.
bdraco commented 1 week ago

It turns out I had one in storage in the ONVIF box

These are really noisy when they go offline

2024-05-05 15:40:13.472 ERROR (MainThread) [homeassistant.components.amcrest.binary_sensor] Could not update Amcrest Camera Audio Detected binary sensor due to error: CommError
2024-05-05 15:40:14.270 DEBUG (MainThread) [homeassistant.components.amcrest.camera] Updating Amcrest Camera camera
2024-05-05 15:40:15.291 WARNING (MainThread) [amcrest.http] <AMC058E4_22D5C1:AMC058E46C1C22D5C1> Trying again due to error: ConnectTimeout('')
2024-05-05 15:40:15.703 WARNING (MainThread) [homeassistant.helpers.update_coordinator] First refresh for openweathermap failed, retrying: success=False last_exception=Invalid API Key provided
2024-05-05 15:40:16.370 DEBUG (MainThread) [homeassistant.components.amcrest.binary_sensor] Updating Amcrest Camera Audio Detected binary sensor
2024-05-05 15:40:19.238 DEBUG (MainThread) [homeassistant.components.amcrest.camera] Take snapshot from Amcrest Camera
2024-05-05 15:40:19.238 DEBUG (MainThread) [homeassistant.components.amcrest.camera] Waiting for previous snapshot from Amcrest Camera
2024-05-05 15:40:20.327 WARNING (MainThread) [amcrest.http] <AMC058E4_22D5C1:AMC058E46C1C22D5C1> Trying again due to error: ConnectTimeout('')
2024-05-05 15:40:20.328 WARNING (MainThread) [amcrest.http] <AMC058E4_22D5C1:AMC058E46C1C22D5C1> Trying again due to error: ConnectTimeout('')
2024-05-05 15:40:20.328 WARNING (MainThread) [amcrest.http] <AMC058E4_22D5C1:AMC058E46C1C22D5C1> Trying again due to error: ConnectTimeout('')
2024-05-05 15:40:20.328 WARNING (MainThread) [amcrest.http] <AMC058E4_22D5C1:AMC058E46C1C22D5C1> Trying again due to error: ConnectTimeout('')
2024-05-05 15:40:20.329 WARNING (MainThread) [amcrest.http] <AMC058E4_22D5C1:AMC058E46C1C22D5C1> Trying again due to error: ConnectTimeout('')
2024-05-05 15:40:20.329 WARNING (MainThread) [amcrest.http] <AMC058E4_22D5C1:AMC058E46C1C22D5C1> Trying again due to error: ConnectTimeout('')
2024-05-05 15:40:21.343 DEBUG (MainThread) [homeassistant.components.amcrest] Amcrest Camera camera errs: 5
2024-05-05 15:40:21.343 ERROR (MainThread) [homeassistant.components.amcrest.camera] Could not get image from Amcrest Camera camera due to error: CommError
2024-05-05 15:40:21.344 ERROR (MainThread) [homeassistant.core] async_create_task with <coroutine object AmcrestCam._async_get_image at 0x36e4f7880> (None)
2024-05-05 15:40:21.371 WARNING (MainThread) [homeassistant.components.binary_sensor] Updating amcrest binary_sensor took longer than the scheduled update interval 0:00:05
2024-05-05 15:40:22.422 WARNING (MainThread) [amcrest.http] <AMC058E4_22D5C1:AMC058E46C1C22D5C1> Trying again due to error: ConnectTimeout('')
2024-05-05 15:40:24.270 WARNING (MainThread) [homeassistant.helpers.entity] Update of camera.amcrest_camera is taking over 10 seconds
2024-05-05 15:40:26.370 WARNING (MainThread) [homeassistant.helpers.entity] Update of binary_sensor.amcrest_camera_audio_detected is taking over 10 seconds
2024-05-05 15:40:26.371 WARNING (MainThread) [homeassistant.components.binary_sensor] Updating amcrest binary_sensor took longer than the scheduled update interval 0:00:05
2024-05-05 15:40:26.383 DEBUG (MainThread) [homeassistant.components.amcrest] Amcrest Camera camera errs: 6
2024-05-05 15:40:26.383 ERROR (MainThread) [homeassistant.components.amcrest] Amcrest Camera camera offline: Too many errors
2024-05-05 15:40:26.390 DEBUG (MainThread) [homeassistant.components.amcrest.binary_sensor] Updating Amcrest Camera Online binary sensor
2024-05-05 15:40:26.394 ERROR (MainThread) [homeassistant.core] async_create_task with <coroutine object Entity.async_update_ha_state at 0x3712a3880> (Entity schedule update ha state camera.amcrest_camera)
2024-05-05 15:40:26.396 DEBUG (MainThread) [homeassistant.components.amcrest] Amcrest Camera camera errs: 7
2024-05-05 15:40:26.396 DEBUG (MainThread) [homeassistant.components.amcrest] Amcrest Camera camera errs: 8
2024-05-05 15:40:26.396 DEBUG (MainThread) [homeassistant.components.amcrest] Amcrest Camera camera errs: 9
2024-05-05 15:40:26.396 DEBUG (MainThread) [homeassistant.components.amcrest] Amcrest Camera camera errs: 10
2024-05-05 15:40:26.396 DEBUG (MainThread) [homeassistant.components.amcrest] Amcrest Camera camera errs: 11
2024-05-05 15:40:26.397 ERROR (MainThread) [homeassistant.components.amcrest.camera] Could not get Amcrest Camera camera attributes due to error: CommError
2024-05-05 15:40:27.397 WARNING (MainThread) [amcrest.http] <AMC058E4_22D5C1:AMC058E46C1C22D5C1> Trying again due to error: ConnectTimeout('')
2024-05-05 15:40:28.475 DEBUG (MainThread) [homeassistant.components.amcrest] Amcrest Camera camera errs: 12
2024-05-05 15:40:28.475 ERROR (MainThread) [homeassistant.components.amcrest.binary_sensor] Could not update Amcrest Camera Audio Detected binary sensor due to error: CommError
2024-05-05 15:40:29.239 DEBUG (MainThread) [homeassistant.components.amcrest.camera] Take snapshot from Amcrest Camera
2024-05-05 15:40:29.239 WARNING (MainThread) [homeassistant.components.amcrest.camera] Attempt to take snapshot when Amcrest Camera camera is offline
bdraco commented 1 week ago
Screenshot 2024-05-05 at 3 39 52 PM

online works

bdraco commented 1 week ago
Screenshot 2024-05-05 at 3 40 43 PM

offline works

bdraco commented 1 week ago

comes back online ok as well

Screenshot 2024-05-05 at 3 42 34 PM Screenshot 2024-05-05 at 3 42 39 PM
bdraco commented 1 week ago

No more thread safety issue with asyncio debug turned on and Home Assistant debug turned on

paludi commented 1 week ago

Thanks a lot for the quick fix @bdraco, I can confirm it fixes the issue mentioned in #116850.