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

fix: fix infinite loop and memory exhaustion #2589

Merged
merged 1 commit into from
Oct 5, 2024
Merged

Conversation

dpwood
Copy link
Contributor

@dpwood dpwood commented Oct 4, 2024

Iterate on a copy of the existing_serials rather than extend the list being iterated causing infinite iteration

Fixes #2579

Iterate on a copy of the existing_serials rather than extend the list being iterated
@dpwood dpwood changed the title Fixes #2579 - Prevent infinite loop and memory exhaustion fix #2579 - infinite loop and memory exhaustion from 4.13.3 Oct 4, 2024
@dpwood dpwood changed the title fix #2579 - infinite loop and memory exhaustion from 4.13.3 fix: Fixes #2579 - infinite loop and memory exhaustion from 4.13.3 Oct 4, 2024
@danielbrunt57
Copy link
Collaborator

danielbrunt57 commented Oct 4, 2024

@dpwood Are you aware of @jleinenbach's PR #2580 to address #2579?
I was not having an issue with the original code (or even 4.13.3 for that matter) so I'm wondering what conditions are present for an infinite loop to occur.
I implemented your code change, which is the correct way to process a list if you are altering the original list (via extend) within the loop, and my AMP still works so that's a good thing.

@dpwood
Copy link
Contributor Author

dpwood commented Oct 4, 2024

Aware of it but not tried it. It looked like an AI attempt at a solution rather than anything focused. A lot of changes that don't seem to address the actual problem.

I was having the issue very reliably and the mistake seemed clear. This one line fix has resolved it for me.

Your question about conditions required for failure is a good one. I suspect number or types of Alexa devices is relevant? I'll do some more investigating.

@dpwood
Copy link
Contributor Author

dpwood commented Oct 4, 2024

On startup the existing_serials is [], after a few minutes the function re-runs and in my case I have 17 entries before entering the loop, which is the number of devices I have. I then see it loop infinitely and the existing_serials array grows indefinitely.

I suspect the precondition is having a device with an appDeviceList which is somehow self-referencing? The new way of adding to the list is potentially recursive.

@danielbrunt57
Copy link
Collaborator

danielbrunt57 commented Oct 5, 2024

The new code might be appending with a serial# vs without a serial# before? I suspect my environment (10 physical "included" Echo devices) might not have any appDeviceList entities. I wonder if it might be the Alexa App "device", which I've excluded (by not including it) thereby eliminating me from experiencing the problem?

@dpwood
Copy link
Contributor Author

dpwood commented Oct 5, 2024

I don't think the serial# behaviour has changed, only the way of building the list.

existing_serials = existing_serials + apps

will have been creating a new list each time, so wouldn't suffer this issue.

I've got Echo Dots, Echo Shows, Sonos, Fire TV sticks, and Alexa Mobile Voice iOS devices in the list. One of those is probably projecting an appDeviceList which has a serial that is then self-referencing.

I added logging to confirm that the issue is definitely within this loop. I can easily replicate it (happens every time). It is also resolved with this change.

@alandtse alandtse changed the title fix: Fixes #2579 - infinite loop and memory exhaustion from 4.13.3 fix: fix infinite loop and memory exhaustion Oct 5, 2024
@alandtse alandtse merged commit f7bdefe into alandtse:dev Oct 5, 2024
3 of 5 checks passed
@alandtse
Copy link
Owner

alandtse commented Oct 5, 2024

Thanks!

@jleinenbach
Copy link
Contributor

I just want to say sorry that I was unable to fix it fast. I tested the version, but not long enough as it seems. I am still trying to improve the code, but I will test it much longer now!

@simoneggersdorfer
Copy link

somehow the infinite loop is still there for me after upgrading to the new version and newest HA-Version.

@dpwood dpwood deleted the patch-1 branch October 13, 2024 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Version v4.13.3 is making HA fail
5 participants