HIGH: No Graceful Shutdown on Bot Exit #5
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What the Problem Is
When the bot is stopped (SIGTERM/SIGINT), it does not properly clean up resources. Voice connections remain active, WebSocket connections hang, and in-memory state is lost.
Where It Occurs
bot/bot.py#L861-L862- Main loop run without shutdown handlerbot/bot.py- No signal handlers registeredbot/server_manager.py- Server processes not terminated gracefullyWhy This Is a Problem
What Can Go Wrong
Scenario 1: Docker Container Stops
docker-compose downScenario 2: Keyboard Interrupt During File Write
Proposed Fix
Implement graceful shutdown handler:
Severity
HIGH - Lack of graceful shutdown causes data loss, corrupted state, and ghost connections after restarts.
Files Affected
bot/bot.py, bot/server_manager.py, bot/utils/voice_manager.py
Fixed in commit
8d51370. Implemented a comprehensive async graceful_shutdown() function that replaces the old sync-only handler. The shutdown sequence now: (1) ends active voice sessions, (2) saves autonomous state, (3) stops APScheduler, (4) cancels all tracked background tasks, (5) closes the Discord gateway. Signal handlers (SIGTERM/SIGINT) schedule the async shutdown on the event loop, with atexit kept as last-resort fallback.