Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Possibility of Event loop is closed warning after Bot shutdown #2997

Closed
harshil21 opened this issue May 5, 2022 · 7 comments
Closed

Comments

@harshil21
Copy link
Member

harshil21 commented May 5, 2022

While testing our code, we became aware that there could be a chance that we get an Event loop is closed error after shutting down the bot. This seems to only affect Windows and python versions 3.8+ and is related to ProactorEventLoop event loop policy on Windows.

MRE:

from telegram import Bot
import asyncio

async def bot_initialize():
    bot = Bot("TOKEN")
    await bot.initialize()

asyncio.run(bot_initialize())

The problem is most likely not with our library but with CPython itself, see python/cpython#83413 and encode/httpx#914.

A workaround would be to switch our default event loop policy to WindowsSelectorEventLoopPolicy, which is now what we do in tests. If multiple users are facing this problem frequently, we could consider switching.

If anybody is willing to dig into this further and figure out when/how/why exactly this error occurs, that would be great.

Python versions affected:

3.8, 3.9

@harshil21
Copy link
Member Author

harshil21 commented Jun 30, 2022

Closing since this is fixed upstream: python/cpython#92841 (comment)
on python 3.10.6 and 3.11.

Copy link
Member

@harshil21 But we support versions lower then 10, and they can see that error. I would either set the proper event loop or throw a warning if it isn't set on the non fixed versions

Copy link
Member Author

@Poolitzer chances that someone bumps into this are rare, so far I've seen this happen due to another (unrelated) error happening. So I don't think we need to take any action just yet.

Copy link
Member

@harshil21 I tend to agree with harshil here, though I think keeping the issue open doesn't hurt. Also gives users a chance to see that we're aware of it give input about this situation

@harshil21 harshil21 reopened this Jun 30, 2022
Copy link
Member

tsnoam commented Jul 2, 2022

@Bibo-Joshi Is there a real problem here? Data loss? Or is it just an annoying log at shutdown time?

If the latter and the problem is with python I think that closing as wontfix is appropriate.

Copy link
Member

@tsnoam I'm not entirely sure tbh. would have to double check …

@Bibo-Joshi
Copy link
Member

I can't find any indication that the unexpected shutdown happens before any shutdown logic. In fact, while I can reproduce the issue with the example from above (where Bot.shutdown is not called!), running

from telegram import Bot
import asyncio


async def bot_initialize():
    async with Bot("TOKEN"):
        pass


asyncio.run(bot_initialize())

where we properly shut down the bot, the error does not appear. Hence, it looks like the problem only happens if the user doesn't properly free resources in the first place. Hence, I'll close as wontfix.

Still, if someone comes across the issue in a more sane setting, we should definitely reopen.

@Bibo-Joshi Bibo-Joshi closed this as not planned Won't fix, can't repro, duplicate, stale Jul 7, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jul 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants