TraceBack 无法连接到主机 api.telegram.org:443

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

我在 aiogram 框架上写了一个电报机器人。该机器人在 一个多星期 内运行良好,没有任何问题。客户开始抱怨机器人冻结。我下载了日志文件并看到了一堆不同的 TraceBacks。这种情况已经持续两天了。 3个回溯列表,例如:

第一

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]

第二

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

第三

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()

在我的电脑上一切正常。该机器人托管在 Google Cloud Platform (Compute engine) 上。我已经尝试重新启动机器人和主机 - 它没有帮助;今天又出现了这个问题。我也更改了机器人令牌,也没有用。为了完整起见,以下是版本列表:

$ 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)

问题是您主机上的时间与世界时间不同

我注意到Google云计算引擎没有任何时间同步服务。这就解释了为什么这个机器人在一段时间内运行良好,然后他开始捕捉回溯。