Skip to content

[BUG] Exception while shutdown #3893

@et-ness

Description

@et-ness

Steps to Reproduce

  1. run an example
  2. stop with CTRL+C
  3. randomly the shutdown got an exception

Expected behaviour

Close the bot without exceptions

Actual behaviour

These exceptions is very similar to issue #1563 if I run the same code on python3.9 I got the exception described in #1563 while if I run on python3.11 sometime I got these exceptions:

Details
^CTraceback (most recent call last):
  File "/usr/lib/python3/dist-packages/anyio/streams/tls.py", line 130, in _call_sslobject_method
    result = func(*args)
             ^^^^^^^^^^^
  File "/usr/lib/python3.11/ssl.py", line 921, in read
    v = self._sslobj.read(len)
        ^^^^^^^^^^^^^^^^^^^^^^
ssl.SSLWantReadError: The operation did not complete (read) (_ssl.c:2546)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/httpcore/backends/asyncio.py", line 34, in read
    return await self._stream.receive(max_bytes=max_bytes)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/anyio/streams/tls.py", line 195, in receive
    data = await self._call_sslobject_method(self._ssl_object.read, max_bytes)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/anyio/streams/tls.py", line 137, in _call_sslobject_method
    data = await self.transport_stream.receive()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/anyio/_backends/_asyncio.py", line 1265, in receive
    await self._protocol.read_event.wait()
  File "/usr/lib/python3.11/asyncio/locks.py", line 213, in wait
    await fut
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/httpcore/_exceptions.py", line 10, in map_exceptions
    yield
  File "/usr/lib/python3/dist-packages/httpcore/backends/asyncio.py", line 32, in read
    with anyio.fail_after(timeout):
  File "/usr/lib/python3/dist-packages/anyio/_core/_tasks.py", line 118, in __exit__
    raise TimeoutError
TimeoutError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/httpx/_transports/default.py", line 60, in map_httpcore_exceptions
    yield
  File "/usr/lib/python3/dist-packages/httpx/_transports/default.py", line 353, in handle_async_request
    resp = await self._pool.handle_async_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/httpcore/_async/connection_pool.py", line 253, in handle_async_request
    raise exc
  File "/usr/lib/python3/dist-packages/httpcore/_async/connection_pool.py", line 237, in handle_async_request
    response = await connection.handle_async_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/httpcore/_async/connection.py", line 90, in handle_async_request
    return await self._connection.handle_async_request(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/httpcore/_async/http11.py", line 112, in handle_async_request
    raise exc
  File "/usr/lib/python3/dist-packages/httpcore/_async/http11.py", line 91, in handle_async_request
    ) = await self._receive_response_headers(**kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/httpcore/_async/http11.py", line 155, in _receive_response_headers
    event = await self._receive_event(timeout=timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/httpcore/_async/http11.py", line 191, in _receive_event
    data = await self._network_stream.read(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/httpcore/backends/asyncio.py", line 31, in read
    with map_exceptions(exc_map):
  File "/usr/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/usr/lib/python3/dist-packages/httpcore/_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc)
httpcore.ReadTimeout

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

Traceback (most recent call last):
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/request/_httpxrequest.py", line 219, in do_request
    res = await self._client.request(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/httpx/_client.py", line 1533, in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/httpx/_client.py", line 1620, in send
    response = await self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/httpx/_client.py", line 1648, in _send_handling_auth
    response = await self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/httpx/_client.py", line 1685, in _send_handling_redirects
    response = await self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/httpx/_client.py", line 1722, in _send_single_request
    response = await transport.handle_async_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/httpx/_transports/default.py", line 352, in handle_async_request
    with map_httpcore_exceptions():
  File "/usr/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/usr/lib/python3/dist-packages/httpx/_transports/default.py", line 77, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ReadTimeout

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

Traceback (most recent call last):
  File "/home/john/test/./bot.py", line 98, in <module>
    main()
  File "/home/john/test/./bot.py", line 95, in main
    app.run_polling()
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/ext/_application.py", line 765, in run_polling
    return self.__run(
           ^^^^^^^^^^^
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/ext/_application.py", line 963, in __run
    loop.run_until_complete(self.updater.stop())  # type: ignore[union-attr]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/ext/_updater.py", line 752, in stop
    await self._stop_polling()
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/ext/_updater.py", line 777, in _stop_polling
    await self.__polling_cleanup_cb()
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/ext/_updater.py", line 381, in _get_updates_cleanup
    await self.bot.get_updates(
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/ext/_extbot.py", line 553, in get_updates
    updates = await super().get_updates(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/_bot.py", line 394, in decorator
    result = await func(self, *args, **kwargs)  # skipcq: PYL-E1102
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/_bot.py", line 3542, in get_updates
    await self._post(
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/_bot.py", line 482, in _post
    return await self._do_post(
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/ext/_extbot.py", line 335, in _do_post
    return await super()._do_post(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/_bot.py", line 510, in _do_post
    return await request.post(
           ^^^^^^^^^^^^^^^^^^^
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/request/_baserequest.py", line 168, in post
    result = await self._request_wrapper(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/request/_baserequest.py", line 288, in _request_wrapper
    raise exc
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/request/_baserequest.py", line 278, in _request_wrapper
    code, payload = await self.do_request(
                    ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/request/_httpxrequest.py", line 236, in do_request
    raise TimedOut from err
telegram.error.TimedOut: Timed out
Exception ignored in: <coroutine object Application._update_fetcher at 0x7f43eb05c400>
Traceback (most recent call last):
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/ext/_application.py", line 1101, in _update_fetcher
  File "/usr/lib/python3.11/asyncio/queues.py", line 160, in get
  File "/usr/lib/python3.11/asyncio/base_events.py", line 761, in call_soon
  File "/usr/lib/python3.11/asyncio/base_events.py", line 519, in _check_closed
RuntimeError: Event loop is closed

I think depends when I start the shutdown. Something is wrong in shutdown routine.

Operating System

Linux

Version of Python, python-telegram-bot & dependencies

python-telegram-bot 20.5
Bot API 6.8
Python 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0]

Relevant log output

File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/request/_httpxrequest.py", line 236, in do_request
    raise TimedOut from err
telegram.error.TimedOut: Timed out
Exception ignored in: <coroutine object Application._update_fetcher at 0x7f43eb05c400>
Traceback (most recent call last):
  File "/home/john/test/.python3/local/lib/python3.11/dist-packages/telegram/ext/_application.py", line 1101, in _update_fetcher

Additional Context

Issue there is only on shutdown, is not a big problem, but is bad to see all those exceptions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions