Skip to content

Commit 738774b

Browse files
committed
Avoid logging traceback when detach device not found
We use the oslo.utils save_and_reraise_exception context manager in our detach device code and catch specific exceptions that mean 'not found' and raise DeviceNotFound instead. When we do that, the save_and_reraise_exception context manager logs an ERROR traceback of the original exception, for informational purposes. This is misleading when trying to debug other issues, as it makes it look like the caught exception caused a problem. This passes the reraise=False keyword arg to the context manager and sets the 'reraise' attribute to True only if we are not going to raise a different exception. Related-Bug: #1836212 Change-Id: Icce1e31fe3ebcbf9e4897bbfa57b7f3d1fba67a3
1 parent 102bc41 commit 738774b

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

nova/tests/unit/virt/libvirt/test_guest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ def test_detach_device_with_retry_operation_failed(self, mock_detach):
327327
inc_sleep_time=.01, max_retry_count=3)
328328
# Some time later, we can do the wait/retry to ensure detach
329329
self.assertRaises(exception.DeviceNotFound, retry_detach)
330+
# Check that the save_and_reraise_exception context manager didn't log
331+
# a traceback when the libvirtError was caught and DeviceNotFound was
332+
# raised.
333+
self.assertNotIn('Original exception being dropped',
334+
self.stdlog.logger.output)
330335

331336
@mock.patch.object(libvirt_guest.Guest, "detach_device")
332337
def test_detach_device_with_retry_operation_internal(self, mock_detach):

nova/virt/libvirt/guest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def _try_detach_device(conf, persistent=False, live=False):
403403
device, persistent, live)
404404

405405
except libvirt.libvirtError as ex:
406-
with excutils.save_and_reraise_exception():
406+
with excutils.save_and_reraise_exception(reraise=False) as ctx:
407407
errcode = ex.get_error_code()
408408
if errcode in (libvirt.VIR_ERR_OPERATION_FAILED,
409409
libvirt.VIR_ERR_INTERNAL_ERROR):
@@ -420,6 +420,10 @@ def _try_detach_device(conf, persistent=False, live=False):
420420
# detach fails because the device is not found
421421
raise exception.DeviceNotFound(
422422
device=alternative_device_name)
423+
# Re-raise the original exception if we're not raising
424+
# DeviceNotFound instead. This will avoid logging of a
425+
# "Original exception being dropped" traceback.
426+
ctx.reraise = True
423427

424428
conf = get_device_conf_func(device)
425429
if conf is None:

0 commit comments

Comments
 (0)