From dd42d99d51b6360a2a091a93d7a3ec61235f3039 Mon Sep 17 00:00:00 2001
From: David Lehman <dlehman@redhat.com>
Date: Tue, 12 May 2015 14:46:05 -0500
Subject: [PATCH 41/51] Consolidate synchronization code in _notifyDevice.

---
 blivet/handler.py | 43 ++++++++++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 19 deletions(-)

diff --git a/blivet/handler.py b/blivet/handler.py
index 8456313..73ae2b3 100644
--- a/blivet/handler.py
+++ b/blivet/handler.py
@@ -475,6 +475,7 @@ class EventHandler(object):
         # might have just been created, in which case it might not have a
         # meaningful sysfs path (for dm and md they aren't predictable).
         device = self.devicetree.getDeviceByName(name)
+        event_sync = None
         if device is None and name.startswith("md"):
             # XXX HACK md devices have no symbolic name by the time they are
             #          removed, so we have to look it up by sysfs path
@@ -494,16 +495,11 @@ class EventHandler(object):
             # Only wait briefly since there is a possibility the device already
             # notified the cv from _postCreate.
             event_sync.wait(1) # wait until the device says it's ready
-            event_sync.notify() # notify the device that we received the event
-            event_sync.wait() # wait for the device to postprocess the event
-            ret = True
         elif action in ("add", "change"):
             # could be any number of operations on a device that may no longer
             # be in the tree
             # device setup could be add or change event
             # all other operations will be change event
-            event_sync = None
-            name = getattr(device, "name", None)
             if device and device.controlSync.starting and \
                not device.delegateControlEvents:
                 ## device setup
@@ -511,6 +507,7 @@ class EventHandler(object):
                 # update sysfsPath since the device will not have access to it
                 # until later in the change handler
                 device.sysfsPath = sysfs_path
+                name = device.name
                 event_sync = device.controlSync
             elif device and device.controlSync.stopping and \
                  device.name.startswith("loop"):
@@ -518,6 +515,7 @@ class EventHandler(object):
                 # XXX HACK You don't get a remove event when you deactivate a
                 #          loop device.
                 log.debug("* teardown %s", device.name)
+                name = device.name
                 event_sync = device.controlSync
             elif action == "change" and device and device.modifySync.resizing:
                 ## device resize
@@ -578,12 +576,15 @@ class EventHandler(object):
                     if current_action.device.controlSync.starting and \
                        not current_action.device.delegateControlEvents:
                         ## setup of device scheduled for destruction
+                        name = current_action.device.name
                         log.debug("** setup %s", current_action.device.name)
                         event_sync = current_action.device.controlSync
                     elif action == "change" and \
                          current_action.device.modifySync.destroying and \
-                         current_action.device.modifySync.validate(info):
+                         current_action.device.modifySync.validate(info) and \
+                         current_action.device.delegateModifyEvents:
                         # change to pv when removing lv or vg
+                        name = current_action.device.name
                         log.debug("** destroy %s", current_action.device.name)
                         event_sync = current_action.device.modifySync
                     elif action == "change" and \
@@ -591,20 +592,15 @@ class EventHandler(object):
                          current_action.format.eventSync.validate(info):
                         ## any change to a format on a device scheduled for
                         ## destruction
+                        name = current_action.device.name
                         log.debug("** change %s format", current_action.device.name)
                         event_sync = current_action.format.eventSync
 
-            if event_sync:
-                event_sync.notify()
-                event_sync.wait()
-                ret = True
         elif action == "remove" and device and device.controlSync.stopping and \
              not device.delegateControlEvents:
             ## device teardown
             log.debug("* teardown %s", device.name)
-            device.controlSync.notify()
-            device.controlSync.wait()
-            ret = True
+            event_sync = device.controlSync
         elif action == "remove":
             ## device destroy
             current_action = None
@@ -619,11 +615,20 @@ class EventHandler(object):
                 device = current_action.device
                 log.debug("* destroy %s", device.name)
                 event_sync = device.modifySync
-                # Only wait briefly since it's possible the device already
-                # notified the cv from _postDestroy.
-                event_sync.wait(1) # wait until the device says it's ready
-                event_sync.notify() # notify device that we received the event
-                event_sync.wait() # wait for the device to postprocess the event
-                ret = True
+            elif current_action and \
+                 current_action.device.exists and \
+                 current_action.device.controlSync.stopping and \
+                 not current_action.device.delegateControlEvents:
+                log.debug("*** teardown %s", current_action.device.name)
+                event_sync = current_action.device.controlSync
+
+        if event_sync is not None:
+            event_sync.reset()
+            print "notify " + name
+            event_sync.notify()
+            print "wait " + name
+            event_sync.wait()
+            print "done " + name
+            ret = True
 
         return ret
-- 
2.1.0

