perf: reuse aiohttp.ClientSession in rubylogging (new session per deleted-message event) #7

Open
opened 2026-06-01 04:04:46 -04:00 by everestmcarthur · 0 comments
everestmcarthur commented 2026-06-01 04:04:46 -04:00 (Migrated from github.com)

rubylogging: New aiohttp.ClientSession created on every deleted-message event

In rubylogging/rubylogging.py (around line 460), a new aiohttp.ClientSession is instantiated for every on_message_delete event that has attachments with re-upload enabled. This is a hot path on active servers.

Problem

async with aiohttp.ClientSession() as session:
    for i, url in enumerate(images_for_preview[:max_previews]):
        async with session.get(url) as resp:
            ...
  • Creates a new TCP connection pool on every call
  • Generates ResourceWarning: Unclosed connector in logs when session exits quickly
  • aiohttp's own docs recommend reusing sessions across requests

Fix

Store a session at cog init and reuse it:

# In RubyLogging.__init__:
self._session: Optional[aiohttp.ClientSession] = None

# In cog_load / _init:
self._session = aiohttp.ClientSession()

# In cog_unload:
if self._session:
    await self._session.close()

# Replace per-event creation with:
async with self._session.get(url) as resp:
    ...

Same pattern in other cogs (lower severity — not hot paths)

  • nexuscore/applications.py:345 — per application submission
  • nexuscore/moderation.py:312 — per cross-server ban
  • wormhole/services/emoji.py:47 — per emoji fetch

The nexuscore/wormhole ones are low-frequency commands so lower priority, but worth fixing for consistency.

## `rubylogging`: New `aiohttp.ClientSession` created on every deleted-message event In `rubylogging/rubylogging.py` (around line 460), a new `aiohttp.ClientSession` is instantiated for every `on_message_delete` event that has attachments with re-upload enabled. This is a hot path on active servers. ### Problem ```python async with aiohttp.ClientSession() as session: for i, url in enumerate(images_for_preview[:max_previews]): async with session.get(url) as resp: ... ``` - Creates a new TCP connection pool on every call - Generates `ResourceWarning: Unclosed connector` in logs when session exits quickly - aiohttp's own docs recommend reusing sessions across requests ### Fix Store a session at cog init and reuse it: ```python # In RubyLogging.__init__: self._session: Optional[aiohttp.ClientSession] = None # In cog_load / _init: self._session = aiohttp.ClientSession() # In cog_unload: if self._session: await self._session.close() # Replace per-event creation with: async with self._session.get(url) as resp: ... ``` ### Same pattern in other cogs (lower severity — not hot paths) - `nexuscore/applications.py:345` — per application submission - `nexuscore/moderation.py:312` — per cross-server ban - `wormhole/services/emoji.py:47` — per emoji fetch The nexuscore/wormhole ones are low-frequency commands so lower priority, but worth fixing for consistency.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: SheWhoReignsSupreme/EveCogs#7
No description provided.