pyhys / minimalmodbus

Easy-to-use Modbus RTU and Modbus ASCII implementation for Python.
Apache License 2.0
308 stars 145 forks source link

long sleep times when changing system clock #68

Closed vthielen closed 3 years ago

vthielen commented 3 years ago

When I set the Linux system clock for the first time after system startup (meaning there might be a big jump, because clock before was uninitialized) the library suddenly seems to 'hang' on the next modbus operation. Some debugging reveals that there is a sleep that times some operation by subtracting two system times. This means that the sleep time is equal to the clock jump, and could becomes days, months... I temporarily changed the code to limit max sleep to 1 sec (I guess that should be enough en all cases), but if I ever have to reinstall/ upgrade the lib I will have to debug the same problem (I have very limited memory for old problems). Is it possible to correct this in the official version of the lib?

On line 1337 of /usr/local/lib/python2.7/minimalmodbus/dist-packages/minimalmodbus.py in the original version there is:

        time.sleep(sleep_time)

I changed this into :

        if sleep_time > 1 : sleep_time=1     # added to avoid possible very long stalls when system clock is reset
        time.sleep(sleep_time)
j123b567 commented 3 years ago

Pleas keep in mind, that python 2.7 is dead platform and it is unlikely, that bugs related to it will be fixed.

When running with python 3, library uses time.monotonic(), which does not have this issue.

pyhys commented 3 years ago

Next release of Minimalmodbus will only support Python3, and there is time.monotonic() available.