3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 01:09:22 +01:00

IRCNetwork: fix autoconnect not applying when socket.connect() fails

This commit is contained in:
James Lu 2018-03-23 20:21:49 -07:00
parent 814dd9a3c5
commit 815535d76b

View File

@ -1387,7 +1387,13 @@ class IRCNetwork(PyLinkNetworkCoreWithUtils):
self._socket = context.wrap_socket(self._socket)
log.info("Connecting to network %r on %s:%s", self.name, ip, port)
try:
self._socket.connect((ip, port))
except (ssl.SSLError, OSError):
log.exception('Unable to connect to network %r', self.name)
self._start_reconnect()
return
self._socket.settimeout(self.pingtimeout)
self._selector_key = selectdriver.register(self)
@ -1498,17 +1504,23 @@ class IRCNetwork(PyLinkNetworkCoreWithUtils):
self._ping_timer.cancel()
self._buffer = b''
self._post_disconnect()
self._start_reconnect()
def _start_reconnect(self):
"""Schedules a reconnection to the network."""
def _reconnect():
# _run_autoconnect() will block and return True after the autoconnect
# delay has passed, if autoconnect is disabled. (We do not want it to
# block whatever is calling disconnect() though.)
# delay has passed, if autoconnect is disabled. We do not want it to
# block whatever is calling disconnect() though, so we run it in a new
# thread.
if self._run_autoconnect():
self.connect()
if self._reconnect_thread is None or not self._reconnect_thread.is_alive():
self._reconnect_thread = threading.Thread(target=_reconnect, name="Reconnecting network %s" % self.name)
self._reconnect_thread.start()
else:
log.debug('(%s) Ignoring attempt to reschedule reconnect as one is in progress.', self.name)
def handle_events(self, line):
raise NotImplementedError