Commit e0b638b5 authored by Julien Muchembled's avatar Julien Muchembled

client: close failing connection explicitly in case of unhandled exception

The changes consist in:
- indenting many lines without exceeding 80 chars
- setting conn = None ASAP on ConnectionClosed
- adding a try..except clause

Starting from next commit, the exception can be POSException.ReadOnlyError.
parent eb3a3937
...@@ -200,6 +200,8 @@ class Application(ThreadedApplication): ...@@ -200,6 +200,8 @@ class Application(ThreadedApplication):
fail_count = 0 fail_count = 0
ask = self._ask ask = self._ask
handler = self.primary_bootstrap_handler handler = self.primary_bootstrap_handler
conn = None
try:
while 1: while 1:
self.ignore_invalidations = True self.ignore_invalidations = True
# Get network connection to primary master # Get network connection to primary master
...@@ -213,10 +215,10 @@ class Application(ThreadedApplication): ...@@ -213,10 +215,10 @@ class Application(ThreadedApplication):
# Otherwise, check one by one. # Otherwise, check one by one.
master_list = self.nm.getMasterList() master_list = self.nm.getMasterList()
if not master_list: if not master_list:
# XXX: On shutdown, it already happened that this list # XXX: On shutdown, it already happened that this
# is empty, leading to ZeroDivisionError. This # list is empty, leading to ZeroDivisionError.
# looks a minor issue so let's wait to have more # This looks a minor issue so let's wait to
# information. # have more information.
logging.error('%r', self.__dict__) logging.error('%r', self.__dict__)
index = (index + 1) % len(master_list) index = (index + 1) % len(master_list)
node = master_list[index] node = master_list[index]
...@@ -230,6 +232,7 @@ class Application(ThreadedApplication): ...@@ -230,6 +232,7 @@ class Application(ThreadedApplication):
try: try:
ask(conn, p, handler=handler) ask(conn, p, handler=handler)
except ConnectionClosed: except ConnectionClosed:
conn = None
fail_count += 1 fail_count += 1
else: else:
self.primary_master_node = node self.primary_master_node = node
...@@ -247,9 +250,14 @@ class Application(ThreadedApplication): ...@@ -247,9 +250,14 @@ class Application(ThreadedApplication):
if self.pt.operational(): if self.pt.operational():
break break
except ConnectionClosed: except ConnectionClosed:
logging.error('Connection to %s lost', self.trying_master_node) conn = self.primary_master_node = None
self.primary_master_node = None logging.error('Connection to %s lost',
self.trying_master_node)
fail_count += 1 fail_count += 1
except:
if conn is not None:
conn.close()
raise
logging.info("Connected and ready") logging.info("Connected and ready")
return conn return conn
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment