aiogram / aiogram

aiogram is a modern and fully asynchronous framework for Telegram Bot API written in Python using asyncio
https://aiogram.dev
MIT License
4.75k stars 835 forks source link

Make errors more clear. Mention that you need start_polling. #314

Closed inyutin closed 4 years ago

inyutin commented 4 years ago

First of all, I want to thank you for this project. It's awesome!

Is your feature request related to a problem? Please describe. I was playing with echo-bot example from documentation. I made code that looks like this:

import json
import logging

from aiogram import Bot, Dispatcher, executor, types

log = logging.getLogger(__name__)

async def hello(message: types.Message):
    await message.reply("Hi", parse_mode="Markdown")

if __name__ == "__main__":
    bot = Bot(token=TOKEN)
    dp = Dispatcher(bot)

    dp.register_message_handler(hello)

However, it doesn't work. So, I get an error:

Exception ignored in: <function BaseBot.__del__ at 0x7f933c691560>
Traceback (most recent call last):
  File "/home/inyutin/Code/fancy_weather/env/lib/python3.7/site-packages/aiogram/bot/base.py", line 109, in __del__
  File "/usr/local/lib/python3.7/asyncio/events.py", line 762, in new_event_loop
  File "/usr/local/lib/python3.7/asyncio/events.py", line 660, in new_event_loop
  File "/usr/local/lib/python3.7/asyncio/unix_events.py", line 51, in __init__
  File "/usr/local/lib/python3.7/asyncio/selector_events.py", line 49, in __init__
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 368, in __init__
  File "/usr/local/lib/python3.7/asyncio/coroutines.py", line 28, in _is_debug_mode
AttributeError: 'NoneType' object has no attribute 'dev_mode'
Exception ignored in: <function BaseEventLoop.__del__ at 0x7f93405be290>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 617, in __del__
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 385, in __repr__
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 1790, in get_debug
AttributeError: '_UnixSelectorEventLoop' object has no attribute '_debug'
2020-05-01 20:45:47,491 asyncio E - Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f933c4b65d0>

Process finished with exit code 0

By this error I dont understand what's going on. After 15-20 minutes I found out that forgot about executor.start_polling(dp, skip_updates=True). I found missing line only by looking at the difference between my code and echo example code.

Describe the solution you'd like I suggest more explicit error at least.

Describe alternatives you've considered However I don't understand why start_polling line is mandatory. Why can't I just create bot, but don't fetch updates?

JrooTJunior commented 4 years ago

This problem is known.

In normal case you must see warning from aiohttp about unclosed client session on application finalization but in some cases python can finalize libraries before Bot.__del__ is done and the you can get unexpected behavior like this. Current finalization solution in framework works perfectly in previous python minor versions but in the last versions is not works.

Now i don't see optimal solutions without breaking backward compatibility in 2.x branch but i already done this in the next major version (3.0a) and it will be released soon (i think in few months).

Also i think i can make throwing explicit warning in bot destruction method. I'll try it.

JrooTJunior commented 4 years ago

And in Python 3.8.2 i don't see this exception. That is magic

image

inyutin commented 4 years ago

To be accurate, error on Python3.7.4, aiohttp3.6.2, aiogram2.8

Thank you for fast response. Its all clear now. Aiolibs is such a destruction magic sometimes. Probably you can just mention this problem in QuickStart guide. Maybe as a comment on echobot