Telegram bot loses connection

I deployed a Telegram bot on Koyeb. Since it doesn’t listen to any port, regular health-checks fail, resulting in stopping my application. As adviced here, I modified code so that simple web-server runs when the application starts working.

The problem is that every 40-240 minutes an exeption emerges:

2022-12-13 00:17:54,367 (__init__.py:960 MainThread) ERROR - TeleBot: "Infinity polling exception: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))"
2022-12-13 00:17:54,368 (__init__.py:962 MainThread) ERROR - TeleBot: "Exception traceback:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.7/site-packages/urllib3/connectionpool.py", line 710, in urlopen
chunked=chunked,
File "/app/.heroku/python/lib/python3.7/site-packages/urllib3/connectionpool.py", line 386, in _make_request
self._validate_conn(conn)
File "/app/.heroku/python/lib/python3.7/site-packages/urllib3/connectionpool.py", line 1042, in _validate_conn
conn.connect()
File "/app/.heroku/python/lib/python3.7/site-packages/urllib3/connection.py", line 424, in connect
tls_in_tls=tls_in_tls,
File "/app/.heroku/python/lib/python3.7/site-packages/urllib3/util/ssl_.py", line 450, in ssl_wrap_socket
sock, context, tls_in_tls, server_hostname=server_hostname
File "/app/.heroku/python/lib/python3.7/site-packages/urllib3/util/ssl_.py", line 493, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
File "/app/.heroku/python/lib/python3.7/ssl.py", line 423, in wrap_socket
session=session
File "/app/.heroku/python/lib/python3.7/ssl.py", line 870, in _create
self.do_handshake()
File "/app/.heroku/python/lib/python3.7/ssl.py", line 1139, in do_handshake
self._sslobj.do_handshake()
ConnectionResetError: [Errno 104] Connection reset by peer

When I run this app on my local computer, the problem does not arise. (Python 3.7.9 is used on both local computer and in my application on Koyeb). On the other serverless platform for deployment I also didn’t have this problem. That platform doesn’t perform health-checks, hence I didn’t have to run a dummy server within my application. So may be my problem is somehow related to running a server? I can’t check this assumption, since if I run my app without running a server, it won’t work longer than 15 minutes due to health-check fails.

This is the code I added to run a server (my app is in Python):

# web_server.py
from os import environ

from http.server import BaseHTTPRequestHandler, HTTPServer

APP_HOST = ''
APP_PORT = int(environ.get('PORT'))


class GetHandler(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(204)
        self.end_headers()

    def do_GET(self):
        self._set_headers()


def run_server(handler_class=GetHandler):
    server_address = (APP_HOST, APP_PORT)
    httpd = HTTPServer(server_address, handler_class)
    httpd.serve_forever()

I run my bot and server together like that:

# main.py
if __name__ == '__main__':
    threading.Thread(target=run_server).start()
    bot.infinity_polling()

Do you have any ideas on what’s the reason of loss of connection and how can I avoid it? Thank you in advance!