This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: SocketServer.DatagramRequestHandler with empty response
Type: behavior Stage: needs patch
Components: Documentation, Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: alzheimer, bpb, christian.heimes, desbma, docs@python, giampaolo.rodola, martin.panter
Priority: normal Keywords:

Created on 2007-08-04 09:21 by alzheimer, last changed 2022-04-11 14:56 by admin.

Files
File name Uploaded Description Edit
DatagramServer.diff bpb, 2008-01-23 14:32 patch & test for proposed new behaviour
Messages (5)
msg32586 - (view) Author: Alzheimer (alzheimer) Date: 2007-08-04 09:21
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.
msg61578 - (view) Author: Ben Bass (bpb) Date: 2008-01-23 10:55
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)
msg61579 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-01-23 11:07
Can you provide a working patch with a unit test and doc updates?
msg61582 - (view) Author: Ben Bass (bpb) Date: 2008-01-23 14:32
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 :-)
msg259958 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-02-09 22:20
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.
History
Date User Action Args
2022-04-11 14:56:25adminsetgithub: 45276
2020-11-04 11:42:06iritkatrielsetversions: + Python 3.8, Python 3.9, Python 3.10, - Python 2.6
nosy: + docs@python

assignee: docs@python
components: + Documentation
stage: patch review -> needs patch
2016-02-22 20:43:01desbmasetnosy: + desbma
2016-02-09 22:20:34martin.pantersetnosy: + martin.panter
title: SocketServer.DatagramRequestHandler -> SocketServer.DatagramRequestHandler with empty response
messages: + msg259958

stage: patch review
2012-03-27 14:34:41giampaolo.rodolasetnosy: + giampaolo.rodola
2010-06-09 21:37:24terry.reedysetversions: - Python 2.5
2008-01-23 14:32:27bpbsetfiles: + DatagramServer.diff
messages: + msg61582
2008-01-23 11:07:07christian.heimessettype: behavior
messages: + msg61579
nosy: + christian.heimes
versions: + Python 2.6
2008-01-23 10:55:31bpbsetnosy: + bpb
messages: + msg61578
2007-08-04 09:21:27alzheimercreate