CRITICAL: Unhandled Asyncio Tasks (Fire-and-Forget Pattern) #1
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 you use
asyncio.create_task()without tracking or error handling, any exception that occurs in that background task is silently swallowed. The task runs in the background, and if it crashes, Python doesn'''t log the error or notify you in any way.Where It Occurs
bot/bot.py#L300- Bipolar interjection checkbot/bot.py#L372- Image handling interjectionbot/bot.py#L454- Video handling interjectionbot/bot.py#L576- GIF handling interjectionbot/bot.py#L669- Embed handling interjectionbot/bot.py#L861-L862- Delayed wakeup taskbot/utils/autonomous.py#L169-L172- Autonomous action tasksbot/utils/bipolar_mode.py#L1116- Bipolar argument taskWhy This Is a Problem
What Can Go Wrong
Scenario 1: Bipolar Interjection Fails
check_for_interjection()starts as a background taskasyncio.TimeoutErrorScenario 2: Voice Session Auto-Leave Fails
RuntimeError: WebSocket connection is closedProposed Fix
Create a
create_tracked_task()helper function that wraps all background tasks with error handling:Severity
CRITICAL - Feature failures are invisible and can persist indefinitely without detection.
Files Affected
11 files total including bot.py, autonomous.py, bipolar_mode.py, voice_receiver.py, voice_manager.py
Fixed in commit
7b7abcf. Added utils/task_tracker.py with a create_tracked_task() wrapper that logs errors with full tracebacks, handles cancellation, and keeps strong references to prevent GC. Replaced all 17 fire-and-forget asyncio.create_task() calls across 7 files (bot.py, autonomous.py, bipolar_mode.py, uno.py, voice_receiver.py, persona_dialogue.py). Already-tracked tasks in voice_audio.py and voice_manager.py were left unchanged.