Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python / Discord: "Unclosed Connector connections"

The below script connects to discord, and then disconnects again. that works perfectly and prints out these two items:

**** Logged in!

**** End

But when the script ends, I get this additional warning:

Unclosed connector

connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x7f18f84e6080>, 3702249.404694462)]']

connector: <aiohttp.connector.TCPConnector object at 0x7f18f70af4c0>

I tried many ways but have not figured out how to et around this; any pointers are welcome. Of course I could simply ignore it, but I'd prefer to handle this "graciously".

Update: Sharing how I have approached it so far. The code I pasted is a minimum working example out of a larger code that allows reproducing the problem easily.

I did try:

  • to define a session (aiohttp.ClientSession()) and then awaiting to close it
  • leverage the @client.event "on_disconnect()" to close the session
  • use "bot.run" rather than "client.run"; and even put that into a async run
  • add a sleep delay to see whether then the connection would be closed before the script ends
  • figure it out by looking at the discord examples (link)
  • study the similar stackoverflow posts and see whether I could get them to work for my case, such as here, here and here

Thus any pointers that guide me on the right path are highly appreciated. If people still feel my question is "unprepared", appreciate a comment as to how I could make it better...

import discord
import asyncio

# Suppress login messages from discord
import logging
logging.getLogger("discord.client").setLevel(logging.WARNING)
logging.getLogger("discord.gateway").setLevel(logging.WARNING)

intents = discord.Intents.default()
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print('**** Logged in!')
    await client.close()

# Starts the script:
DISCORD_BOT_TOKEN = 'xxx'
if __name__ == '__main__':
    client.run(DISCORD_BOT_TOKEN)
    print('**** End')
like image 512
ManiMatter Avatar asked Dec 22 '25 02:12

ManiMatter


1 Answers

This no longer seems to be an issue.

Re-tested with the following and worked perfectly well:

  • OS: Ubuntu 24.04.2 LTS

  • Python Version: Python 3.12.3

  • Pip Packages:

    • discord 2.3.2

    • discord.py 2.5.2

    • aiohttp 3.11.18

    • asyncio 3.4.3

like image 111
ManiMatter Avatar answered Dec 24 '25 22:12

ManiMatter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!