Repository: pymodbus-dev/pymodbus

Issue:
ModbusTCP _in_waiting not defined.
<!--
Please use the Pymodbus gitter channel at https://gitter.im/pymodbus_dev/Lobby or Stack Overflow(tag [pymodbus](https://stackoverflow.com/questions/tagged/pymodbus) for
support questions.

Before opening a new issue, make sure you do the following:
    * check that your issue isn't already filed: https://github.com/riptideio/pymodbus/issues
    * prepare a short, runnable example that reproduce the issue with the latest development version of Pymodbus
-->

### Versions

* Python: 3.8
* OS: hastío
* Pymodbus: 2.51
* Modbus Hardware (if used): 

### Pymodbus Specific
* Client: tcp - sync

### Description
The modbus integration in home assistant uses pymodbus, and lately we have started getting more and more issues with
```
File "/usr/src/homeassistant/homeassistant/components/modbus/modbus.py", line 305, in _pymodbus_call
result = self._pb_call[use_call][ENTRY_FUNC](address, value, **kwargs)
File "/usr/local/lib/python3.9/site-packages/pymodbus/client/common.py", line 103, in write_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 194, in execute
response, last_exception = self._retry_transaction(retries, "empty", request, expected_response_length, full=full)
File "/usr/local/lib/python3.9/site-packages/pymodbus/transaction.py", line 250, in _retry_transaction
in_waiting = self.client._in_waiting()
AttributeError: 'ModbusTcpClient' object has no attribute '_in_waiting'
```

We use slave 0 as default, and some users inform that the error goes away if they specify a slave > 0, but this is not consistent.

I am the maintainer of the modbus integration.


### Code and Logs

```python
 # code and logs here.
https://github.com/home-assistant/core/tree/dev/homeassistant/components/modbus
```

ModbusTCP _in_waiting not defined.
<!--
Please use the Pymodbus gitter channel at https://gitter.im/pymodbus_dev/Lobby or Stack Overflow(tag [pymodbus](https://stackoverflow.com/questions/tagged/pymodbus) for
support questions.

Before opening a new issue, make sure you do the following:
    * check that your issue isn't already filed: https://github.com/riptideio/pymodbus/issues
    * prepare a short, runnable example that reproduce the issue with the latest development version of Pymodbus
-->

### Versions

* Python: 3.8
* OS: hastío
* Pymodbus: 2.51
* Modbus Hardware (if used): 

### Pymodbus Specific
* Client: tcp - sync

### Description
The modbus integration in home assistant uses pymodbus, and lately we have started getting more and more issues with
```
File "/usr/src/homeassistant/homeassistant/components/modbus/modbus.py", line 305, in _pymodbus_call
result = self._pb_call[use_call][ENTRY_FUNC](address, value, **kwargs)
File "/usr/local/lib/python3.9/site-packages/pymodbus/client/common.py", line 103, in write_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 194, in execute
response, last_exception = self._retry_transaction(retries, "empty", request, expected_response_length, full=full)
File "/usr/local/lib/python3.9/site-packages/pymodbus/transaction.py", line 250, in _retry_transaction
in_waiting = self.client._in_waiting()
AttributeError: 'ModbusTcpClient' object has no attribute '_in_waiting'
```

We use slave 0 as default, and some users inform that the error goes away if they specify a slave > 0, but this is not consistent.

I am the maintainer of the modbus integration.


### Code and Logs

```python
 # code and logs here.
https://github.com/home-assistant/core/tree/dev/homeassistant/components/modbus
```



Discussion:
Is the issue also seen with 2.5.2?
Never mind this is a bug.
Yup but difficult to reproduce and isolate, I have tried for a while….but of course you know the code a lot better.

Looking forward to see a fix.
Is the issue also seen with 2.5.2?
Never mind this is a bug.
Yup but difficult to reproduce and isolate, I have tried for a while….but of course you know the code a lot better.

Looking forward to see a fix.

Resolving patch:
diff --git a/pymodbus/transaction.py b/pymodbus/transaction.py
--- a/pymodbus/transaction.py
+++ b/pymodbus/transaction.py
@@ -247,11 +247,12 @@ def _retry_transaction(self, retries, reason,
             time.sleep(delay)
             _logger.debug("Sleeping {}".format(delay))
         self.client.connect()
-        in_waiting = self.client._in_waiting()
-        if in_waiting:
-            if response_length == in_waiting:
-                result = self._recv(response_length, full)
-                return result, None
+        if hasattr(self.client, "_in_waiting"):
+            in_waiting = self.client._in_waiting()
+            if in_waiting:
+                if response_length == in_waiting:
+                    result = self._recv(response_length, full)
+                    return result, None
         return self._transact(packet, response_length, full=full)
 
     def _transact(self, packet, response_length,


Test patch: