From patchwork Tue Apr 26 09:52:06 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Rowe X-Patchwork-Id: 92902 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 5B730B6F19 for ; Tue, 26 Apr 2011 19:55:18 +1000 (EST) Received: from localhost ([::1]:50183 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QEezA-0004G3-Sh for incoming@patchwork.ozlabs.org; Tue, 26 Apr 2011 05:55:12 -0400 Received: from eggs.gnu.org ([140.186.70.92]:56704) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QEewI-00043j-Ao for qemu-devel@nongnu.org; Tue, 26 Apr 2011 05:52:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QEewH-0004YB-8e for qemu-devel@nongnu.org; Tue, 26 Apr 2011 05:52:14 -0400 Received: from smtp02.citrix.com ([66.165.176.63]:56086) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QEewG-0004Y1-U0 for qemu-devel@nongnu.org; Tue, 26 Apr 2011 05:52:13 -0400 X-IronPort-AV: E=Sophos;i="4.64,267,1301889600"; d="scan'208";a="146168310" Received: from ftlpmailmx01.citrite.net ([10.13.107.65]) by FTLPIPO02.CITRIX.COM with ESMTP/TLS/RC4-MD5; 26 Apr 2011 05:52:07 -0400 Received: from celebrindal.localnet (10.80.2.52) by FTLPMAILMX01.citrite.net (10.13.107.65) with Microsoft SMTP Server id 8.3.137.0; Tue, 26 Apr 2011 05:52:07 -0400 From: Simon Rowe To: qemu-devel Date: Tue, 26 Apr 2011 10:52:06 +0100 User-Agent: KMail/1.13.3 (Linux/2.6.33.7-desktop586-2mnb; KDE/4.4.3; i686; ; ) MIME-Version: 1.0 Message-ID: <201104261052.06128.simon.rowe@eu.citrix.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 66.165.176.63 Subject: [Qemu-devel] [PATCH] handle bind(), listen() race X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Hello, we've seen a very occasional failure in the startup of qemu where the call to inet_listen() for the VNC port fails with EADDRINUSE. I believe there is a race condition when two qemu processes both bind to the same port, in one the subsequent call to listen() will succeed and the other fail. Below is a patch which appears to deal with this scenario but we would appreciate any comments on it. Thanks Simon if (!try_next || sockets_debug) @@ -201,13 +214,7 @@ freeaddrinfo(res); return -1; -listen: - if (listen(slisten,1) != 0) { - perror("listen"); - closesocket(slisten); - freeaddrinfo(res); - return -1; - } +listened: snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset); qemu_opt_set(opts, "host", uaddr); qemu_opt_set(opts, "port", uport); diff -ur qemu-0.14.0-org/qemu-sockets.c qemu-0.14.0/qemu-sockets.c --- qemu-0.14.0-org/qemu-sockets.c 2011-02-16 14:44:05.000000000 +0000 +++ qemu-0.14.0/qemu-sockets.c 2011-04-26 10:41:14.472067346 +0100 @@ -158,6 +158,7 @@ /* create socket + bind */ for (e = res; e != NULL; e = e->ai_next) { +rebind: getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, uaddr,INET6_ADDRSTRLEN,uport,32, NI_NUMERICHOST | NI_NUMERICSERV); @@ -182,7 +183,19 @@ if (sockets_debug) fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__, inet_strfamily(e->ai_family), uaddr, inet_getport(e)); - goto listen; + if (listen(slisten,1) == 0) { + goto listened; + } + else { + int err = errno; + + perror("listen"); + closesocket(slisten); + if (err == EADDRINUSE) + goto rebind; + freeaddrinfo(res); + return -1; + } } try_next = to && (inet_getport(e) <= to + port_offset);