Skip to content

Give each ModbusDeviceIdentification its own data - #3036

Merged
janiversen merged 1 commit into
pymodbus-dev:devfrom
kratos0718:fix-device-identification-shared-dict
Sep 28, 2026
Merged

janiversen merged 1 commit into
pymodbus-dev:devfrom
kratos0718:fix-device-identification-shared-dict

Conversation

@kratos0718

Copy link
Copy Markdown

ModbusDeviceIdentification.stat_data is a class attribute and __init__, update() and __setitem__ all write into it, so every identity object shares one dict. The ModbusControlBlock keeps its own identity instance, so creating any identity anywhere also changes what a running server reports:

server_identity = ModbusDeviceIdentification(info_name={"VendorName": "Acme"})
ModbusControlBlock().Identity.update(server_identity)
ModbusDeviceIdentification(info_name={"VendorName": "SomethingElse"})
ModbusControlBlock().Identity.VendorName   # 'SomethingElse'

The fix copies the defaults into an instance dict in __init__. I added two tests to test/pdu/test_device.py, one for two independent identities and one for the control block case. Both fail on dev and pass with the change. test/pdu, test/server and test/simulator pass (1035 tests), and ruff and pylint are clean.

stat_data is a class attribute and __init__ wrote into it, so every
identity shared one dict. Creating any identity also changed the one
held by the ModbusControlBlock, i.e. what a running server reports.

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

This is one that have been on my list for a long time, thanks for adding a solution.

@janiversen
janiversen merged commit db89344 into pymodbus-dev:dev Sep 28, 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