Skip to contentSkip to conversation

: VNC: set listener socket to non-blocking mode

PATCH In review Gianni Tedesco 2 participants 3 messages Oct 21, 2010 – Oct 22, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 : VNC: set listener socket to non-blocking mode

This prevents qemu from hanging waiting for a client to connect. I have reproduced this when doing a loadvm but it may be a more general problem in that poll/accept may race if a client aborts the connection with a RST before the accept has completed. In either case the fix seems harmless.

ui/vnc.c +1 -0
index 864342e..172b988 100644
@@ -2736,5 +2736,6 @@ int vnc_display_open(DisplayState *ds, const char *display)
             vs->display = dpy;
         }
     }
+    socket_set_nonblock(vs->lsock);
     return qemu_set_fd_handler2(vs->lsock, NULL, vnc_listen_read, NULL, vs);
 }
#1 lore raw
Quoted 8 lines · Gianni Tedesco

This prevents qemu from hanging waiting for a client to connect. I have reproduced this when doing a loadvm but it may be a more general problem in that poll/accept may race if a client aborts the connection with a RST before the accept has completed. In either case the fix seems harmless.

I'd feel a little better with explicit error handling in the accept() path and a comment explaining why this was necessary.

But otherwise, nice catch!

Regards,

Anthony Liguori

Quoted 15 lines
ui/vnc.c +1 -0
index 864342e..172b988 100644
@@ -2736,5 +2736,6 @@ int vnc_display_open(DisplayState *ds, const char *display)
              vs->display = dpy;
          }
      }
+    socket_set_nonblock(vs->lsock);
      return qemu_set_fd_handler2(vs->lsock, NULL, vnc_listen_read, NULL, vs);
  }
#2 lore raw
Quoted 7 lines · Anthony Liguori
Quoted 6 lines · Gianni Tedesco

This prevents qemu from hanging waiting for a client to connect. I have reproduced this when doing a loadvm but it may be a more general problem in that poll/accept may race if a client aborts the connection with a RST before the accept has completed. In either case the fix seems harmless.

Quoted 7 lines
Quoted 2 lines

I'd feel a little better with explicit error handling in the accept() path and a comment explaining why this was necessary.

But otherwise, nice catch!

Thanks!

Not sure what you mean about the explicit error handling in the accept() path though? Accept can fail for a few reasons, the most worrying is EMFILE which easily leads to livelock. Poll reports listener as ready to accept but accept fails because we maxed out the fd table so go back to poll and the listener is still ready to accept etc... It's not always clear how to handle specific accept() errors.

I think only calling vnc_connect() when we have an fd is the best we can hope for right now.

Unless I misunderstood?

-----8<-----8<-----8<-----

ui/vnc.c +4 -0
index 864342e..65dc55c 100644
@@ -2736,5 +2736,9 @@ int vnc_display_open(DisplayState *ds, const char *display)
             vs->display = dpy;
         }
     }
+    /* necessary to prevent accept() hanging indefinitely if a clients
+     * connection-reset wins the race between poll() and accept()
+     */
+    socket_set_nonblock(vs->lsock);
     return qemu_set_fd_handler2(vs->lsock, NULL, vnc_listen_read, NULL, vs);
 }