Repository navigation
SocketServer.DatagramRequestHandler with empty response #45276
Description
Activity
I seem to have misunderstood SocketServer completely, maybe someone can tell me if I was using the wrong thing after all.
I wanted to implement an UDP protocol in Python. For sending and receiving UDP packets, I chose SocketServer.UDPSocketServer along with the DatagramRequestHandler.
It is probably implied, but did not become clear to me just by reading the documentation, that apparently a server is meant to send back a packet for every packet it receives, no matter what. Which in turn means that you can't have servers talk to other servers, because they'd be sending packets back and forth endlessly then.
At least, that's what the DatagramRequestHandler is doing. It issues a socket.sendto in its finish() routine. Took me a while to find out where all the empty packets where coming from...
Does it make sense at all for an UDP server to do this? Sure, it works for simple protocols that do nothing but query/answer. But it's really useless for anything else.
The documentation could be more verbose on this behaviour, in any case.
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Aug 4, 2007 I've just bumped into this issue. In my opinion the finish() method
should only do anything if wfile is not empty, i.e:temp = self.wfile.getvalue() if temp: self.socket.sendto(temp, self.client_address)
Can you provide a working patch with a unit test and doc updates?
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jan 23, 2008 Main issue here (as I see it) is that StreamRequestHandler and
DatagramRequestHandler should behave in the same way. This is not the
case in Python 2.5.1 for the case where the handle method does not
respond to the request socket (e.g. in my case it is forwarding data to
a different socket).While handler methods in StreamRequestHandler need not send any data
back to the request socket, in DatagramRequestHandlers an attempt will
be made to send data whether any is available or not. This causes a
socket hang (for several minutes) on Windows with a '10040 Message too
long' error.By only sending data back to the request if the handler has written to
wfile, this is avoided, giving the twin fixes of avoiding a nasty
socket error and providing compatibilty with StreamRequestHandler
behaviour.Test has been updated to add tests of handlers which do not respond to
the request; this causes a hang in Python2.5.1 stock (not sure how to
avoid this and cleanly fail), test passes with changed SocketServer.p.s. this is my first patch submission to anything, so go easy :-)
Sorry that nobody has responded to your patch in almost a decade. :( Unfortunately in the meantime the test file seems to have been completely rewritten.
For the original problem, I think it would be better if we just documented that this is the way it works. Sending an empty response message seems like a plausible way to acknowledge handling of a request. This parallels the stream handler only shutting down the connection when the request handler has finished. Changing this could break compatibility.
Ben: if you’re still interested, can you explain what the problem is on Windows? My experiments with Wine on Linux suggest that it can send empty UDP responses fine. Is Windows able to send and receive zero-length UDP messages with plain sockets? Perhaps this should be a separate bug.
- changed the title
[-]SocketServer.DatagramRequestHandler[/-][+]SocketServer.DatagramRequestHandler with empty response[/+]on Feb 9, 2016 - addeddocsDocumentation in the Doc dirDocumentation in the Doc dir3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of life3.10 (EOL)end of lifeend of life
on Nov 4, 2020 - addedpendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is providedand removed3.10 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life
on Sep 21, 2025 This one is waiting for feedback for 9 years, so unless someone proposes keeping it open (and provides tests, updated patch, etc.), it will soon be closed.
- addedtopic-socketRelated to the socket, socketserver and selectors modulesRelated to the socket, socketserver and selectors modules
on Jul 3, 2026 Still reproduces at tip (3.16):
DatagramRequestHandler.finish()callssendto()unconditionally, so a handler that writes nothing towfilestill sends a zero-byte datagram back. But that matches vadmium's 2016 read — he called the empty datagram a plausible way to acknowledge a request, and thought making the send conditional could break compatibility. Nothing new has arrived since (nobody answered vadmium's 2016 Windows question, and that hang was never reproduced separately). Looks closable as not planned; the one actionable leftover would be the short doc note vadmium suggested in 2016 — a line inDoc/library/socketserver.rstsayingDatagramRequestHandleralways sends a reply datagram, empty if the handler wrote nothing. I can send that if it's wanted.Quick check at current main (6df4993)
import socket, socketserver, sys, threading print(sys.version) class NoReplyHandler(socketserver.DatagramRequestHandler): def handle(self): pass # write nothing to wfile srv = socketserver.UDPServer(("127.0.0.1", 0), NoReplyHandler) threading.Thread(target=srv.handle_request, daemon=True).start() c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) c.settimeout(5) c.sendto(b"ping", srv.server_address) data, addr = c.recvfrom(1024) print("reply received anyway: len =", len(data), "data =", data) srv.server_close()
3.16.0a0 (remotes/upstream/HEAD:6df4993bb64, Jul 19 2026, 20:24:53) [Clang 21.0.0 (clang-2100.1.1.101)] reply received anyway: len = 0 data = b''
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsTodo
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: