Skip to content
Advertisement

TraceBack Cannot connect to host api.telegram.org:443

I wrote a telegram bot on the aiogram framework. The bot worked fine for over a week, with no problems. The customer started complaining about the bot freezing. I downloaded the log file and saw a bunch of different TraceBacks. This situation has been happening for two days now. List of 3 TraceBacks, for example:

First

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/aiohttp/connector.py", line 986, in _wrap_create_connection
    return await self._loop.create_connection(*args, **kwargs)  # type: ignore[return-value]  # noqa
  File "/usr/lib/python3.7/asyncio/base_events.py", line 959, in create_connection
    raise exceptions[0]
  File "/usr/lib/python3.7/asyncio/base_events.py", line 946, in create_connection
    await self.sock_connect(sock, address)
  File "/usr/lib/python3.7/asyncio/selector_events.py", line 464, in sock_connect
    return await fut
  File "/usr/lib/python3.7/asyncio/selector_events.py", line 469, in _sock_connect
    sock.connect(address)
OSError: [Errno 101] Network is unreachable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/aiogram/bot/api.py", line 139, in make_request
    async with session.post(url, data=req, **kwargs) as response:
  File "/usr/local/lib/python3.7/dist-packages/aiohttp/client.py", line 1138, in __aenter__
    self._resp = await self._coro
  File "/usr/local/lib/python3.7/dist-packages/aiohttp/client.py", line 536, in _request
    req, traces=traces, timeout=real_timeout
  File "/usr/local/lib/python3.7/dist-packages/aiohttp/connector.py", line 542, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "/usr/local/lib/python3.7/dist-packages/aiohttp/connector.py", line 907, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
  File "/usr/local/lib/python3.7/dist-packages/aiohttp/connector.py", line 1206, in _create_direct_connection
    raise last_exc
  File "/usr/local/lib/python3.7/dist-packages/aiohttp/connector.py", line 1187, in _create_direct_connection
    client_error=client_error,
  File "/usr/local/lib/python3.7/dist-packages/aiohttp/connector.py", line 992, in _wrap_create_connection
    raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host api.telegram.org:443 ssl:default [Network is unreachable]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/aiogram/dispatcher/dispatcher.py", line 385, in start_polling
    allowed_updates=allowed_updates
  File "/usr/local/lib/python3.7/dist-packages/aiogram/bot/bot.py", line 110, in get_updates
    result = await self.request(api.Methods.GET_UPDATES, payload)
  File "/usr/local/lib/python3.7/dist-packages/aiogram/bot/base.py", line 232, in request
    proxy=self.proxy, proxy_auth=self.proxy_auth, timeout=self.timeout, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/aiogram/bot/api.py", line 142, in make_request
    raise exceptions.NetworkError(f"aiohttp client throws an error: {e.__class__.__name__}: {e}")
aiogram.utils.exceptions.NetworkError: Aiohttp client throws an error: ClientConnectorError: Cannot connect to host api.telegram.org:443 ssl:default [Network is unreachable]

Second

future: <Task finished coro=<Dispatcher._process_polling_updates() done, defined at /usr/local/lib/python3.7/dist-packages/aiogram/dispatcher/dispatcher.py:407> exception=TelegramAPIError('Gateway Timeout')>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/aiogram/dispatcher/dispatcher.py", line 415, in _process_polling_updates
    for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
  File "/usr/local/lib/python3.7/dist-packages/aiogram/dispatcher/dispatcher.py", line 235, in process_updates
    return await asyncio.gather(*tasks)
  File "/usr/local/lib/python3.7/dist-packages/aiogram/dispatcher/handler.py", line 116, in notify
    response = await handler_obj.handler(*args, **partial_data)
  File "/usr/local/lib/python3.7/dist-packages/aiogram/dispatcher/dispatcher.py", line 256, in process_update
    return await self.message_handlers.notify(update.message)
  File "/usr/local/lib/python3.7/dist-packages/aiogram/dispatcher/handler.py", line 116, in notify
    response = await handler_obj.handler(*args, **partial_data)
  File "/home/koval_pavlo03/TokenSaleBot/handlers/admin_commands.py", line 152, in do_adm
    await message.answer_document(open('logs.txt', 'rb'))
  File "/usr/local/lib/python3.7/dist-packages/aiogram/types/message.py", line 733, in answer_document
    reply_markup=reply_markup,
  File "/usr/local/lib/python3.7/dist-packages/aiogram/bot/bot.py", line 708, in send_document
    result = await self.request(api.Methods.SEND_DOCUMENT, payload, files)
  File "/usr/local/lib/python3.7/dist-packages/aiogram/bot/base.py", line 232, in request
    proxy=self.proxy, proxy_auth=self.proxy_auth, timeout=self.timeout, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/aiogram/bot/api.py", line 140, in make_request
    return check_result(method, response.content_type, response.status, await response.text())
  File "/usr/local/lib/python3.7/dist-packages/aiogram/bot/api.py", line 128, in check_result
    raise exceptions.TelegramAPIError(description)
