Blame SOURCES/0004-decouple-disconnection-of-the-main-channel-from-clie.patch

e2c81d
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
4c08dd
From: Yonit Halperin <yhalperi@redhat.com>
4c08dd
Date: Thu, 25 Jul 2013 14:19:21 -0400
20333d
Subject: [PATCH] decouple disconnection of the main channel from client
4c08dd
 destruction
4c08dd
4c08dd
Fixes rhbz#918169
4c08dd
4c08dd
Some channels make direct calls to reds/main_channel routines. If
4c08dd
these routines try to read/write to the socket, and they get socket
4c08dd
error, main_channel_client_on_disconnect is called, and triggers
4c08dd
red_client_destroy. In order to prevent accessing expired references
4c08dd
to RedClient, RedChannelClient, or other objects (inside the original call, after
4c08dd
red_client_destroy has been called) I made the call to
4c08dd
red_client_destroy asynchronous with respect to main_channel_client_on_disconnect.
4c08dd
I added MAIN_DISPATCHER_CLIENT_DISCONNECT to main_dispatcher.
4c08dd
main_channel_client_on_disconnect pushes this msg to the dispatcher,
4c08dd
instead of calling directly to reds_client_disconnect.
4c08dd
4c08dd
The patch uses RedClient ref-count in order to handle a case where
4c08dd
reds_client_disconnect is called directly (e.g., when a new client connects while
4c08dd
another one is connected), while there is already CLIENT_DISCONNECT msg
4c08dd
pending in the main_dispatcher.
4c08dd
4c08dd
Examples:
4c08dd
(1) snd_worker.c
4c08dd
4c08dd
    snd_disconnect_channel()
4c08dd
        channel->cleanup() //snd_playback_cleanup
4c08dd
            reds_enable_mm_timer()
4c08dd
                .
4c08dd
                .
4c08dd
                main_channel_push_multi_media_time()...socket_error
4c08dd
                    .
4c08dd
                    .
4c08dd
                    red_client_destory()
4c08dd
                        .
4c08dd
                        .
4c08dd
                        snd_disconnect_channel()
4c08dd
                            channel->cleanup()
4c08dd
                                celt051_encoder_destroy()
4c08dd
            celt051_encoder_destory() // double release
4c08dd
4c08dd
Note that this bug could have been solved by changing the order of
4c08dd
calls: e.g., channel->stream = NULL before calling cleanup, and
4c08dd
some other changes + reference counting. However, I found other
4c08dd
places in the code with similar problems, and I looked for a general
4c08dd
solution, at least till we redesign red_channel to handle reference
4c08dd
counting more consistently.
4c08dd
4c08dd
(2) inputs_channel.c
4c08dd
4c08dd
    inputs_connect()
4c08dd
        main_channel_client_push_notify()...socket_error
4c08dd
                .
4c08dd
                .
4c08dd
            red_client_destory()
4c08dd
                .
4c08dd
                .
4c08dd
        red_channel_client_create() // refers to client which is already destroyed
4c08dd
4c08dd
(3) reds.c
4c08dd
4c08dd
    reds_handle_main_link()
4c08dd
       main_channel_push_init() ...socket error
4c08dd
                .
4c08dd
                .
4c08dd
            red_client_destory()
4c08dd
                .
4c08dd
                .
4c08dd
       main_channel_client_start_net_test(mcc) // refers to mcc which is already destroyed
4c08dd
4c08dd
    This can explain the assert in rhbz#964136, comment #1 (but not the hang that occurred before).
4c08dd
---
4c08dd
 server/main_channel.c    |  9 +++++----
4c08dd
 server/main_dispatcher.c | 37 +++++++++++++++++++++++++++++++++++++
4c08dd
 server/main_dispatcher.h |  7 +++++++
4c08dd
 server/reds.c            |  1 +
4c08dd
 server/reds.h            |  2 ++
4c08dd
 5 files changed, 52 insertions(+), 4 deletions(-)
4c08dd
4c08dd
diff --git a/server/main_channel.c b/server/main_channel.c
4c08dd
index 233e633..fe032a6 100644
4c08dd
--- a/server/main_channel.c
4c08dd
+++ b/server/main_channel.c
4c08dd
@@ -46,6 +46,7 @@
4c08dd
 #include "red_common.h"
4c08dd
 #include "reds.h"
4c08dd
 #include "migration_protocol.h"
4c08dd
+#include "main_dispatcher.h"
4c08dd
 
4c08dd
 #define ZERO_BUF_SIZE 4096
4c08dd
 
4c08dd
@@ -175,13 +176,13 @@ int main_channel_is_connected(MainChannel *main_chan)
4c08dd
     return red_channel_is_connected(&main_chan->base);
4c08dd
 }
4c08dd
 
4c08dd
-// when disconnection occurs, let reds shutdown all channels. This will trigger the
4c08dd
-// real disconnection of main channel
4c08dd
+/*
4c08dd
+ * When the main channel is disconnected, disconnect the entire client.
4c08dd
+ */
4c08dd
 static void main_channel_client_on_disconnect(RedChannelClient *rcc)
4c08dd
 {
4c08dd
     spice_printerr("rcc=%p", rcc);
4c08dd
-    reds_client_disconnect(rcc->client);
4c08dd
-//    red_channel_client_disconnect(rcc);
4c08dd
+    main_dispatcher_client_disconnect(rcc->client);
4c08dd
 }
4c08dd
 
4c08dd
 RedClient *main_channel_get_client_by_link_id(MainChannel *main_chan, uint32_t connection_id)
4c08dd
diff --git a/server/main_dispatcher.c b/server/main_dispatcher.c
4c08dd
index bf160dd..dbe1037 100644
4c08dd
--- a/server/main_dispatcher.c
4c08dd
+++ b/server/main_dispatcher.c
4c08dd
@@ -41,6 +41,7 @@ enum {
4c08dd
     MAIN_DISPATCHER_CHANNEL_EVENT = 0,
4c08dd
     MAIN_DISPATCHER_MIGRATE_SEAMLESS_DST_COMPLETE,
4c08dd
     MAIN_DISPATCHER_SET_MM_TIME_LATENCY,
4c08dd
+    MAIN_DISPATCHER_CLIENT_DISCONNECT,
4c08dd
 
4c08dd
     MAIN_DISPATCHER_NUM_MESSAGES
4c08dd
 };
4c08dd
@@ -59,6 +60,10 @@ typedef struct MainDispatcherMmTimeLatencyMessage {
4c08dd
     uint32_t latency;
4c08dd
 } MainDispatcherMmTimeLatencyMessage;
4c08dd
 
4c08dd
+typedef struct MainDispatcherClientDisconnectMessage {
4c08dd
+    RedClient *client;
4c08dd
+} MainDispatcherClientDisconnectMessage;
4c08dd
+
4c08dd
 /* channel_event - calls core->channel_event, must be done in main thread */
4c08dd
 static void main_dispatcher_self_handle_channel_event(
4c08dd
                                                 int event,
4c08dd
@@ -108,6 +113,16 @@ static void main_dispatcher_handle_mm_time_latency(void *opaque,
4c08dd
     red_client_unref(msg->client);
4c08dd
 }
4c08dd
 
4c08dd
+static void main_dispatcher_handle_client_disconnect(void *opaque,
4c08dd
+                                                     void *payload)
4c08dd
+{
4c08dd
+    MainDispatcherClientDisconnectMessage *msg = payload;
4c08dd
+
4c08dd
+    spice_debug("client=%p", msg->client);
4c08dd
+    reds_client_disconnect(msg->client);
4c08dd
+    red_client_unref(msg->client);
4c08dd
+}
4c08dd
+
4c08dd
 void main_dispatcher_seamless_migrate_dst_complete(RedClient *client)
4c08dd
 {
4c08dd
     MainDispatcherMigrateSeamlessDstCompleteMessage msg;
4c08dd
@@ -137,6 +152,20 @@ void main_dispatcher_set_mm_time_latency(RedClient *client, uint32_t latency)
4c08dd
                             &msg;;
4c08dd
 }
4c08dd
 
4c08dd
+void main_dispatcher_client_disconnect(RedClient *client)
4c08dd
+{
4c08dd
+    MainDispatcherClientDisconnectMessage msg;
4c08dd
+
4c08dd
+    if (!client->disconnecting) {
4c08dd
+        spice_debug("client %p", client);
4c08dd
+        msg.client = red_client_ref(client);
4c08dd
+        dispatcher_send_message(&main_dispatcher.base, MAIN_DISPATCHER_CLIENT_DISCONNECT,
4c08dd
+                                &msg;;
4c08dd
+    } else {
4c08dd
+        spice_debug("client %p already during disconnection", client);
4c08dd
+    }
4c08dd
+}
4c08dd
+
4c08dd
 static void dispatcher_handle_read(int fd, int event, void *opaque)
4c08dd
 {
4c08dd
     Dispatcher *dispatcher = opaque;
4c08dd
@@ -144,6 +173,11 @@ static void dispatcher_handle_read(int fd, int event, void *opaque)
4c08dd
     dispatcher_handle_recv_read(dispatcher);
4c08dd
 }
4c08dd
 
4c08dd
+/*
4c08dd
+ * FIXME:
4c08dd
+ * Reds routines shouldn't be exposed. Instead reds.c should register the callbacks,
4c08dd
+ * and the corresponding operations should be made only via main_dispatcher.
4c08dd
+ */
4c08dd
 void main_dispatcher_init(SpiceCoreInterface *core)
4c08dd
 {
4c08dd
     memset(&main_dispatcher, 0, sizeof(main_dispatcher));
4c08dd
@@ -160,4 +194,7 @@ void main_dispatcher_init(SpiceCoreInterface *core)
4c08dd
     dispatcher_register_handler(&main_dispatcher.base, MAIN_DISPATCHER_SET_MM_TIME_LATENCY,
4c08dd
                                 main_dispatcher_handle_mm_time_latency,
4c08dd
                                 sizeof(MainDispatcherMmTimeLatencyMessage), 0 /* no ack */);
4c08dd
+    dispatcher_register_handler(&main_dispatcher.base, MAIN_DISPATCHER_CLIENT_DISCONNECT,
4c08dd
+                                main_dispatcher_handle_client_disconnect,
4c08dd
+                                sizeof(MainDispatcherClientDisconnectMessage), 0 /* no ack */);
4c08dd
 }
4c08dd
diff --git a/server/main_dispatcher.h b/server/main_dispatcher.h
4c08dd
index 0c79ca8..522c7f9 100644
4c08dd
--- a/server/main_dispatcher.h
4c08dd
+++ b/server/main_dispatcher.h
4c08dd
@@ -7,6 +7,13 @@
4c08dd
 void main_dispatcher_channel_event(int event, SpiceChannelEventInfo *info);
4c08dd
 void main_dispatcher_seamless_migrate_dst_complete(RedClient *client);
4c08dd
 void main_dispatcher_set_mm_time_latency(RedClient *client, uint32_t latency);
4c08dd
+/*
4c08dd
+ * Disconnecting the client is always executed asynchronously,
4c08dd
+ * in order to protect from expired references in the routines
4c08dd
+ * that triggered the client destruction.
4c08dd
+ */
4c08dd
+void main_dispatcher_client_disconnect(RedClient *client);
4c08dd
+
4c08dd
 void main_dispatcher_init(SpiceCoreInterface *core);
4c08dd
 
4c08dd
 #endif //MAIN_DISPATCHER_H
4c08dd
diff --git a/server/reds.c b/server/reds.c
4c08dd
index 30d0652..c66ddc4 100644
4c08dd
--- a/server/reds.c
4c08dd
+++ b/server/reds.c
4c08dd
@@ -537,6 +537,7 @@ void reds_client_disconnect(RedClient *client)
4c08dd
     }
4c08dd
 
4c08dd
     if (!client || client->disconnecting) {
4c08dd
+        spice_debug("client %p already during disconnection", client);
4c08dd
         return;
4c08dd
     }
4c08dd
 
4c08dd
diff --git a/server/reds.h b/server/reds.h
4c08dd
index c5c557d..1c5ae84 100644
4c08dd
--- a/server/reds.h
4c08dd
+++ b/server/reds.h
4c08dd
@@ -136,6 +136,8 @@ void reds_handle_agent_mouse_event(const VDAgentMouseState *mouse_state); // use
4c08dd
 extern struct SpiceCoreInterface *core;
4c08dd
 
4c08dd
 // Temporary measures to make splitting reds.c to inputs_channel.c easier
4c08dd
+
4c08dd
+/* should be called only from main_dispatcher */
4c08dd
 void reds_client_disconnect(RedClient *client);
4c08dd
 
4c08dd
 // Temporary (?) for splitting main channel