Issue 26403: Catch FileNotFoundError in socketserver.DatagramRequestHandler (original) (raw)

When using socketserver to create a simple server for Unix Domain sockets (see server_dgram.py), and when sending data with a client that immediately shuts down (without waiting for a response, on Linux I test with 'echo data | nc -Uu -w 0 /tmp/s.socket') I get this exception:

Exception happened during processing of request from /tmp/nc.XXXXsuGc1C Traceback (most recent call last): File "/usr/lib/python3.4/socketserver.py", line 617, in process_request_thread self.finish_request(request, client_address) File "/usr/lib/python3.4/socketserver.py", line 344, in finish_request self.RequestHandlerClass(request, client_address, self) File "/usr/lib/python3.4/socketserver.py", line 675, in init self.finish() File "/usr/lib/python3.4/socketserver.py", line 752, in finish self.socket.sendto(self.wfile.getvalue(), self.client_address) FileNotFoundError: [Errno 2] No such file or directory

The attached patch fixes this by checking if there is something to send before calling sendto.

Also I am wondering if we should catch FileNotFoundError (and possibly other exceptions) here, because with TCP or UDP, the server does not raise any exception if client is disconnected when finish is called in the handler.

Thank you

This change has already been proposed in Issue 1767511, which I recently replied to. I think it is valid to send an empty datagram back to the client, and not doing so could break existing code.

Also see Issue 5824 about removing the comment about recvfrom() on Linux.

Your FileNotFoundError is caused by the client removing its receiving socket file. This would also occur if the server was trying to send a non-zero datagram.

IMO this is usually an asynchronous error that is beyond the control of the server. In the TCP case, you can get similar errors if the client disconnected (BrokenPipeError, ConnectionResetError, etc). But these “error” conditions might only be detected after the server has closed its socket, which is why you don’t always see them.