diff --git a/octavia/controller/healthmanager/health_manager.py b/octavia/controller/healthmanager/health_manager.py index f824596b15..764d1c60f5 100644 --- a/octavia/controller/healthmanager/health_manager.py +++ b/octavia/controller/healthmanager/health_manager.py @@ -85,6 +85,7 @@ class HealthManager(object): futs = [] while not self.dead.is_set(): amp_health = None + lock_session = None try: lock_session = db_api.get_session(autocommit=False) amp = None @@ -123,7 +124,8 @@ class HealthManager(object): time.sleep(CONF.health_manager.heartbeat_timeout) except Exception: with excutils.save_and_reraise_exception(): - lock_session.rollback() + if lock_session: + lock_session.rollback() if amp_health is None: break diff --git a/octavia/tests/unit/controller/healthmanager/test_health_manager.py b/octavia/tests/unit/controller/healthmanager/test_health_manager.py index 1670422614..53b691f4c8 100644 --- a/octavia/tests/unit/controller/healthmanager/test_health_manager.py +++ b/octavia/tests/unit/controller/healthmanager/test_health_manager.py @@ -117,3 +117,21 @@ class TestHealthManager(base.TestCase): session_mock.assert_called_once_with(autocommit=False) self.assertFalse(failover_mock.called) + + @mock.patch('octavia.controller.worker.v1.controller_worker.' + 'ControllerWorker.failover_amphora') + @mock.patch('octavia.db.repositories.AmphoraHealthRepository.' + 'get_stale_amphora', return_value=None) + @mock.patch('octavia.db.api.get_session') + def test_health_check_db_error(self, session_mock, get_stale_amp_mock, + failover_mock): + get_stale_amp_mock.return_value = None + + mock_session = mock.MagicMock() + session_mock.return_value = mock_session + session_mock.side_effect = TestException('DB Error') + exit_event = threading.Event() + hm = healthmanager.HealthManager(exit_event) + + self.assertRaises(TestException, hm.health_check) + self.assertEqual(0, mock_session.rollback.call_count) diff --git a/releasenotes/notes/Fix-HM-DB-Rollback-no-connection-2664c4f7823ecaec.yaml b/releasenotes/notes/Fix-HM-DB-Rollback-no-connection-2664c4f7823ecaec.yaml new file mode 100644 index 0000000000..1d15db31c0 --- /dev/null +++ b/releasenotes/notes/Fix-HM-DB-Rollback-no-connection-2664c4f7823ecaec.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Fixed an issue with the health manager reporting an UnboundLocalError if + it gets an exception attempting to get a database connection.