Commit f5df871
User
feat(high): Fix memory leak in session management with SessionManager
Implemented comprehensive session lifecycle management:
- SessionManager class with LRU eviction policy
- Automatic cleanup of expired sessions
- Connection pooling and reuse
- Memory leak prevention
Features:
- LRU cache with configurable max_sessions limit
- Automatic eviction of least recently used sessions
- TTL-based session expiration (default: 1 hour)
- Background cleanup task (default: 5 minutes interval)
- Thread-safe session access with asyncio.Lock
- Statistics tracking (cache hits, misses, evictions)
Session Lifecycle:
1. get_session() - Get or create session (lazy initialization)
2. Session pooling - Reuse existing sessions
3. LRU eviction - Remove oldest when at capacity
4. TTL expiration - Cleanup inactive sessions
5. Graceful shutdown - Close all sessions on shutdown
Memory Leak Prevention:
- Proper connection cleanup via clear_session()
- Automatic eviction prevents unbounded growth
- Background task removes expired sessions
- Shutdown closes all active sessions
Integration:
- Added to DIContainer with lazy initialization
- Integrated into initialize() and shutdown() lifecycle
- Configurable via ErniConfig.data_dir
Configuration:
- max_sessions: 100 (prevents memory exhaustion)
- session_ttl: 3600 seconds (1 hour)
- cleanup_interval: 300 seconds (5 minutes)
- db_path: data/sessions.db (persistent storage)
Benefits:
- Prevents memory leaks from unclosed connections
- Reduces database connection overhead
- Improves performance with session reuse
- Automatic cleanup of stale sessions
- Thread-safe concurrent access
Tests:
- 13 comprehensive unit tests
- Test LRU eviction, TTL expiration, cleanup
- Test concurrent access, statistics
- All tests passing (13/13)
Before:
- Sessions created but never cleaned up
- Database connections left open
- Memory usage grows unbounded
- No session reuse
After:
- Automatic session lifecycle management
- Proper connection cleanup
- Bounded memory usage (max 100 sessions)
- Session pooling and reuse
- Background cleanup task
Resolves: openai#11 (HIGH priority)
Impact: Eliminates memory leaks and improves stability1 parent 59e069e commit f5df871
File tree
4 files changed
+643
-0
lines changed- examples/erni-foto-agency
- erni_foto_agency
- session
- tests/unit
4 files changed
+643
-0
lines changedLines changed: 28 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
123 | 124 | | |
124 | 125 | | |
125 | 126 | | |
| 127 | + | |
126 | 128 | | |
127 | 129 | | |
128 | 130 | | |
| |||
274 | 276 | | |
275 | 277 | | |
276 | 278 | | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
277 | 297 | | |
278 | 298 | | |
279 | 299 | | |
| |||
371 | 391 | | |
372 | 392 | | |
373 | 393 | | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
374 | 397 | | |
375 | 398 | | |
376 | 399 | | |
| |||
387 | 410 | | |
388 | 411 | | |
389 | 412 | | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
390 | 417 | | |
391 | 418 | | |
392 | 419 | | |
| |||
419 | 446 | | |
420 | 447 | | |
421 | 448 | | |
| 449 | + | |
422 | 450 | | |
423 | 451 | | |
424 | 452 | | |
| |||
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
0 commit comments