Skip to content

RuntimeError: Event loop is closed #5021

@javad94

Description

@javad94

Steps to Reproduce

import logging

from telegram.ext import (
    Application,
)

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)

logger = logging.getLogger(__file__)
logger.setLevel(logging.INFO)
logging.getLogger("httpx").setLevel(logging.WARNING)


def error_handler(update, context) -> None:
    logger.exception(
        msg="Unhanlded Exception while handling an update:", exc_info=context.error
    )


async def post_init(application: Application) -> None:
    await application.bot.send_message(OWNER_USER_ID, "deployed")


def main():
    application = (
        Application.builder()
        .token(TOKEN)
        .post_init(post_init)
        .read_timeout(20)
        .write_timeout(20)
        .connect_timeout(20)
        .pool_timeout(10)
        .get_updates_read_timeout(20)
        .build()
    )

    application.run_polling(
        allowed_updates=["message", "callback_query"], bootstrap_retries=10
    )


if __name__ == "__main__":
    main()

Expected behaviour

The code should run normally.

Actual behaviour

sometimes in unstable networks, i got RuntimeError: Event loop is closed error when running the code.

Operating System

windows 11 24h2 with the code running in docker with python:3.13-slim image

Version of Python, python-telegram-bot & dependencies

python-telegram-bot 22.5
Bot API 9.2
Python 3.13.9 (main, Oct 21 2025, 02:09:29) [GCC 14.2.0]

Relevant log output

/usr/local/lib/python3.13/site-packages/telegram/ext/_application.py:631: RuntimeWarning: coroutine 'Application._update_fetcher' was never awaited
  self._update_fetcher(), name=f"Application:{self.bot.id}:update_fetcher"
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/app/project/__main__.py", line 4, in <module>
    main()
    ~~~~^^
  File "/app/project/bot/bot_getupdates.py", line 79, in main
    application.run_polling(allowed_updates=["message", "callback_query"], bootstrap_retries=10)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/telegram/ext/_application.py", line 839, in run_polling
    return self.__run(
           ~~~~~~~~~~^
        updater_coroutine=self.updater.start_polling(
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<9 lines>...
        close_loop=close_loop,
        ^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/local/lib/python3.13/site-packages/telegram/ext/_application.py", line 1061, in __run
    loop.run_until_complete(self.start())
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/asyncio/base_events.py", line 725, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.13/site-packages/telegram/ext/_application.py", line 631, in start
    self._update_fetcher(), name=f"Application:{self.bot.id}:update_fetcher"
                                                ^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/telegram/_bot.py", line 553, in id
    return self.bot.id
           ^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/telegram/_bot.py", line 542, in bot
    raise RuntimeError(
    ...<2 lines>...
    )
RuntimeError: ExtBot is not properly initialized. Call `ExtBot.initialize` before accessing this property.
Exception ignored in: <function Connection.__del__ at 0x7f954e1c2fc0>
Traceback (most recent call last):
  File "/usr/local/lib/python3.13/site-packages/aiomysql/connection.py", line 1131, in __del__
  File "/usr/local/lib/python3.13/site-packages/aiomysql/connection.py", line 339, in close
  File "/usr/local/lib/python3.13/asyncio/selector_events.py", line 1205, in close
  File "/usr/local/lib/python3.13/asyncio/selector_events.py", line 865, in close
  File "/usr/local/lib/python3.13/asyncio/base_events.py", line 833, in call_soon
  File "/usr/local/lib/python3.13/asyncio/base_events.py", line 556, in _check_closed
RuntimeError: Event loop is close

Additional Context

No response

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