[Spice-devel] [PATCH spice-gtk] session: timeout after 10s of socket connect() attempt

Marc-André Lureau marcandre.lureau at gmail.com
Mon May 13 09:43:26 PDT 2013


https://bugzilla.redhat.com/show_bug.cgi?id=885101
---
 gtk/spice-session.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/gtk/spice-session.c b/gtk/spice-session.c
index 8f4e1bd..51a75e4 100644
--- a/gtk/spice-session.c
+++ b/gtk/spice-session.c
@@ -1693,6 +1693,9 @@ struct spice_open_host {
     GError *error;
     GSocketConnection *connection;
     GSocketClient *client;
+#if !GLIB_CHECK_VERSION(2,26,0)
+    guint timeout_id;
+#endif
 };
 
 static void socket_client_connect_ready(GObject *source_object, GAsyncResult *result,
@@ -1795,6 +1798,20 @@ static gboolean open_host_idle_cb(gpointer data)
     return FALSE;
 }
 
+#define SOCKET_TIMEOUT 10
+
+#if !GLIB_CHECK_VERSION(2,26,0)
+static gboolean connect_timeout(gpointer data)
+{
+    spice_open_host *open_host = data;
+
+    open_host->timeout_id = 0;
+    coroutine_yieldto(open_host->from, NULL);
+
+    return FALSE;
+}
+#endif
+
 /* coroutine context */
 G_GNUC_INTERNAL
 GSocketConnection* spice_session_channel_open_host(SpiceSession *session, SpiceChannel *channel,
@@ -1827,11 +1844,27 @@ GSocketConnection* spice_session_channel_open_host(SpiceSession *session, SpiceC
     }
 
     open_host.client = g_socket_client_new();
+#if GLIB_CHECK_VERSION(2,26,0)
+    g_socket_client_set_timeout(open_host.client, SOCKET_TIMEOUT);
+#else
+    open_host.timeout_id =
+        g_timeout_add_seconds(SOCKET_TIMEOUT, connect_timeout, &open_host);
+#endif
 
     guint id = g_idle_add(open_host_idle_cb, &open_host);
     /* switch to main loop and wait for connection */
     coroutine_yield(NULL);
-    g_source_remove (id);
+    g_source_remove(id);
+
+#if GLIB_CHECK_VERSION(2,26,0)
+    g_socket_client_set_timeout(open_host.client, 0);
+#else
+    if (open_host.timeout_id == 0)
+        open_host.error = g_error_new(SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
+                                      "connect timed out");
+    else
+        g_source_remove(open_host.timeout_id);
+#endif
 
     if (open_host.error != NULL) {
         g_warning("%s", open_host.error->message);
-- 
1.8.2.1.342.gfa7285d



More information about the Spice-devel mailing list