Self test for TCP half-closed handling
This patch improves the echo server program half-closed handling.
When the client socket closes ensure that data received from the
client is echoed back to the client before closing the socket
from the server end.
@@ -230,7 +230,25 @@ class TcpEchoServer:
split_chunk_for_display(recv_data)))
sel.modify(sock, selectors.EVENT_READ | selectors.EVENT_WRITE, data=data)
else:
- logger.log('%s Closing connection to %s:%d' % (self.prefix, data.addr[0], data.addr[1]))
+ while data.outb:
+ logger.log('%s Client closed: flush client input to %s:%d' % (self.prefix, data.addr[0], data.addr[1]))
+ try:
+ sent = sock.send(data.outb)
+ data.outb = data.outb[sent:]
+ except IOError:
+ logger.log('%s Connection to %s:%d IOError: %s' %
+ (self.prefix, data.addr[0], data.addr[1], traceback.format_exc()))
+ sel.unregister(sock)
+ sock.close()
+ return 0
+ except Exception:
+ self.error = ('%s Connection to %s:%d exception: %s' %
+ (self.prefix, data.addr[0], data.addr[1], traceback.format_exc()))
+ logger.log(self.error)
+ sel.unregister(sock)
+ sock.close()
+ return 1
+ logger.log('%s Client closed: closing connection to %s:%d' % (self.prefix, data.addr[0], data.addr[1]))
sel.unregister(sock)
sock.close()
return 0
