From 0731a1d6983e663f8cb5cf282641c702027ae21c Mon Sep 17 00:00:00 2001 From: Dmitry Bukhta Date: Tue, 20 Feb 2018 17:58:51 +0300 Subject: [PATCH] Raise ProxyConnectionError instead looping forever (#621) We shouldn't try reconnecting when using a proxy if what's unavailable is the proxy server (and not Telegram servers). --- telethon/extensions/tcp_client.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/telethon/extensions/tcp_client.py b/telethon/extensions/tcp_client.py index a306302a..dd177aa2 100644 --- a/telethon/extensions/tcp_client.py +++ b/telethon/extensions/tcp_client.py @@ -8,6 +8,11 @@ from datetime import timedelta from io import BytesIO, BufferedWriter from threading import Lock +try: + import socks +except ImportError: + socks = None + MAX_TIMEOUT = 15 # in seconds CONN_RESET_ERRNOS = { errno.EBADF, errno.ENOTSOCK, errno.ENETUNREACH, @@ -70,6 +75,9 @@ class TcpClient: self._socket.connect(address) break # Successful connection, stop retrying to connect except OSError as e: + # Stop retrying to connect if proxy connection error occurred + if socks and isinstance(e, socks.ProxyConnectionError): + raise # There are some errors that we know how to handle, and # the loop will allow us to retry if e.errno in (errno.EBADF, errno.ENOTSOCK, errno.EINVAL,