From mboxrd@z Thu Jan 1 00:00:00 1970 From: Anthony Liguori Subject: [PATCH 2/2] Make sure that TCP binds don't fail because of stale state Date: Wed, 14 Sep 2005 20:38:27 -0500 Message-ID: <4328D093.9080103@us.ibm.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------090205010501060108050104" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel List-Id: xen-devel@lists.xenproject.org This is a multi-part message in MIME format. --------------090205010501060108050104 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit SO_REUSEADDR does not immediately free up a port when a server is killed with a signal (which is what we do for xend stop). Since there is no great way (at least, that I know of) to deal with this, the following patch implements a 30 second timeout for bind to help make it highly unlikely that we'll get burned by this. On a very slow system, killing xend with a signal takes less than a second for the port to become available again so 30 seconds seems really safe. With this patch (and the previous one), I can execute the following: while true; do xend restart && xm list || break; done And it runs without any trouble (I ran it for an hour before finally giving up). Regards, Anthony Liguori --------------090205010501060108050104 Content-Type: text/x-patch; name="6819_xend-race.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="6819_xend-race.diff" # HG changeset patch # User anthony@localhost.localdomain # Node ID 13d6ca9c35dd02770901b9a8f9f2e53c7ed500c5 # Parent 8c866d412673e4169386ee5c96ba10f20f54bf6c SO_REUSEADDR is not enough to ensure we don't get address in use errors when xend dies from a signal. We implement a 30 second time to ensure we don't fail if we don't have to. Signed-off-by: Anthony Liguori diff -r 8c866d412673 -r 13d6ca9c35dd tools/python/xen/web/tcp.py --- a/tools/python/xen/web/tcp.py Wed Sep 14 23:47:37 2005 +++ b/tools/python/xen/web/tcp.py Thu Sep 15 00:58:12 2005 @@ -18,6 +18,7 @@ import sys import socket import types +import time from connection import * from protocol import * @@ -35,8 +36,25 @@ def createSocket(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - addr = (self.interface, self.port) - sock.bind(addr) + + # SO_REUSEADDR does not always ensure that we do not get an address + # in use error when restarted quickly + # we implement a timeout to try and avoid failing unnecessarily + + timeout = time.time() + 30 + again = True + while again and time.time() < timeout: + again = False + try: + sock.bind((self.interface, self.port)) + except socket.error, (errno, strerrno): + if errno == 98: + again = True + else: + raise socket.error(errno, strerrno) + if again: + raise socket.error(98, "address in use") + return sock def acceptConnection(self, sock, protocol, addr): --------------090205010501060108050104 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --------------090205010501060108050104--