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

Memory leak on HA OS 11.5 #401

Closed
MattiaEffendi opened this issue Feb 10, 2024 · 13 comments
Closed

Memory leak on HA OS 11.5 #401

MattiaEffendi opened this issue Feb 10, 2024 · 13 comments
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@MattiaEffendi
Copy link

MattiaEffendi commented Feb 10, 2024

Describe the bug
When initializing the integration (both pre-configured or via the configuration wizard, at the end), Home Assistant docker container starts using a lot of RAM, leading to the container being killed by the host system.
When that happens, HA Core is dead and the frontend cannot be reached. HA Core restart or host reboot doesn't help, leading to the same problem

The only way to fix is to log-in via SMB (if you have the addon installed) and delete the custom_components/dreame_vacuum folder.

I have been running the integration v2.0.0b11 without any problems for the past few months, and this only started happening yesterday after updating Home Assistant Operating System to 11.5.
The VM that was running Home Assistant has always had 2GB of RAM, i've increased that to 6GB to try and see if it was the problem but it still persisted.

I've been able to fix my main Home Assistant instance as described above, but i have a Home Assistant snapshot affected by this problem that i can restore on another instance to investigate the problem.

To Reproduce

  • Install HA OS 11.5
  • Install integration via HACS
  • Run the configuration wizard for the integration. When you click "done" on the last step, Home Assistant Core will crash and the error in the picture below will appear in the host console.

Or, if you have the addon already installed and integration set-up:

  • Update to HA OS 11.5
  • On boot, Home Assistant Core will crash and the error in the picture below will appear in the host console.

Expected behavior
Be able to set up the integration, or boot HA core if the integration itself is already installed.

Screenshots

Screenshot 2024-02-10 at 18 55 00 (took via Home Assistant host console)

Additional Information (please complete the following information)

  • Model Name [e.g. dreame.vacuum.p2028]: dreame.vacuum.r2228o
  • Firmware Version [e.g. 1156]: 4.3.9_3104
  • Home Assistant Version: 2024.2.1, supervisor 2024.01.1, OS 11.5
  • Configuration Type [With or without map support]: With map support
  • Errors or warnings shown in the HA logs (if applicable): None, except "Home Assistant has crashed!"
@MattiaEffendi MattiaEffendi added bug Something isn't working question Further information is requested labels Feb 10, 2024
@Tasshack
Copy link
Owner

Do you have any HA logs just before it was killed?

@MattiaEffendi
Copy link
Author

@Tasshack nothing. The logs before it was killed were from around 15/20 seconds before.

@Tasshack
Copy link
Owner

Tasshack commented Feb 10, 2024

That may mean it crashes when installing the prerequisites.
One of the requirements of the integration is py-mini-racer and that package relies on static libraries compiled for each cpu architecture. If pil cannot find the native libraries for the py-mini-racer it tries to compile them from source. That is a case on RPI3 and RPI4 devices but this process should never use more than 2GB memory.

You can try to manually install the requirements from virtual environment of the HA

  "requirements": [
    "pillow",
    "numpy",
    "pybase64",
    "requests",
    "pycryptodome",
    "python-miio",
    "py-mini-racer",
    "tzlocal",
    "paho-mqtt"
  ],

@MattiaEffendi
Copy link
Author

MattiaEffendi commented Feb 10, 2024

@Tasshack That could definitely be the issue. I remembered now that when watching top the process which was memory leaking was, in fact, some setuptools stuff.

Just for clarity: the manual install you suggested is to debug the problem or a definitive fix? Because i think that if i run those commands in the Core container they won't be preserved on container restart, or not?

@Tasshack
Copy link
Owner

For debugging;
You can just remove the py-mini-racer from manifest.json at the dreame_vacuum directory. Integration won't start and complain about it's lackness but HA should start if that was the problematic library.

For fix;
I have never used HA OS and not sure how it is different from the Core installation but I can manually install the requirements on my HA Core using commands below. Basically HA is doing at the same thing for each required library from the inside so it should be persistent after the reboot.

sudo -u homeassistant -H -s
cd /srv/homeassistant && python3 -m venv . && source bin/activate
pip install py-mini-racer

You probably won't be able to install it manually too since it still will be using to much memory but that is an another issue to solve.

@MattiaEffendi
Copy link
Author

MattiaEffendi commented Feb 10, 2024

@Tasshack Home Assistant OS is basically a Linux image containing scripts that orchestrate some docker containers (Home Assistant Core, Home Assistant Supervisor, Home Assistant Observer, the addons you install from the addons page and a few more). I don't know if that could help you but i'm running my install on Proxmox on an x86_64 arch.

I've tried removing the py-mini-racer dependency and i can confirm Home Assistant starts correctly, but the weird thing is that the integration works. Sensors are exposed and the state is correct. I don't know what that library is for but at a first glance the integration is working correctly.

But the weirdest thing is that even when restoring the full backup that was "broken" on another instance (aka, with the integration installed and untouched), and that was throwing the error in the photo above.

The only difference between when i was restoring that a few hours ago trying to debug (and it was giving me the error) and now is that now the robot is cleaning and back then it was at its base. I have no idea if this can have an influence.

Could that be some sort of server-side issue of PyPI or the package repository the dependency used that caused the leak, and it being now fixed the package installs like normal?

I have no idea

@Tasshack
Copy link
Owner

Tasshack commented Feb 10, 2024

That library is responsible for running the JS code that is for optimizing the VSLAM vacuum maps because python is too slow for running some flood fill algorithms and such. Since yours is a LIDAR device, that piece of code won't be used but python will complain about the map.py and everything inside it because a library has to be imported even when it has not been used.
Maybe that was an older python behaviour but your map feature shouldn't be working right now without from py_mini_racer import MiniRacer

@MattiaEffendi
Copy link
Author

MattiaEffendi commented Feb 10, 2024

@Tasshack i can confirm that re-installing the integration on my main Home Assistant instance now works.

I have no idea what can have caused that memory leak repeatedly (and i can confirm that the problem wasn't one-shot, i've created multiple VMs and installed the plugin multiple times). Do you think the hypotesis i wrote in the message above can make sense?

Could that be some sort of server-side issue of PyPI or the package repository the dependency used that caused the leak, and it being now fixed the package installs like normal?

@MattiaEffendi
Copy link
Author

I have a Proxmox snapshot of the instance made at the time when the installation was memory leaking. I will try to restore a VM with that snapshot to ensure that the problem is now ok. I still can't explain what happened

@Tasshack
Copy link
Owner

Normally py-mini-racer shipped with the precompiled libraries for x64 systems so it should not try to compile them on your system in the first place so that can verify your suggestion.
Also don't forget that every version of HA requires all of the requirements to be installed again. So this issue may happen any version of the OS and it is not version specific and I am closing this issue because of that.

@MattiaEffendi
Copy link
Author

@Tasshack that makes sense, but even if it needs to compile for x64 do you think that it needs to use more than 6GB of memory? It still seems a lot to me.

I can confirm that after the restore of the affected snapshot everything now works correctly.

@Tasshack
Copy link
Owner

As I said before, memory usage is another issue that is not the scope of this project and can be resulted from many different reasons apart from the integration.
There is nothing I can fix or guess about this issue because it cannot be reproduced right now.

@Tasshack
Copy link
Owner

@MattiaEffendi looks like this is not an issue limited to your setup after all.
@Ives25 #353 (reply in thread)

@Tasshack Tasshack reopened this Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants