You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TCP server: requests behind an undecodable request in the same TCP segment are discarded #3055
Server: StartAsyncTcpServer with a ModbusServerContext
Description
This is the third item from #3047, re-tested after #3049 as promised. Two requests arrive in one TCP segment: an unknown function code (0x41, tid 1) followed by a valid Read Holding Registers request (tid 2). The exception for tid 1 is sent, but tid 2 is never answered:
server/requesthandler.pycallback_data (line 83 on e747a12) returns len(data) when it catches a ModbusIOException, which discards every ADU in the buffer, not only the one that failed to decode. The same two requests sent in separate segments are both answered. (The exception's transaction and unit ids being 0 is reported in #3051.)
Expected
Only the undecodable ADU is consumed (its MBAP length is valid, so its end is known); tid 2 is answered.
Reproducer
importsockets=socket.create_connection(("127.0.0.1", 5020)); s.settimeout(1)
s.sendall(bytes.fromhex("000100000006014100000002""000200000006010300000001")) # unknown fc, then valid read, one send()print(s.recv(100).hex()) # exception for tid 1print(s.recv(100).hex()) # socket.timeout: tid 2 was discarded
That is the intended behavior, because with an unknown fc there are no way to know the correct length of the frame (the mdap can be corrupted as well and are not present in e.g. serial communication), so all received data are cleared.
If you send a valid fc followed by an invalid in the same send(), the first frame is handled and the second is discarded.
Please be aware this is not noise on the line, it is the device sending something false, and thus it can very well be that the length field is false as well, meaning the only sure thing is to clear the data......alternatively the connection could be disconnected, but that is a bit too drastic in my opinion.
Thanks for explaining. Agreed for serial framing, where there is no MBAP, and for a malformed MBAP header (protocol id ≠ 0, length out of range): there the boundary is unknowable and clearing is the safe choice, as #3053 now does.
The case here is different: it is TCP, the header is valid (protocol id 0, length 6 matching the PDU), and only the function code is unsupported. That is a well-formed request for an unimplemented function, which the spec answers with exception 01, and the next request in the buffer is valid too, so discarding it silently loses a request the client will never hear about. libmodbus took the same view in stephane/libmodbus#881 (frame by MBAP length once the header is valid).
Your call, of course; if you prefer to keep clearing, maybe a debug/warning log line when bytes are dropped would at least make it visible.
At the place where the unknown function code is detected, transaction id as well as device id are not available because it is a generic check....however I will leave this open and see if there is a way.
Thanks for keeping it open. You're right that DecodePDU.decode() doesn't know the ids, but its caller does: FramerBase.handleFrame() has already parsed tid and dev_id from the MBAP, and the length it consumed (used_len), when it calls decode(). I opened #3056 for the ids, with the fix in #3057: catch the ModbusIOException in the framer, fill in the ids, re-raise.
The same spot could cover this issue: if the framer also attaches used_len to the exception, callback_data can skip just that ADU and keep processing the rest of the buffer, falling back to len(data) when the length isn't known. A rough prototype (about ten lines across the framer and the request handler) on f1b95b3, with unknown fc / valid read / unknown fc / valid read in one segment, answers all four with their own ids (the two exceptions go out first, since valid requests are handled via call_soon), and gives the same results as unpatched in test/framer, test/pdu, test/server and test/transaction. I only tried it with the socket framer, not RTU/ASCII. If you'd like this one as a PR too, I'll send it once #3057 is in, so the two stay separate.
Activity
janiversen commentedon Oct 12, 2026
That is the intended behavior, because with an unknown fc there are no way to know the correct length of the frame (the mdap can be corrupted as well and are not present in e.g. serial communication), so all received data are cleared.
If you send a valid fc followed by an invalid in the same send(), the first frame is handled and the second is discarded.
janiversen commentedon Oct 12, 2026
Please be aware this is not noise on the line, it is the device sending something false, and thus it can very well be that the length field is false as well, meaning the only sure thing is to clear the data......alternatively the connection could be disconnected, but that is a bit too drastic in my opinion.
sotomayorlucas commentedon Oct 12, 2026
Thanks for explaining. Agreed for serial framing, where there is no MBAP, and for a malformed MBAP header (protocol id ≠ 0, length out of range): there the boundary is unknowable and clearing is the safe choice, as #3053 now does.
The case here is different: it is TCP, the header is valid (protocol id 0, length 6 matching the PDU), and only the function code is unsupported. That is a well-formed request for an unimplemented function, which the spec answers with exception 01, and the next request in the buffer is valid too, so discarding it silently loses a request the client will never hear about. libmodbus took the same view in stephane/libmodbus#881 (frame by MBAP length once the header is valid).
Your call, of course; if you prefer to keep clearing, maybe a debug/warning log line when bytes are dropped would at least make it visible.
janiversen commentedon Oct 12, 2026
At the place where the unknown function code is detected, transaction id as well as device id are not available because it is a generic check....however I will leave this open and see if there is a way.
sotomayorlucas commentedon Oct 12, 2026
Thanks for keeping it open. You're right that
DecodePDU.decode()doesn't know the ids, but its caller does:FramerBase.handleFrame()has already parsedtidanddev_idfrom the MBAP, and the length it consumed (used_len), when it callsdecode(). I opened #3056 for the ids, with the fix in #3057: catch theModbusIOExceptionin the framer, fill in the ids, re-raise.The same spot could cover this issue: if the framer also attaches
used_lento the exception,callback_datacan skip just that ADU and keep processing the rest of the buffer, falling back tolen(data)when the length isn't known. A rough prototype (about ten lines across the framer and the request handler) on f1b95b3, with unknown fc / valid read / unknown fc / valid read in one segment, answers all four with their own ids (the two exceptions go out first, since valid requests are handled viacall_soon), and gives the same results as unpatched intest/framer,test/pdu,test/serverandtest/transaction. I only tried it with the socket framer, not RTU/ASCII. If you'd like this one as a PR too, I'll send it once #3057 is in, so the two stay separate.janiversen commentedon Oct 12, 2026
Fixed in #3059