aiogram.utils.exceptions.TelegramAPIError: Gateway Timeout

Third

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/aiogram/dispatcher/dispatcher.py", line 385, in start_polling
    allowed_updates=allowed_updates
  File "/usr/local/lib/python3.7/dist-packages/aiogram/bot/bot.py", line 110, in get_updates
    result = await self.request(api.Methods.GET_UPDATES, payload)
  File "/usr/local/lib/python3.7/dist-packages/aiogram/bot/base.py", line 232, in request
    proxy=self.proxy, proxy_auth=self.proxy_auth, timeout=self.timeout, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/aiogram/bot/api.py", line 140, in make_request
    return check_result(method, response.content_type, response.status, await response.text())
  File "/usr/local/lib/python3.7/dist-packages/aiogram/bot/api.py", line 119, in check_result
    exceptions.ConflictError.detect(description)
  File "/usr/local/lib/python3.7/dist-packages/aiogram/utils/exceptions.py", line 140, in detect
    raise err(cls.text or description)
aiogram.utils.exceptions.TerminatedByOtherGetUpdates: Terminated by other getupdates request; make sure that only one bot instance is running
01/21/2022 19:57:14 SSL error in data received
protocol: <asyncio.sslproto.SSLProtocol object at 0x7feef4858278>
transport: <_SelectorSocketTransport fd=9 read=polling write=<idle, bufsize=0>>
Traceback (most recent call last):
  File "/usr/lib/python3.7/asyncio/sslproto.py", line 526, in data_received
    ssldata, appdata = self._sslpipe.feed_ssldata(data)
  File "/usr/lib/python3.7/asyncio/sslproto.py", line 207, in feed_ssldata
    self._sslobj.unwrap()
  File "/usr/lib/python3.7/ssl.py", line 767, in unwrap
    return self._sslobj.shutdown()

On my computer everything works fine. The bot is hosted on Google Cloud Platform (Compute engine). I have already tried restarting the bot and the host machine – it did not help; the problem appeared again today. I also changed the bot token, also did not work. For the sake of completeness, below is a list of versions:

$ python3 --version
Python 3.7.3

$ python3 -m pip show aiogram
Name: aiogram
Version: 2.18
Summary: Is a pretty simple and fully asynchronous framework for Telegram Bot API
Home-page: https://github.com/aiogram/aiogram
Author: Alex Root Junior
Author-email: jroot.junior@gmail.com
License: MIT
Location: /usr/local/lib/python3.7/dist-packages
Requires: aiohttp, Babel, certifi
Required-by: 

$ python3 -m pip show aiohttp
Name: aiohttp
Version: 3.8.1
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: 
Author-email: 
License: Apache 2
Location: /usr/local/lib/python3.7/dist-packages
Requires: aiosignal, async-timeout, asynctest, attrs, charset-normalizer, frozenlist, multidict, typing-extensions, yarl
Required-by: aiogram

UPD

Here you can see log file (Google Drive)

Advertisement

Answer

The problem is that time on your host machine is different from world time.

I have noticed that Google Cloud compute engine doesn’t have any time synchronization services. That explains why the bot works well for some time, and then he starts catching tracebacks.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement