[banshee] Fix race condition when starting multiple instances (bgo#648522)



commit b780ebced90e5aae3464e16fb686f25ddd21fd9b
Author: Bertrand Lorentz <bertrand lorentz gmail com>
Date:   Sun Jan 8 12:24:29 2012 +0100

    Fix race condition when starting multiple instances (bgo#648522)
    
    We were first only checking whether the DBus name had an owner, and
    grabbing the name much later in the startup process. This created a race
    condition, so two Banshee processes started at almost the same time
    could both think they were the primary instance.
    
    Add a DBusConnection.GrabDefaultName method which tries to grab the
    default DBus name and returns whether we are the primary instance or
    not. If DBus is not available we have to assume we're the primary
    instance. Use that new method in Booter.

 src/Clients/Booter/Booter/Entry.cs                 |    2 +-
 .../Banshee.ServiceStack/DBusConnection.cs         |   26 ++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletions(-)
---
diff --git a/src/Clients/Booter/Booter/Entry.cs b/src/Clients/Booter/Booter/Entry.cs
index c824315..3f87397 100644
--- a/src/Clients/Booter/Booter/Entry.cs
+++ b/src/Clients/Booter/Booter/Entry.cs
@@ -79,7 +79,7 @@ namespace Booter
                 return;
             }
 
-            if (DBusConnection.ApplicationInstanceAlreadyRunning) {
+            if (!DBusConnection.GrabDefaultName ()) {
                 // DBus Command/Query/File Proxy Client
                 BootClient ("Halie");
                 NotifyStartupComplete ();
diff --git a/src/Core/Banshee.Services/Banshee.ServiceStack/DBusConnection.cs b/src/Core/Banshee.Services/Banshee.ServiceStack/DBusConnection.cs
index 1a1912a..a72bb26 100644
--- a/src/Core/Banshee.Services/Banshee.ServiceStack/DBusConnection.cs
+++ b/src/Core/Banshee.Services/Banshee.ServiceStack/DBusConnection.cs
@@ -124,6 +124,32 @@ namespace Banshee.ServiceStack
             }
         }
 
+        // Tries to grab the default DBus service name and returns true if we're the primary instance,
+        // false if Banshee is already running. If DBus is not available, we assume we're the primary instance
+        public static bool GrabDefaultName ()
+        {
+            bool primary_instance = false;
+
+            connect_tried = true;
+
+            if (!enabled) {
+                primary_instance = true;
+            }
+
+            try {
+                if (Connect (DefaultServiceName, true) == RequestNameReply.PrimaryOwner) {
+                    active_connections.Add (DefaultServiceName);
+                    primary_instance = true;
+                }
+            } catch (Exception e) {
+                Log.Exception ("DBus support could not be started. Disabling for this session.", e);
+                enabled = false;
+                primary_instance = true;
+            }
+
+            return primary_instance;
+        }
+
         private static RequestNameReply Connect (string serviceName, bool init)
         {
             connect_tried = true;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]