core: Modbus over TCP causes memory leak

The problem

Modbus integration causes memory leak. I have 14 energy meters connected via Modbus TCP (65 entities in total) and everyday I experience crash of HA production installation due to lack of memory and swap. The same occurs on clean Home Assistant Container installation from which I attach the logs. There’s nothing added in this clean installation except for System Monitor and Modbus integrations. I attach the screenshot of memory usage from both HA installations. Both installations use latest HA versions and the bug doesn’t relate to particular version.

image image image

What version of Home Assistant Core has the issue?

core-2024.9.3

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Modbus

Link to integration documentation on our website

No response

Diagnostics information

home-assistant_modbus_2024-10-02T09-08-31.340Z.log

Example YAML snippet

modbus:
  - name: "Miernik Energii Rozdzielnica"
    type: tcp
    host: "192.168.1.252"
    port: 4196
    sensors:
      # Pompa CO Góra
      - unique_id: pompa_co_gora_napiecie
        name: Pompa CO Góra Napięcie
        device_address: 1
        address: 0
        scan_interval: 2
        input_type: input
        data_type: float32
        device_class: voltage
        precision: 1
        state_class: measurement
        unit_of_measurement: V
      - unique_id: pompa_co_gora_natezenie_pradu
        name: Pompa CO Góra Natężenie prądu
        device_address: 1
        address: 8
        scan_interval: 2
        input_type: input
        data_type: float32
        device_class: current
        precision: 2
        state_class: measurement
        unit_of_measurement: A
      - unique_id: pompa_co_gora_moc
        name: Pompa CO Góra Moc
        device_address: 1
        address: 18
        scan_interval: 2
        input_type: input
        data_type: float32
        device_class: power
        precision: 1
        state_class: measurement
        unit_of_measurement: W
      - unique_id: pompa_co_gora_power_factor
        name: Pompa CO Góra Power factor
        device_address: 1
        address: 42
        scan_interval: 2
        input_type: input
        data_type: float32
        device_class: power_factor
        precision: 2
        state_class: measurement
      - unique_id: pompa_co_gora_suma_dostarczonej_energii
        name: Pompa CO Góra Suma dostarczonej energii
        device_address: 1
        address: 256
        scan_interval: 2
        input_type: input
        data_type: float32
        device_class: energy
        precision: 2
        state_class: total_increasing
        unit_of_measurement: kWh

Anything in the logs that might be useful for us?

No response

Additional information

I’m using Waveshare 4-CH RS485 TO POE ETH (B) gateway to convert Modbus RTU to Modbus TCP.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 36 (10 by maintainers)

Commits related to this issue

Most upvoted comments

I think the simplest solution would be to use a DUC per entity which already has safe guards to prevent this problem.

While I could make the change, sadly I don’t have any modbus devices so test with it so it will need to wait for a developer who does

@joostlek I wasn’t able to run py-spy top --pid 67 due to error Error: Unsupported version of Python: 3.12.0.

The callgrind files are attached attached but start_log_objects was interrupted by container crash due to lack of memory. I will try tomorrow. profile.zip

On 2025.1 I didn’t notice any improvement, memory still leaks as it was.

It seems nobody cares about one of major smart home communication protocols…

Unfortunately the yaml matches the spec and my suspect was wrong. The only further suggestion is to try break down the problem in smaller pieces, in example focusing on sensors coming from one device at a time and avoid to include sensors missing the answer as in example device 1.

I did it for all sensors, no worries 🙂 “It” stands for scan_interval.