Skip to content

sync modbus tcp crashed when server breaks connection (in HomeAssistant) #721

@janiversen

Description

@janiversen

Versions

  • Python:
    verifified with 3.8, 3.9 and 3.10

  • OS:
    Hass

  • Pymodbus:
    2.53

  • Modbus Hardware (if used):
    Reproduceable with software server as well.

Pymodbus Specific

  • Server: tcp/rtu/ascii - sync/async
  • Client: tcp/rtu/ascii - sync/async
    Client sync (modbus integration)

Description

This is the original issue:

home-assistant/core#68161

Hello,

I have a Victron Quattro inverter connected to Home assistant pulling data via Modbus TCP.

All works well until the inverter/charger is rebooted (say for a firmware update) or turned off/on for service work, or even a simple network issue temporarily.

Home assistant simply shows "unavailable" for all sensors until home assistant is restarted. There are no other issues and the device can stay up forever otherwise.. it is annoying as if someone is doing work or something does happen and it is not noticed the charger/inverter is not logged until home assistant is manually restarted to fix the connection issue.

Code and Logs

+++++++++++++++++++++++++++++++++++++++++++++

My config:

Configuration.yaml

modbus: !include modbus.yaml

Modbus.yaml

modbus:

  • name: victron
    type: tcp
    host: 192.168.50.131 # use the IP address of your CCGX
    port: 502
    retries: 20000 ||| Makes no difference what this is set to.
    close_comm_on_error: false |||||||||Tried this both False and the default of true
    sensors:
    • name: "Shore Power"
      scan_interval: 10
      unit_of_measurement: "W"
      slave: 100
      address: 820

    • name: "PV Power"
      scan_interval: 10
      unit_of_measurement: "W"
      slave: 100
      address: 850

    • name: "AC Loads"
      scan_interval: 10
      unit_of_measurement: "W"
      slave: 100
      address: 817

+++++++++++++++++++++++++++++++++++++++

 # code and logs here.
Here is the export from the core log: 
2022-03-14 22:51:31 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/modbus/sensor.py", line 63, in async_update
    result = await self._hub.async_pymodbus_call(
  File "/usr/src/homeassistant/homeassistant/components/modbus/modbus.py", line 390, in async_pymodbus_call
    result = await self.hass.async_add_executor_job(
  File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/modbus/modbus.py", line 367, in _pymodbus_call
    result = entry.func(address, value, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/pymodbus/client/common.py", line 114, in read_holding_registers
    return self.execute(request)
  File "/usr/local/lib/python3.9/site-packages/pymodbus/client/sync.py", line 109, in execute
    return self.transaction.execute(request)
  File "/usr/local/lib/python3.9/site-packages/pymodbus/transaction.py", line 174, in execute
    response, last_exception = self._transact(
  File "/usr/local/lib/python3.9/site-packages/pymodbus/transaction.py", line 294, in _transact
    result = self._recv(response_length, full)
  File "/usr/local/lib/python3.9/site-packages/pymodbus/transaction.py", line 326, in _recv
    read_min = self.client.framer.recvPacket(min_size)
  File "/usr/local/lib/python3.9/site-packages/pymodbus/framer/__init__.py", line 49, in recvPacket
    return self.client.recv(size)
  File "/usr/local/lib/python3.9/site-packages/pymodbus/client/sync.py", line 89, in recv
    return self._recv(size)
  File "/usr/local/lib/python3.9/site-packages/pymodbus/client/sync.py", line 293, in _recv
    ready = select.select([self.socket], [], [], end - time_)
ValueError: filedescriptor out of range in select()

I personally think it is a timing problem, socket closes just before select() is called, causing select() to “see” a closed file descriptor.

Maybe it is about time to get version 3.0.0 out as release.

Let me know if I can add more information to help.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions