Skip to content

Keep the RTU frame after a skipped frame for another device. - #3112

Merged
janiversen merged 2 commits into
pymodbus-dev:devfrom
aliassar:fix/rtu-decode-frame-length
Dec 4, 2026
Merged

janiversen merged 2 commits into
pymodbus-dev:devfrom
aliassar:fix/rtu-decode-frame-length

Conversation

@aliassar

@aliassar aliassar commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3110.

Checked on dev (080cf3a): the repro from the issue still loses device 2's response. FramerRTU.decode() returns the whole buffer length on success, so when handleFrame() skips a frame for another device with continue, the frame behind it is consumed too.

decode() now returns the frame length (used_len + size, the size is known there and the CRC matched), so handleFrame() in base.py can skip a frame and still see the next one. If what is left after the frame is too short to be another frame, decode() takes it too, so a stray byte after the frame (#2664, test_handleFrame2) is still dropped like before. The CRC failure path is unchanged: the frame end isn't known there, so the whole buffer still goes.

Tests: test_rtu_skip_frame_for_other_device and a "double good crc" RTU case in test_decode_complicated fail on dev and pass here. test_bad_file_record_request_for_framer_rtu covers the decode error path with a stray byte. Full pytest --cov is at 100%, and codespell, ruff, pylint and zuban are clean.

#3068 changes these lines in rtu.py too, so whichever lands second needs a small merge.

FramerRTU.decode() returned the whole buffer length on success, so when
handleFrame() skipped a frame for another device, the frame after it was
consumed too. decode() now returns the frame length, and handleFrame()
still drops whatever follows the frame it uses. After a skipped frame the
rest is kept only if it starts with the expected device id, so a stray
byte there is still dropped. The CRC failure path is unchanged and still
drops the whole buffer.

Fixes pymodbus-dev#3110.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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.

Please do not break the structure.

#3068 do have some changes in this, but those are merely due to changes in pdu. Once this PR is merged I will update #3068.

Comment thread pymodbus/framer/rtu.py Outdated
return used_len + size, dev_id, 0, data[used_len + 1 : crc_start]
return 0, 0, 0, self.EMPTY

def handleFrame(

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.

This do not belong in rtu.py it is handled in base.py and are common for all frames.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, makes sense. I removed the handleFrame() override in 0029572, the skipping is left to base.py now. The only change in rtu.py is in decode(): it returns the frame length instead of the whole buffer, and still takes a tail that is too short to be another frame (the stray 0x00 from #2664), so test_handleFrame2 keeps passing unchanged.

Drop the handleFrame() override in rtu.py, the skipping is handled in
base.py. decode() returns the frame length, and takes the rest of the
buffer too when it is too short to be another frame (stray byte after
the frame, pymodbus-dev#2664).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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 77fd673 into pymodbus-dev:dev Dec 4, 2026
31 of 32 checks passed
janiversen pushed a commit that referenced this pull request Dec 4, 2026
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

FramerRTU.decode() consumes the whole buffer: a frame following a skipped frame in the same packet is lost

2 participants