Skip to content

Answer each UDP request at the address it came from - #3091

Merged
janiversen merged 1 commit into
pymodbus-dev:devfrom
bIackr0se:fix/udp-reply-address
Nov 19, 2026
Merged

janiversen merged 1 commit into
pymodbus-dev:devfrom
bIackr0se:fix/udp-reply-address

Conversation

@bIackr0se

Copy link
Copy Markdown
Contributor

Why

A ModbusUdpServer with more than one client can send a reply to the wrong client. The server uses one ServerRequestHandler for every UDP peer. callback_data queues each request with call_soon, and handle_request then answers at self.last_addr, which the next datagram overwrites. When a second valid request is read before the first request is answered, the first reply goes to the second sender.

If that second datagram cannot be decoded, last_addr is still None when the first reply is sent. sendto(data, None) raises TypeError, asyncio logs "Fatal write error on datagram transport" and closes the server's socket, and later requests get no reply (client C in the run below).

The fix passes each request's address along with the request, so its reply goes to the peer that sent it.

What changed

  • pymodbus/server/requesthandler.py: callback_data queues handle_later(pdu, addr) with the address the PDU arrived from, and handle_request(pdu, addr=None) sends the response to that address instead of self.last_addr.
  • test/server/test_requesthandler.py: test_rh_reply_goes_to_request_sender feeds a request from peer A and then a datagram from peer B, before A's request is handled, and checks that tid 1 goes to A and tid 2 to B. It runs with a valid second request and with an unknown function code (0x41); both cases fail on dev.

Evidence

A ModbusUdpServer on 127.0.0.1 and raw UDP clients sending Read Holding Registers (device 1, address 0, count 2). In "overlap" A and B send back to back; in "bad-second" B sends a datagram with function code 0x41 right after A's request; "after" is a new client C that sends one request at the end.

Before (dev 19808cc):

sequential  A(sent tid=1) received ['tid=1 fc=0x03']
sequential  B(sent tid=2) received ['tid=2 fc=0x03']
overlap     A(sent tid=1) received []
overlap     B(sent tid=2) received ['tid=1 fc=0x03', 'tid=2 fc=0x03']
bad-second  A(sent tid=1) received []
bad-second  B(sent tid=2) received ['tid=2 fc=0xc1']
after       C(sent tid=3) received []

with Fatal write error on datagram transport and TypeError: sendto(): AF_INET address must be tuple, not NoneType on stderr.

After:

sequential  A(sent tid=1) received ['tid=1 fc=0x03']
sequential  B(sent tid=2) received ['tid=2 fc=0x03']
overlap     A(sent tid=1) received ['tid=1 fc=0x03']
overlap     B(sent tid=2) received ['tid=2 fc=0x03']
bad-second  A(sent tid=1) received ['tid=1 fc=0x03']
bad-second  B(sent tid=2) received ['tid=2 fc=0xc1']
after       C(sent tid=3) received ['tid=3 fc=0x03']

codespell, ruff, pylint and zuban are clean. pytest test/server/test_requesthandler.py passes 17 of 17.

Merge risk

Door: two-way.
Blast radius: where ServerRequestHandler sends a response. TCP, TLS and serial servers create one handler per connection, so the address they pass is the one self.last_addr held before.
Safe because: on the server's own dispatch path the only behavior change is which address the reply goes to, and the full test suite passes on both dev (2024 passed, 7 skipped) and this branch (2026 passed, 7 skipped; the two new tests). handle_request takes the address as a new optional argument; a direct call without it sends its reply, if any, to None, the value stream transports already pass, and no call in the package or its examples omits it.
Known limit: the UDP server still keeps one receive buffer for all peers, so a truncated datagram from one client can be joined with the next client's request. That happens with and without this change.

A UDP server uses one ServerRequestHandler for all peers. callback_data
schedules each request with call_soon, and handle_request answered at
self.last_addr, which every later datagram overwrites. When a second
datagram is read before the first request runs, the first reply goes to
the second peer; when that second datagram is malformed, last_addr is
None and sendto(data, None) closes the server's transport. Pass the
request's address along with the PDU.

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

That is really a hidden bug, thanks a lot for solving this for other users.

@janiversen
janiversen merged commit 1428f03 into pymodbus-dev:dev Nov 19, 2026
31 of 32 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