Skip to content

Commit 250c590

Browse files
authored
Merge pull request CloudBotIRC#172 from linuxdaemon/gonzobot+fix-reconnect
Fix handling of connection errors
2 parents 9a82584 + 5dd454d commit 250c590

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

cloudbot/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def try_connect(self):
7979
try:
8080
yield from self.connect(timeout)
8181
except Exception:
82-
logger.exception("[%s] Error occurred while connecting.")
82+
logger.exception("[%s] Error occurred while connecting.", self.name)
8383
else:
8484
break
8585

cloudbot/clients/irc.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,13 @@ def connect(self, timeout=None):
126126
return
127127

128128
if self._connected:
129+
self._connected = False
129130
logger.info("[{}] Reconnecting".format(self.name))
130-
self._transport.close()
131+
if self._transport:
132+
self._transport.close()
131133
else:
132-
self._connected = True
133134
logger.info("[{}] Connecting".format(self.name))
135+
134136
optional_params = {}
135137
if self.local_bind:
136138
optional_params["local_addr"] = self.local_bind
@@ -144,6 +146,8 @@ def connect(self, timeout=None):
144146

145147
self._transport, self._protocol = yield from coro
146148

149+
self._connected = True
150+
147151
tasks = [
148152
self.bot.plugin_manager.launch(hook, Event(bot=self.bot, conn=self, hook=hook))
149153
for hook in self.bot.plugin_manager.connect_hooks
@@ -303,14 +307,14 @@ def connection_lost(self, exc):
303307
# we've been closed intentionally, so don't reconnect
304308
return
305309
logger.error("[{}] Connection lost: {}".format(self.conn.name, exc))
306-
async_util.wrap_future(self.conn.connect(), loop=self.loop)
310+
async_util.wrap_future(self.conn.try_connect(), loop=self.loop)
307311

308312
def eof_received(self):
309313
self._connected = False
310314
# create a new connected_future for when we are connected.
311315
self._connected_future = async_util.create_future(self.loop)
312316
logger.info("[{}] EOF received.".format(self.conn.name))
313-
async_util.wrap_future(self.conn.connect(), loop=self.loop)
317+
async_util.wrap_future(self.conn.try_connect(), loop=self.loop)
314318
return True
315319

316320
@asyncio.coroutine

plugins/core/check_conn.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def conncheck(nick, bot, notice):
1515
notice("{}, {}".format(conn, bot.connections[conn].connected))
1616
# if the value is in fact false try to connect
1717
if not bot.connections[conn].connected:
18-
bot.connections[conn].connect()
18+
bot.connections[conn].try_connect()
1919
# Send a message from each irc network connection to the nick that issued the command
2020
bot.connections[conn].message(nick, "just letting you know I am here. {}".format(conn))
2121

@@ -27,7 +27,7 @@ def do_reconnect(conn):
2727
yield from asyncio.sleep(2)
2828
conn._quit = False
2929

30-
coro = conn.connect()
30+
coro = conn.try_connect()
3131
try:
3232
yield from asyncio.wait_for(coro, 30)
3333
except asyncio.TimeoutError:

0 commit comments

Comments
 (0)