agittins / bermuda

Bermuda Bluetooth/BLE Triangulation / Trilateration for HomeAssistant
MIT License
643 stars 18 forks source link

Gracefully handle excessive advert caches #367

Closed agittins closed 1 week ago

agittins commented 2 weeks ago

A number of users have experienced startup crashes when first installing Bermuda, where this has been due to their systems having thousands (sometimes hundreds of thousands) of stale bluetooth advertisements cached in the backend.

These seem to persist in their systems via the /config/.storage/bluetooth.remote_scanners json file which gets saved out by the bluetooth manager on exit. There is a particular custom integration which might be responsible for this at least in a few cases, but until I test and prove if that's the case I'd rather not name it in speculation, plus it's possible there are others, and/or it might have been due to a bug in core that was later resolved, hard to say at this time.

Ideally, Bermuda should guard itself (and therefore the system) from instability caused by unexpected input.

This issue is to track those efforts to guard against, and ideally to resolve those cases. Additionally, attempts to replicate the issue and raise bug reports with packages that might cause this are also welcomed/intended.

Workaround

If you experience a crash on first installing and starting up with Bermuda, this issue might be relevant. It is especially so if you have previously had another bluetooth-tracking custom integration installed (so not from core, but via HACS or manual installation) - even if you have since uninstalled it.

On your HA system, check to see if this file is present, and how big it is:

~ # ls -l /homeassistant/.storage/bluetooth.*
-rw-r--r--    1 root     root         31284 Nov  9 05:06 /homeassistant/.storage/bluetooth.passive_update_processor
-rw-r--r--    1 root     root         80568 Nov  7 14:34 /homeassistant/.storage/bluetooth.remote_scanners
~ #

The bluetooth.remote_scanners file is the one of interest here. In this example, it is about 80KB in size (80568 bytes). This is a normal size for a moderately "busy" standalone house in a non-urban area. If your file is more than 300,000 bytes then you probably have this issue.

Do not proceed unless you have verified that the above applies to your situation, or you are comfortable with what you're doing.

You can work around the problem and get your system booting again by doing this in exactly this order:

How to rename the file:

You will need access to the command line on your Homeassistant machine. This example works for HAOS (the custom Homeasstant virtual machine) but your system might have a different install location or you might need to find out how to get to a command line. If you post to any of the HA community forums, explain your problem and link to this issue, someone will probably be able to help you.

From a standar HAOS command line:

~ # mv /homeassistant/.storage/bluetooth.remote_scanners /homeassistant/.storage/bluetooth.remote_scanners.bad

Copy/paste the results (or take a photo, whatever you need to do) so you can show someone what happened if you run into trouble.

This should solve the issue for you and HA (and Bermuda) should start up properly on future attempts.

Bermuda response Part I: Duck and cover

Bermuda should, before starting to process any advertisements:

Bermuda response Part II: Counter-ops

Repairs have the option to walk the user through solving an issue via a data flow. Bermuda could:

Other options

It's possible that raising a repair on identifying excess stale records, flushing the backend cache and potentially throttling the rogue integration should really be raised with core to be performed by the bt manager itself, rather than having Bermuda try and stomp around in there.

agittins commented 1 week ago

This should be resolved by the next HA release, as a fix has been merged that prevents these records being loaded from disk at startup: https://github.com/home-assistant/core/issues/130432