From 3782acd744a135934660c8dc17ec3f579231ba79 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Thu, 22 Oct 2009 15:49:23 +0900
Subject: [PATCH] Try to reduce wrong errors in InstanceShutdown

In backend.InstanceShutdown(), there is a race condition between
checking that the instance exists and trying to shut it down which
translates sometime in error messages like:

Tue Oct 20 20:08:30 2009 - WARNING: Could not shutdown instance: Failed
to force stop instance instance9: Failed to stop instance instance9:
exited with exit code 1, Error: Domain 'instance9' does not exist.

To fix this, we ignore any hypervisor StopInstance() errors if the
instance doesn't exist anymore, since our purpose (to make the instance
go away) is already accomplished.

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

diff --git a/lib/backend.py b/lib/backend.py
index 095771489..8c906c9e6 100644
--- a/lib/backend.py
+++ b/lib/backend.py
@@ -1001,6 +1001,10 @@ def InstanceShutdown(instance, timeout):
     try:
       hyper.StopInstance(instance, retry=tried_once)
     except errors.HypervisorError, err:
+      if instance.name not in hyper.ListInstances():
+        # if the instance is no longer existing, consider this a
+        # success and go to cleanup
+        break
       _Fail("Failed to stop instance %s: %s", iname, err)
     tried_once = True
     time.sleep(sleep_time)
@@ -1013,7 +1017,10 @@ def InstanceShutdown(instance, timeout):
     try:
       hyper.StopInstance(instance, force=True)
     except errors.HypervisorError, err:
-      _Fail("Failed to force stop instance %s: %s", iname, err)
+      if instance.name in hyper.ListInstances():
+        # only raise an error if the instance still exists, otherwise
+        # the error could simply be "instance ... unknown"!
+        _Fail("Failed to force stop instance %s: %s", iname, err)
 
     time.sleep(1)
     if instance.name in GetInstanceList([hv_name]):
-- 
GitLab