qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Simon Rowe <simon.rowe@eu.citrix.com>
To: qemu-devel <qemu-devel@nongnu.org>
Subject: [Qemu-devel] [PATCH] handle bind(), listen() race
Date: Tue, 26 Apr 2011 10:52:06 +0100	[thread overview]
Message-ID: <201104261052.06128.simon.rowe@eu.citrix.com> (raw)

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


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);
             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);

                 reply	other threads:[~2011-04-26  9:52 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201104261052.06128.simon.rowe@eu.citrix.com \
    --to=simon.rowe@eu.citrix.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).