# Buggy code
def touch_member(self, data, ttl=None, 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()

    if member and (create_member or member.session != self._session):
        try:
            self._client.kv.delete(self.member_path)
            create_member = True
        except Exception:
            return False

    if not create_member and member and deep_compare(data, member.data):
        return True

    try:
        args = {} if permanent else {"acquire": self._session}
        self._client.kv.put(
            self.member_path, json.dumps(data, separators=(",", ":")), **args
        )
        if self._register_service:
            self.update_service(
                not create_member and member and member.data or {}, data
            )
        return True
    except Exception:
        logger.exception("touch_member")
    return False


# Traceback (urllib3.exceptions.ReadTimeoutError)
2018-11-06 23:06:17,073 INFO: Lock owner: db1; I am db1reserve
2018-11-06 23:06:17,074 INFO: does not have lock
2018-11-06 23:06:17,095 INFO: no action.  i am a secondary and i am following a leader
2018-11-06 23:06:27,497 ERROR: watch
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/urllib3/connectionpool.py", line 384, in _make_request
six.raise_from(e, None)
File "<string>", line 2, in raise_from
File "/usr/lib/python3.4/site-packages/urllib3/connectionpool.py", line 380, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib64/python3.4/http/client.py", line 1227, in getresponse
response.begin()
File "/usr/lib64/python3.4/http/client.py", line 386, in begin
version, status, reason = self._read_status()
File "/usr/lib64/python3.4/http/client.py", line 348, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/lib64/python3.4/socket.py", line 378, in readinto
return self._sock.recv_into(b)
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/usr/lib/python3.4/site-packages/urllib3/connectionpool.py", line 386, in _make_request
self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
File "/usr/lib/python3.4/site-packages/urllib3/connectionpool.py", line 306, in _raise_timeout
raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='localhost', port=8500): Read timed out. (read timeout=10.388829231262207)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 494, in watch
idx, _ = self._client.kv.get(self.leader_path, index=leader_index, wait=str(timeout) + 's')
File "/usr/lib/python3.4/site-packages/consul/base.py", line 554, in get
params=params)
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 105, in wrapper
return callback(self.response(self.http.request(method.upper(), self.uri(path, params), **kwargs)))
File "/usr/lib/python3.4/site-packages/urllib3/request.py", line 68, in request
**urlopen_kw)
File "/usr/lib/python3.4/site-packages/urllib3/request.py", line 89, in request_encode_url
return self.urlopen(method, url, **extra_kw)
File "/usr/lib/python3.4/site-packages/urllib3/poolmanager.py", line 322, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/usr/lib/python3.4/site-packages/urllib3/connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/lib/python3.4/site-packages/urllib3/util/retry.py", line 398, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=8500): Max retries exceeded with url: /v1/kv/patroni/dbcluster/leader?wait=9.388829231262207s&amp;index=22078692 (Caused by ReadTimeoutError("HTTPConnectionPool(host='localhost', port=8500): Read timed out. (read timeout=10.388829231262207)",))
2018-11-06 23:06:39,312 INFO: Lock owner: db1; I am db1reserve
2018-11-06 23:06:39,315 INFO: does not have lock
2018-11-06 23:07:46,569 ERROR: refresh_session
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/patroni/utils.py", line 254, in __call__
return func(*args, **kwargs)
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 249, in _do_refresh_session
self._client.session.renew(self._session)
File "/usr/lib/python3.4/site-packages/consul/base.py", line 1913, in renew
params=params)
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 105, in wrapper
return callback(self.response(self.http.request(method.upper(), self.uri(path, params), **kwargs)))
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 76, in response
raise ConsulInternalError(msg)
patroni.dcs.consul.ConsulInternalError: 500 rpc error getting client: failed to get conn: dial tcp <nil>->192.168.241.12:8300: i/o timeout
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 268, in refresh_session
return self.retry(self._do_refresh_session)
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 214, in retry
return self._retry.copy()(*args, **kwargs)
File "/usr/lib/python3.4/site-packages/patroni/utils.py", line 263, in __call__
raise RetryFailedError("Exceeded retry deadline")
patroni.utils.RetryFailedError: 'Exceeded retry deadline'
2018-11-06 23:07:49,542 INFO: Lock owner: db1; I am db1reserve
2018-11-06 23:08:56,082 ERROR: refresh_session
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/patroni/__init__.py", line 149, in patroni_main
patroni.run()
File "/usr/lib/python3.4/site-packages/patroni/__init__.py", line 114, in run
logger.info(self.ha.run_cycle())
File "/usr/lib/python3.4/site-packages/patroni/ha.py", line 1236, in run_cycle
info = self._run_cycle()
File "/usr/lib/python3.4/site-packages/patroni/ha.py", line 1232, in _run_cycle
self.touch_member()
File "/usr/lib/python3.4/site-packages/patroni/ha.py", line 178, in touch_member
return self.dcs.touch_member(data)
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 344, in touch_member
create_member = not permanent and self.refresh_session()
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 271, in refresh_session
raise ConsulError('Failed to renew/create session')
patroni.dcs.consul.ConsulError: 'Failed to renew/create session'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/patroni/utils.py", line 254, in __call__
return func(*args, **kwargs)
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 249, in _do_refresh_session
self._client.session.renew(self._session)
File "/usr/lib/python3.4/site-packages/consul/base.py", line 1913, in renew
params=params)
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 105, in wrapper
return callback(self.response(self.http.request(method.upper(), self.uri(path, params), **kwargs)))
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 76, in response
raise ConsulInternalError(msg)
patroni.dcs.consul.ConsulInternalError: 500 rpc error getting client: failed to get conn: dial tcp <nil>->192.168.241.12:8300: i/o timeout
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 268, in refresh_session
return self.retry(self._do_refresh_session)
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 214, in retry
return self._retry.copy()(*args, **kwargs)
File "/usr/lib/python3.4/site-packages/patroni/utils.py", line 263, in __call__
raise RetryFailedError("Exceeded retry deadline")
patroni.utils.RetryFailedError: 'Exceeded retry deadline'
----------------------------------------
Exception happened during processing of request from ('192.168.242.11', 53742)
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('192.168.242.11', 51610)
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('192.168.242.11', 51612)
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('192.168.242.11', 39238)
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('192.168.242.11', 39242)
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('192.168.242.11', 39240)
----------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/patroni/__init__.py", line 149, in patroni_main
patroni.run()
File "/usr/lib/python3.4/site-packages/patroni/__init__.py", line 114, in run
logger.info(self.ha.run_cycle())
File "/usr/lib/python3.4/site-packages/patroni/ha.py", line 1236, in run_cycle
info = self._run_cycle()
File "/usr/lib/python3.4/site-packages/patroni/ha.py", line 1232, in _run_cycle
self.touch_member()
File "/usr/lib/python3.4/site-packages/patroni/ha.py", line 178, in touch_member
return self.dcs.touch_member(data)
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 344, in touch_member
create_member = not permanent and self.refresh_session()
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 271, in refresh_session
raise ConsulError('Failed to renew/create session')
patroni.dcs.consul.ConsulError: 'Failed to renew/create session'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/bin/patroni", line 11, in <module>
sys.exit(main())
File "/usr/lib/python3.4/site-packages/patroni/__init__.py", line 182, in main
return patroni_main()
File "/usr/lib/python3.4/site-packages/patroni/__init__.py", line 153, in patroni_main
patroni.shutdown()
File "/usr/lib/python3.4/site-packages/patroni/__init__.py", line 137, in shutdown
self.ha.shutdown()
File "/usr/lib/python3.4/site-packages/patroni/ha.py", line 1253, in shutdown
self.touch_member()
File "/usr/lib/python3.4/site-packages/patroni/ha.py", line 178, in touch_member
return self.dcs.touch_member(data)
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 344, in touch_member
create_member = not permanent and self.refresh_session()
File "/usr/lib/python3.4/site-packages/patroni/dcs/consul.py", line 271, in refresh_session
raise ConsulError('Failed to renew/create session')
patroni.dcs.consul.ConsulError: 'Failed to renew/create session'

# Fixed code
def touch_member(self, data, ttl=None, 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()

    if member and (create_member or member.session != self._session):
        self._client.kv.delete(self.member_path)
        create_member = True

    if not create_member and member and deep_compare(data, member.data):
        return True

    try:
        args = {} if permanent else {"acquire": self._session}
        self._client.kv.put(
            self.member_path, json.dumps(data, separators=(",", ":")), **args
        )
        if self._register_service:
            self.update_service(
                not create_member and member and member.data or {}, data
            )
        return True
    except Exception:
        logger.exception("touch_member")
    return False