ljean / modbus-tk

Create Modbus app easily with Python
Other
557 stars 211 forks source link

Thread safety modbus tcp slave #104

Open CorF4 opened 5 years ago

CorF4 commented 5 years ago

Dear all, here is a question: I have a script which sets up a modbus tcp slave. This slave I use as a simulator which is basically managing its register content via set_values and get_values. I am performing a component test with this simulator. Therefore, I setup a few clients that connect to this slave and send some random read or write requests. This means there are several clients that could potentially write the same registers at a time. I would not expect it to be a problem though, cause set_values has a lock which should prevent race conditions. However, when performing this sort of stress test I sometimes see such messages:

(2018-08-08 12:34:19,776) (Thread-1 ) ('127.0.0.1', 34616) is connected with socket 52...
problematic value  -49190920.0
(2018-08-08 12:34:20,266) (Thread-1 ) handle request failed: integer out of range for 'H' format code

Where it seems that the simulator constantly reconnects to a different socket and then comes the info on the handle request fail (not sure which comes first, it's repeating itself with increasing socket number). I added the 'problematic value' printout in the code of modbus.py in function _read_registers(self, block_type, request_pdu). Now, my question is: is it possible that set_values actually causes some negative values to be written into the block when invoked multiple times at the same time? Or should I rather check my simulator again to make sure I didn't call the set_values with an already deprecated values-list? Has someone maybe seen such a behaviour? Thanks!

CorF4 commented 5 years ago

Dear all, I think this was caused by a bug in my simulator. However, I am still interested as to why I see socket connection messages when invalid values appear. Thanks!