Skip to content

Unclosed client session, Unclosed connector #2399

Description

@honzajavorek

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

  • Python v3.11.8-final
  • py-cord v2.5.None-final
  • aiohttp v3.9.3
  • system info: Darwin 23.3.0 Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:27 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T8103

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    unconfirmed bugA bug report that needs triaging

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions