Pycord-Development / pycord

Pycord is a modern, easy to use, feature-rich, and async ready API wrapper for Discord written in Python
https://docs.pycord.dev
MIT License
2.7k stars 458 forks source link

Unclosed client session, Unclosed connector #2399

Open honzajavorek opened 5 months ago

honzajavorek commented 5 months ago

Summary

After upgrading to 2.5.0, my program emits unexpected errors to log output.

Reproduction Steps

The code below works correctly with 2.4.1 and all previous versions:

class Client(discord.Client):
    async def on_ready(self):
        await self.wait_until_ready()
        logger.debug("Discord connection ready")

        # Do stuff here
        ...

        logger.debug("Closing Discord client")
        await self.close()

    async def on_error(self, event, *args, **kwargs):
        logger.debug("Got an error, raising")
        raise

client = Client(loop=asyncio.new_event_loop())

def exc_handler(loop, context):
    nonlocal exc
    exc = context.get("exception")
    logger.debug(f"Recording exception: {exc}")
    loop.default_exception_handler(context)
    logger.debug("Stopping async execution")
    loop.stop()

client.loop.set_exception_handler(exc_handler)
logger.debug("Starting")
client.run(DISCORD_API_KEY)

After upgrading to 2.5.0, it produces the following logging at the end of the output:

...
[custom_logger] DEBUG: Recording exception: None
[asyncio] ERROR: Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x10d2e83d0>
[custom_logger] DEBUG: Stopping async execution
[custom_logger] DEBUG: Recording exception: None
[asyncio] ERROR: Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x10d03fc40>, 358615.555545375)]']
connector: <aiohttp.connector.TCPConnector object at 0x10d2e8510>
[custom_logger] DEBUG: Stopping async execution

Minimal Reproducible Code

No response

Expected Results

The code I have is designed to spin up a Discord client, do something over the API, and then close the client and end the script. It catches any exceptions and lets them to bubble up to crash the script in case of errors. It's supposed to work as a one-time job, not as a long-running bot. This code has worked correctly for several years now. It still does, but with 2.5.0 it started to produce errors which indicate something suddenly ain't right.

Actual Results

Getting errors which indicate something suddenly ain't right.

Intents

not relevant, but it's Intents(guilds=True, members=True, message_content=True)

System Information

Checklist

Additional Context

I wanted to figure out what commits in pycord caused the errors, but there's 262 new commits in the new version and that's just too many for me to go through 🤯 So just hoping here you might have a hunch what could be the issue.

llimonix commented 4 months ago

image my bot, which has been working for several months, has just appeared now

NeloBlivion commented 4 months ago

I'm fairly sure this is due to changes in aiohttp itself and not this library, as 2.5.0 didn't have any major underlying changes. Perhaps experiment with downgrading your aiohttp version (minimum 3.6.0). Coincidentally there's mention of an Unclosed client session warning fix in the 3.9.5 changelog; not sure if it's related, but maybe try that too.

honzajavorek commented 2 months ago

I see the same behavior with aiohttp pinned to either 3.6.0 or 3.9.5.

timreibe commented 1 month ago

+1 same issue here on version 2.5.0, our discord bot was working seamlessly until upgrading

timreibe commented 1 month ago

hey @honzajavorek, could you solve this issue somehow?

honzajavorek commented 1 month ago

Not yet. I plan to clone pycord and bisect commits to see when exactly this has started, but I haven't had time to do it. I see the warnings every day in my terminal, so I'm motivated to nail down this annoyance, but still it's low priority as it has no actual impact on functionality (at least I hope! 😀)