Fix two race conditions:
1) make sure that the xpra socket exists before trying to attach to it
2) make sure that the client has attached before we start the application

The fix for '1' solves a problem when the system is under load and the
one for '2' fixes a problem with firefox starting too soon and not
having system themes applied.

-- 
Jamie Strandboge                 http://www.ubuntu.com/
Description: Fix two race conditions:
 1) make sure that the xpra socket exists before trying to attach to it 
 2) make sure that the client has attached before we start the application

Acked-By: Jamie Strandboge <[email protected]>

=== modified file 'utils/apparmor/sandbox.py'
--- utils/apparmor/sandbox.py	2013-01-14 14:55:53 +0000
+++ utils/apparmor/sandbox.py	2013-02-15 16:41:36 +0000
@@ -1,6 +1,6 @@
 # ------------------------------------------------------------------
 #
-#    Copyright (C) 2011-2012 Canonical Ltd.
+#    Copyright (C) 2011-2013 Canonical Ltd.
 #
 #    This program is free software; you can redistribute it and/or
 #    modify it under the terms of version 2 of the GNU General Public
@@ -15,6 +15,7 @@
 import pwd
 import re
 import signal
+import socket
 import sys
 import tempfile
 import time
@@ -605,13 +606,36 @@
         self.pids.append(listener_x)
 
         started = False
-        time.sleep(0.5) # FIXME: detect if running
-        for i in range(self.timeout): # 5 seconds to start
+
+        # We need to wait for the xpra socket to exist before attaching
+        fn = os.path.join(os.environ['HOME'], '.xpra', '%s-%s' % \
+                          (socket.gethostname(), self.display.split(':')[1]))
+        for i in range(self.timeout * 2): # up to self.timeout seconds to start
+            if os.path.exists(fn):
+                debug("Found '%s'! Proceeding to attch" % fn)
+                break
+            debug("'%s' doesn't exist yet, waiting" % fn)
+            time.sleep(0.5)
+
+        if not os.path.exists(fn):
+            sys.stdout.flush()
+            self.cleanup()
+            raise AppArmorException("Could not start xpra (try again with -d)")
+
+        for i in range(self.timeout): # Up to self.timeout seconds to start
             rc, out = cmd(['xpra', 'list'])
-            if 'LIVE session at %s' % self.display in out:
+
+            if 'DEAD session at %s' % self.display in out:
+                error("xpra session at '%s' died" % self.display, do_exit=False)
+                break
+
+            search = 'LIVE session at %s' % self.display
+            if search in out:
                 started = True
                 break
             time.sleep(1)
+            debug("Could not find '%s' in:\n" % search)
+            debug(out)
 
         if not started:
             sys.stdout.flush()
@@ -638,6 +662,15 @@
 
         self.pids.append(listener_attach)
 
+        # Make sure that a client has attached
+        for i in range(self.timeout): # up to self.timeout seconds to attach
+            time.sleep(1)
+            rc, out = cmd (['xpra', 'info', self.display])
+            if 'clients=1' in out:
+                break
+            debug("Client not connected yet, waiting")
+            debug(out)
+
         msg("TODO: filter '~/.xpra/run-xpra'")
 
 

Attachment: signature.asc
Description: OpenPGP digital signature

-- 
AppArmor mailing list
[email protected]
Modify settings or unsubscribe at: 
https://lists.ubuntu.com/mailman/listinfo/apparmor

Reply via email to