Priyanshi Saxena commited on
Commit
18efaca
·
1 Parent(s): c785b3f

fix: Improve session cleanup and management

Browse files

- Enhanced ChartDataTool cleanup method with proper session management
- Added try/finally blocks to ensure CoinGecko tool cleanup
- Improved garbage collection for better memory management
- Fixed aiohttp session closure issues that were causing warnings

Files changed (1) hide show
  1. src/tools/chart_data_tool.py +13 -2
src/tools/chart_data_tool.py CHANGED
@@ -90,6 +90,7 @@ class ChartDataTool(BaseTool):
90
 
91
  async def _get_price_chart_data(self, symbol: str, days: int) -> str:
92
  """Get price chart data with fallback for API failures"""
 
93
  try:
94
  # First try to get real data from CoinGecko
95
  from src.tools.coingecko_tool import CoinGeckoTool
@@ -155,6 +156,13 @@ class ChartDataTool(BaseTool):
155
  logger.error(f"Price chart data generation failed: {e}")
156
  # Final fallback to mock data
157
  return await self._get_mock_price_data(symbol, days)
 
 
 
 
 
 
 
158
 
159
  async def _get_mock_price_data(self, symbol: str, days: int) -> str:
160
  """Fallback mock price data"""
@@ -396,5 +404,8 @@ class ChartDataTool(BaseTool):
396
 
397
  async def cleanup(self):
398
  """Cleanup method for session management"""
399
- # ChartDataTool doesn't maintain persistent connections, so nothing to clean up
400
- pass
 
 
 
 
90
 
91
  async def _get_price_chart_data(self, symbol: str, days: int) -> str:
92
  """Get price chart data with fallback for API failures"""
93
+ coingecko = None
94
  try:
95
  # First try to get real data from CoinGecko
96
  from src.tools.coingecko_tool import CoinGeckoTool
 
156
  logger.error(f"Price chart data generation failed: {e}")
157
  # Final fallback to mock data
158
  return await self._get_mock_price_data(symbol, days)
159
+ finally:
160
+ # Cleanup CoinGecko tool session
161
+ if coingecko and hasattr(coingecko, 'cleanup'):
162
+ try:
163
+ await coingecko.cleanup()
164
+ except Exception:
165
+ pass # Ignore cleanup errors
166
 
167
  async def _get_mock_price_data(self, symbol: str, days: int) -> str:
168
  """Fallback mock price data"""
 
404
 
405
  async def cleanup(self):
406
  """Cleanup method for session management"""
407
+ # ChartDataTool creates temporary tools that may have sessions
408
+ # Since we don't maintain persistent references, sessions should auto-close
409
+ # But we can force garbage collection to ensure cleanup
410
+ import gc
411
+ gc.collect()