Fix undecodable function-code exception identity - #2992
Merged
janiversen merged 2 commits intoJul 28, 2026
Merged
Conversation
When the server cannot decode a request function code, echo the framed transaction and device ids on the illegal-function exception response, and use function code 0x00 instead of the hardcoded 0x28. Fixes pymodbus-dev#2990 Signed-off-by: Sankalp Thakur <sankalphimself@gmail.com>
Add transaction_id and dev_id as first-class fields so the type checker accepts the undecodable-function exception path for pymodbus-dev#2990. Signed-off-by: Sankalp Thakur <sankalphimself@gmail.com>
janiversen
approved these changes
Jul 28, 2026
janiversen
left a comment
Collaborator
There was a problem hiding this comment.
VERY nice way to solve the problem!
Feel free to search for ModbusIOException, I am pretty sure there are a number of places where transaction id and/or function code is known.
LGTM, thanks
sankalpsthakur
added a commit
to sankalpsthakur/pymodbus
that referenced
this pull request
Jul 28, 2026
Critical review of pymodbus-dev#2992/pymodbus-dev#2993 found register_message identity was dead because DecodePDU swallowed ModbusIOException. Re-raise payload decode errors, enrich framing tid/dev_id in the framer, use known fcode on the server exception response when present, and forward the UDP peer addr. Also lean the helper docstring, drop a private-method unit test, and assert identity through handleFrame + sync execute paths. Signed-off-by: Sankalp Thakur <sankalphimself@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When the server cannot decode a request function code (unassigned/reserved FC or serial noise),
ServerRequestHandler.callback_datawas building the exception response with two bugs:40(0x28) → every undecodable request produced exception FC0xA8, unrelated to the request.transaction_idleft at0→ TCP clients correlating in-flight requests by transaction id could not match the response.last_pduis cleared at the start of framing, so the framer now attaches the already-decoded MBAP/RTUtransaction_idanddev_idto theModbusIOException. The server uses those attributes on the exception path, and buildsExceptionResponse(0x00, ILLEGAL_FUNCTION, ...)instead of the unrelated40.This matches maintainer guidance on #2990: echo transaction id, use
0x00rather than inventing a function code from garbage.Validation
New unit coverage:
0x09echoes MBAP tid0x1234Fixes #2990
AI/LLM disclosure