Skip to content

TCP server: requests behind an undecodable request in the same TCP segment are discarded #3055

Closed

Description

@sotomayorlucas

Versions

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:

DEBUG transport:308 recv: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x41 0x0 0x0 0x0 0x2 0x0 0x2 0x0 0x0 0x0 0x6 0x1 0x3 0x0 0x0 0x0 0x1 extra data:
DEBUG base:75 Processing: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x41 0x0 0x0 0x0 0x2 0x0 0x2 0x0 0x0 0x0 0x6 0x1 0x3 0x0 0x0 0x0 0x1
DEBUG transport:357 send: 0x0 0x0 0x0 0x0 0x0 0x3 0x0 0xc1 0x1

server/requesthandler.py callback_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

import socket
s = 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 1
print(s.recv(100).hex())   # socket.timeout: tid 2 was discarded

Activity

  1. janiversen commented on Oct 12, 2026

    @janiversen
    Collaborator

    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.

  2. janiversen commented on Oct 12, 2026

    @janiversen
    Collaborator

    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.

  3. sotomayorlucas commented on Oct 12, 2026

    @sotomayorlucas
    ContributorAuthor

    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.

  4. janiversen commented on Oct 12, 2026

    @janiversen
    Collaborator

    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.

  5. sotomayorlucas commented on Oct 12, 2026

    @sotomayorlucas
    ContributorAuthor

    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.

  6. janiversen commented on Oct 12, 2026

    @janiversen
    Collaborator

    Fixed in #3059

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions