From 00c599319577730fcd085d9645fbe14a7f61c4ee Mon Sep 17 00:00:00 2001 From: huangtianhua Date: Thu, 5 Feb 2015 15:29:54 +0800 Subject: [PATCH] Release stack lock when successfully acquire Should not to release the stack lock if acquire failed, such as stack action in progress. Change-Id: I2a519c47306065b0a3412a8c6faa64006dea691f Closes-Bug: #1418360 --- heat/engine/stack_lock.py | 2 ++ heat/tests/test_stack_lock.py | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/heat/engine/stack_lock.py b/heat/engine/stack_lock.py index 9d5fb7837..588a54d90 100644 --- a/heat/engine/stack_lock.py +++ b/heat/engine/stack_lock.py @@ -132,6 +132,8 @@ class StackLock(object): try: self.acquire() yield + except exception.ActionInProgress: + raise except: # noqa with excutils.save_and_reraise_exception(): self.release(stack_id) diff --git a/heat/tests/test_stack_lock.py b/heat/tests/test_stack_lock.py index 01b0c331a..017a1318a 100644 --- a/heat/tests/test_stack_lock.py +++ b/heat/tests/test_stack_lock.py @@ -145,7 +145,7 @@ class StackLockTest(common.HeatTestCase): self.assertRaises(exception.ActionInProgress, slock.acquire) self.m.VerifyAll() - def test_thread_lock_context_mgr_exception(self): + def test_thread_lock_context_mgr_exception_acquire_success(self): db_api.stack_lock_create = mock.Mock(return_value=None) db_api.stack_lock_release = mock.Mock(return_value=None) slock = stack_lock.StackLock(self.context, self.stack, self.engine_id) @@ -157,6 +157,18 @@ class StackLockTest(common.HeatTestCase): self.assertRaises(self.TestThreadLockException, check_thread_lock) self.assertEqual(1, db_api.stack_lock_release.call_count) + def test_thread_lock_context_mgr_exception_acquire_fail(self): + db_api.stack_lock_create = mock.Mock(return_value=self.engine_id) + db_api.stack_lock_release = mock.Mock() + slock = stack_lock.StackLock(self.context, self.stack, self.engine_id) + + def check_thread_lock(): + with slock.thread_lock(self.stack.id): + self.assertEqual(1, db_api.stack_lock_create.call_count) + raise exception.ActionInProgress + self.assertRaises(exception.ActionInProgress, check_thread_lock) + assert not db_api.stack_lock_release.called + def test_thread_lock_context_mgr_no_exception(self): db_api.stack_lock_create = mock.Mock(return_value=None) db_api.stack_lock_release = mock.Mock(return_value=None)