From 4c4dc3a6d331426e472e2dd1e9b0513da7cb7450 Mon Sep 17 00:00:00 2001 From: Andrew Laski Date: Mon, 11 Aug 2014 14:36:30 -0400 Subject: [PATCH] Lock attach_volume There are some issues with instance and volume cleanup when the volume is not in a fully attached state so it will be safer to not attempt a terminate_instance while there are attachments in progress. Change-Id: I4347794e51004a881bf4ef5ee30f65ac28773e51 Closes-Bug: #1355348 --- nova/compute/manager.py | 15 ++++++++++----- nova/tests/compute/test_compute.py | 3 +-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/nova/compute/manager.py b/nova/compute/manager.py index 660785e1adf4..b22322c21ec2 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -4290,11 +4290,16 @@ class ComputeManager(manager.Manager): bdm = objects.BlockDeviceMapping.get_by_volume_id( context, volume_id) driver_bdm = driver_block_device.DriverVolumeBlockDevice(bdm) - try: - return self._attach_volume(context, instance, driver_bdm) - except Exception: - with excutils.save_and_reraise_exception(): - bdm.destroy(context) + + @utils.synchronized(instance.uuid) + def do_attach_volume(context, instance, driver_bdm): + try: + return self._attach_volume(context, instance, driver_bdm) + except Exception: + with excutils.save_and_reraise_exception(): + bdm.destroy(context) + + do_attach_volume(context, instance, driver_bdm) def _attach_volume(self, context, instance, bdm): context = context.elevated() diff --git a/nova/tests/compute/test_compute.py b/nova/tests/compute/test_compute.py index 8991a7ab6067..b8960d894a9b 100644 --- a/nova/tests/compute/test_compute.py +++ b/nova/tests/compute/test_compute.py @@ -4336,9 +4336,8 @@ class ComputeTestCase(BaseTestCase): fake_attach_volume) # attach volume to instance - instance_p = obj_base.obj_to_primitive(instance) self.compute.attach_volume(self.context, volume['id'], - '/dev/vdc', instance_p, bdm=bdm) + '/dev/vdc', instance, bdm=bdm) # assert volume attached correctly self.assertEqual(volume['device_name'], '/dev/vdc')