diff --git a/patroni/dcs/consul.py b/patroni/dcs/consul.py index c22d0f8e..ed4e86ac 100644 --- a/patroni/dcs/consul.py +++ b/patroni/dcs/consul.py @@ -367,7 +367,11 @@ class Consul(AbstractDCS): def touch_member(self, data, permanent=False): cluster = self.cluster member = cluster and cluster.get_member(self._name, fallback_to_leader=False) - create_member = not permanent and self.refresh_session() + + try: + create_member = not permanent and self.refresh_session() + except DCSError: + return False if member and (create_member or member.session != self._session): self._client.kv.delete(self.member_path) diff --git a/patroni/ha.py b/patroni/ha.py index 953c9636..7c0490b4 100644 --- a/patroni/ha.py +++ b/patroni/ha.py @@ -1370,8 +1370,12 @@ class Ha(object): def run_cycle(self): with self._async_executor: - info = self._run_cycle() - return (self.is_paused() and 'PAUSE: ' or '') + info + try: + info = self._run_cycle() + return (self.is_paused() and 'PAUSE: ' or '') + info + except Exception: + logger.exception('Unexpected exception') + return 'Unexpected exception raised, please report it as a BUG' def shutdown(self): if self.is_paused(): diff --git a/tests/test_consul.py b/tests/test_consul.py index 785e80f3..6d0c4732 100644 --- a/tests/test_consul.py +++ b/tests/test_consul.py @@ -122,6 +122,8 @@ class TestConsul(unittest.TestCase): self.c.refresh_session = Mock(return_value=True) for _ in range(0, 4): self.c.touch_member({'balbla': 'blabla'}) + self.c.refresh_session = Mock(side_effect=ConsulError('foo')) + self.assertFalse(self.c.touch_member({'balbla': 'blabla'})) @patch.object(consul.Consul.KV, 'put', Mock(side_effect=InvalidSession)) def test_take_leader(self): diff --git a/tests/test_ha.py b/tests/test_ha.py index a35641f3..178ca1ef 100644 --- a/tests/test_ha.py +++ b/tests/test_ha.py @@ -1073,3 +1073,8 @@ class TestHa(PostgresInit): self.ha.cluster = get_cluster_initialized_without_leader(leader=True, cluster_config=config) self.ha.has_lock = true self.assertEqual(self.ha.run_cycle(), 'no action. i am the leader with the lock') + + @patch.object(Cluster, 'has_member', true) + def test_run_cycle(self): + self.ha.dcs.touch_member = Mock(side_effect=DCSError('foo')) + self.assertEqual(self.ha.run_cycle(), 'Unexpected exception raised, please report it as a BUG')