Unclosed aiohttp Sessions #4

Open
opened 2026-02-02 15:09:58 -05:00 by jared · 0 comments
Owner

async with aiohttp.ClientSession() as session: # Lines 131, 563
Issue: Creating new sessions for each request is inefficient. Should reuse a single session.

Fix:

class CustomBot(commands.Bot):
def init(self):
# ... existing code ...
self.http_session: aiohttp.ClientSession = None

async def setup_hook(self):
    self.http_session = aiohttp.ClientSession()
    await self.tree.sync()

async def close(self):
    if self.http_session:
        await self.http_session.close()
    await super().close()

Then use self.http_session everywhere instead of creating new ones

async with aiohttp.ClientSession() as session: # Lines 131, 563 Issue: Creating new sessions for each request is inefficient. Should reuse a single session. Fix: class CustomBot(commands.Bot): def __init__(self): # ... existing code ... self.http_session: aiohttp.ClientSession = None async def setup_hook(self): self.http_session = aiohttp.ClientSession() await self.tree.sync() async def close(self): if self.http_session: await self.http_session.close() await super().close() # Then use self.http_session everywhere instead of creating new ones
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: LotusGuild/discordBot#4