HIGH: Unclosed aiohttp Sessions Cause Resource Leaks #19

Open
opened 2026-02-16 22:39:56 +02:00 by Koko210 · 0 comments
Owner

aiohttp sessions are created but not properly closed, leading to resource leaks and connection exhaustion.

Where It Occurs

  • bot/utils/voice_audio.py - aiohttp.ClientSession() without context manager
  • bot/stt_client.py - Sessions created without cleanup
  • cat-plugins/danbooru_client.py - aiohttp sessions not closed
  • bot/utils/image_generation.py - Potential session leaks

Why This Is a Problem

  1. Resource Leaks: Each unclosed session holds open TCP connections
  2. Connection Exhaustion: OS limits max open file descriptors
  3. Slow Performance: Connection pools remain open
  4. Memory Leaks: Session objects not garbage collected

What Can Go Wrong

Scenario 1: Session Exhaustion After Extended Runtime

  1. Bot runs for 48 hours processing messages
  2. Each message creates new aiohttp session for image generation
  3. Sessions are never closed, connections stay open
  4. After 1000 sessions, OS reaches file descriptor limit (typically 1024)
  5. Next attempt to create session fails with OSError
  6. Bot cannot make HTTP requests, features break

Scenario 2: WebSocket Connection Pool Full

  1. Voice audio streaming creates persistent aiohttp session
  2. Session not closed when user leaves voice channel
  3. Connection pool remains allocated
  4. After 20 voice sessions, connection pool exhausted
  5. New voice users cannot connect
  6. Bot shows error but cannot recover without restart

Proposed Fix

Use async with context managers for all aiohttp sessions. Create APIClient class with session management and close on shutdown.

Severity

HIGH - Unclosed sessions cause resource exhaustion and require bot restart.

Files Affected

bot/utils/voice_audio.py, bot/stt_client.py, cat-plugins/danbooru_client.py, bot/utils/image_generation.py

aiohttp sessions are created but not properly closed, leading to resource leaks and connection exhaustion. ## Where It Occurs - bot/utils/voice_audio.py - aiohttp.ClientSession() without context manager - bot/stt_client.py - Sessions created without cleanup - cat-plugins/danbooru_client.py - aiohttp sessions not closed - bot/utils/image_generation.py - Potential session leaks ## Why This Is a Problem 1. Resource Leaks: Each unclosed session holds open TCP connections 2. Connection Exhaustion: OS limits max open file descriptors 3. Slow Performance: Connection pools remain open 4. Memory Leaks: Session objects not garbage collected ## What Can Go Wrong ### Scenario 1: Session Exhaustion After Extended Runtime 1. Bot runs for 48 hours processing messages 2. Each message creates new aiohttp session for image generation 3. Sessions are never closed, connections stay open 4. After 1000 sessions, OS reaches file descriptor limit (typically 1024) 5. Next attempt to create session fails with OSError 6. Bot cannot make HTTP requests, features break ### Scenario 2: WebSocket Connection Pool Full 1. Voice audio streaming creates persistent aiohttp session 2. Session not closed when user leaves voice channel 3. Connection pool remains allocated 4. After 20 voice sessions, connection pool exhausted 5. New voice users cannot connect 6. Bot shows error but cannot recover without restart ## Proposed Fix Use async with context managers for all aiohttp sessions. Create APIClient class with session management and close on shutdown. ## Severity HIGH - Unclosed sessions cause resource exhaustion and require bot restart. ## Files Affected bot/utils/voice_audio.py, bot/stt_client.py, cat-plugins/danbooru_client.py, bot/utils/image_generation.py
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Koko210/miku-discord#19