fix: unclosed aiohttp client session causing resource leak #52

Closed
opened 2026-02-05 00:32:42 +09:00 by agentson · 0 comments
Collaborator

Problem

Unclosed aiohttp.ClientSession and TCPConnector instances cause resource leaks on shutdown.

Error Log

ERROR: Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f52bbbc9400>

ERROR: Unclosed connector
connections: ['deque([(<aiohttp.client_proto.ResponseHandler object at 0x7f52bbbcc170>, 61302.617584737)])']
connector: <aiohttp.connector.TCPConnector object at 0x7f52bc4e75c0>

Root Cause

  • aiohttp sessions created but not properly closed on shutdown
  • Missing async with context manager or explicit await session.close()
  • Likely in src/broker/kis_api.py or src/broker/overseas.py

Impact

  • Severity: LOW - cosmetic error, but indicates improper resource management
  • Consequence:
    • Warning messages on every shutdown
    • Potential memory leaks in long-running processes
    • May leave open TCP connections

Solution

Best Practice: Use async context manager

async with aiohttp.ClientSession() as session:
    async with session.get(url) as response:
        data = await response.json()

Alternative: Explicit cleanup in shutdown handler

class KISBroker:
    async def close(self):
        if self._session:
            await self._session.close()

# In main.py shutdown
try:
    await run()
finally:
    await broker.close()
    await overseas_broker.close()

Investigation Required

  1. Find all aiohttp session creation points
  2. Verify proper cleanup on normal/abnormal shutdown
  3. Add async close() methods to broker classes if missing

Files to Investigate

  • src/broker/kis_api.py - Check session management
  • src/broker/overseas.py - Check session lifecycle
  • src/main.py - Add cleanup in shutdown handler

References

## Problem Unclosed `aiohttp.ClientSession` and `TCPConnector` instances cause resource leaks on shutdown. ## Error Log ``` ERROR: Unclosed client session client_session: <aiohttp.client.ClientSession object at 0x7f52bbbc9400> ERROR: Unclosed connector connections: ['deque([(<aiohttp.client_proto.ResponseHandler object at 0x7f52bbbcc170>, 61302.617584737)])'] connector: <aiohttp.connector.TCPConnector object at 0x7f52bc4e75c0> ``` ## Root Cause - aiohttp sessions created but not properly closed on shutdown - Missing `async with` context manager or explicit `await session.close()` - Likely in `src/broker/kis_api.py` or `src/broker/overseas.py` ## Impact - **Severity**: LOW - cosmetic error, but indicates improper resource management - **Consequence**: - Warning messages on every shutdown - Potential memory leaks in long-running processes - May leave open TCP connections ## Solution **Best Practice: Use async context manager** ```python async with aiohttp.ClientSession() as session: async with session.get(url) as response: data = await response.json() ``` **Alternative: Explicit cleanup in shutdown handler** ```python class KISBroker: async def close(self): if self._session: await self._session.close() # In main.py shutdown try: await run() finally: await broker.close() await overseas_broker.close() ``` ## Investigation Required 1. Find all aiohttp session creation points 2. Verify proper cleanup on normal/abnormal shutdown 3. Add async `close()` methods to broker classes if missing ## Files to Investigate - `src/broker/kis_api.py` - Check session management - `src/broker/overseas.py` - Check session lifecycle - `src/main.py` - Add cleanup in shutdown handler ## References - [aiohttp docs: Proper session cleanup](https://docs.aiohttp.org/en/stable/client_advanced.html#graceful-shutdown)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: jihoson/The-Ouroboros#52