Repository navigation
Answer each UDP request at the address it came from - #3091
Merged
Merged
Conversation
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
approved these changes
Nov 19, 2026
janiversen
left a comment
Collaborator
There was a problem hiding this comment.
LGTM, thanks.
That is really a hidden bug, thanks a lot for solving this for other users.
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.
Why
A
ModbusUdpServerwith more than one client can send a reply to the wrong client. The server uses oneServerRequestHandlerfor every UDP peer.callback_dataqueues each request withcall_soon, andhandle_requestthen answers atself.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_addris stillNonewhen the first reply is sent.sendto(data, None)raisesTypeError, 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_dataqueueshandle_later(pdu, addr)with the address the PDU arrived from, andhandle_request(pdu, addr=None)sends the response to that address instead ofself.last_addr.test/server/test_requesthandler.py:test_rh_reply_goes_to_request_senderfeeds 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 ondev.Evidence
A
ModbusUdpServeron 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 (
dev19808cc):with
Fatal write error on datagram transportandTypeError: sendto(): AF_INET address must be tuple, not NoneTypeon stderr.After:
codespell, ruff, pylint and zuban are clean.
pytest test/server/test_requesthandler.pypasses 17 of 17.Merge risk
Door: two-way.
Blast radius: where
ServerRequestHandlersends a response. TCP, TLS and serial servers create one handler per connection, so the address they pass is the oneself.last_addrheld 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_requesttakes the address as a new optional argument; a direct call without it sends its reply, if any, toNone, 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.