From e0561198741b0d3a155924eca353bf7598534ca4 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Thu, 27 May 2010 13:37:55 +0200
Subject: [PATCH] Fix two race conditions in reboot instance
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If the instance crashes between backend.InstanceReboot checks the list
of running instances and the execution of hv_xen.RebootInstance,
ini_info will be None. And if the instance doesn't reboot fast enough,
new_info will be None. Both cases lead to β€œTypeError: unsubscriptable
object”. Too bad pylint doesn't detect such cases.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/hypervisor/hv_xen.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lib/hypervisor/hv_xen.py b/lib/hypervisor/hv_xen.py
index a281e6a9b..77f98b079 100644
--- a/lib/hypervisor/hv_xen.py
+++ b/lib/hypervisor/hv_xen.py
@@ -213,6 +213,10 @@ class XenHypervisor(hv_base.BaseHypervisor):
     """
     ini_info = self.GetInstanceInfo(instance.name)
 
+    if ini_info is None:
+      raise errors.HypervisorError("Failed to reboot instance %s,"
+                                   " not running" % instance.name)
+
     result = utils.RunCmd(["xm", "reboot", instance.name])
     if result.failed:
       raise errors.HypervisorError("Failed to reboot instance %s: %s, %s" %
@@ -223,7 +227,8 @@ class XenHypervisor(hv_base.BaseHypervisor):
       new_info = self.GetInstanceInfo(instance.name)
 
       # check if the domain ID has changed or the run time has decreased
-      if new_info[1] != ini_info[1] or new_info[5] < ini_info[5]:
+      if (new_info is not None and
+          (new_info[1] != ini_info[1] or new_info[5] < ini_info[5])):
         return
 
       raise utils.RetryAgain()
-- 
GitLab