Skip to content

Report bad file record frames as undecodable - #3069

Merged
janiversen merged 2 commits into
pymodbus-dev:devfrom
sankalpsthakur:fix/file-record-decode-errors
Oct 22, 2026
Merged

janiversen merged 2 commits into
pymodbus-dev:devfrom
sankalpsthakur:fix/file-record-decode-errors

Conversation

@sankalpsthakur

@sankalpsthakur sankalpsthakur commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

DecodePDU.decode turns ValueError, IndexError and struct.error into ModbusIOException. The framer tags that with the transaction and device id, and the request handler answers it with an exception response (#3057). The file record PDUs raised a plain ModbusException instead when the byte count (read and write file record) or the fifo count (read fifo queue response) points past the end of the frame (pymodbus/pdu/file_message.py). That exception leaves the framer without the ids, so the handler's ModbusIOException path never sees it.

These three decode checks now raise ModbusIOException, with the function code. ModbusException stays for problems with the use of the API, such as the checks in the FileRecord constructor, and decode itself is unchanged.

Tests: test_decode_bad_file_record_frame covers a bad read file record and write file record request on the server decoder and a bad read fifo queue response on the client decoder, and test_bad_file_record_request_for_framer_socket checks that the transaction and device id reach the exception. On dev all four fail with ModbusException: Modbus Error: Invalid byte count: 16 or Invalid fifo count: 5; here they pass. test/pdu and test/framer: 965 passed. ruff check, ruff format --check and pylint (10.00/10) are clean on the changed file.

AI disclosure: written with help from AI coding tools (Claude Code); I reviewed the change and checked the behaviour above before submitting.

The file record PDUs (read and write file record, read fifo queue) raise
ModbusException when the byte or fifo count in a frame points past its end.
DecodePDU.decode only turned ValueError, IndexError and struct.error into
ModbusIOException, so these escaped the framer without transaction and
device id, and the server could not answer them with an exception response
like any other undecodable request.

Convert ModbusException there as well, and keep ModbusIOException as is.

@janiversen janiversen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But ModbusException is a legal and documented exception, that is also raised f.x. by mixin.py

We do not want to convert ModbusException to ModbusIOException, as they are used to indicate 2 different types of problems.

@janiversen

janiversen commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

if ModbusException is raises in a place (like decode) it should be changed to ModbusIOException....to signal it is a communication problem and e.g. a problem with the use of the API.

The ModbusException in file_message.py is actually being changed with #3068 (not sure if this change have been uploaded yet).

ModbusException signals a problem with the use of the API, so decode()
is back to converting only ValueError, IndexError and struct.error.
The three checks that reject a frame while decoding (the read and write
file record byte counts and the read fifo queue count) now raise
ModbusIOException themselves, with the function code. The constructor
checks in file_message.py still raise ModbusException.
@sankalpsthakur

Copy link
Copy Markdown
Contributor Author

makes sense, thanks. reworked it that way in 5133651: decode() is back to unchanged, and the three decode-time checks in file_message.py (both byte counts and the fifo count) now raise ModbusIOException with the function code, while the FileRecord constructor checks keep ModbusException. the tests are the same and still fail on dev. if #3068 lands first i'll rebase onto it.

@janiversen janiversen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm Thanks,

@janiversen
janiversen merged commit add3009 into pymodbus-dev:dev Oct 22, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants