fix(tasks): replace fire-and-forget asyncio.create_task with create_tracked_task
Add utils/task_tracker.py with create_tracked_task() that wraps background tasks with error logging, cancellation handling, and reference tracking. Replace all 17 fire-and-forget asyncio.create_task() calls across 7 files: - bot/bot.py (5 interjection checks) - bot/utils/autonomous.py (2 check-and-act/react tasks) - bot/utils/bipolar_mode.py (3 argument tasks) - bot/commands/uno.py (1 game loop task) - bot/utils/voice_receiver.py (3 STT/interruption callbacks) - bot/utils/persona_dialogue.py (4 dialogue turn/interjection tasks) Previously-tracked tasks (voice_audio.py, voice_manager.py) were left as-is since they already store task references for cancellation. Closes #1
This commit is contained in:
@@ -17,6 +17,7 @@ import discord
|
||||
from discord.ext import voice_recv
|
||||
|
||||
from utils.stt_client import STTClient
|
||||
from utils.task_tracker import create_tracked_task
|
||||
|
||||
logger = logging.getLogger('voice_receiver')
|
||||
|
||||
@@ -256,11 +257,11 @@ class VoiceReceiverSink(voice_recv.AudioSink):
|
||||
stt_client = STTClient(
|
||||
user_id=user_id,
|
||||
stt_url=self.stt_url,
|
||||
on_partial_transcript=lambda text, timestamp: asyncio.create_task(
|
||||
self._on_partial_transcript(user_id, text)
|
||||
on_partial_transcript=lambda text, timestamp: create_tracked_task(
|
||||
self._on_partial_transcript(user_id, text), task_name="stt_partial_transcript"
|
||||
),
|
||||
on_final_transcript=lambda text, timestamp: asyncio.create_task(
|
||||
self._on_final_transcript(user_id, text, user)
|
||||
on_final_transcript=lambda text, timestamp: create_tracked_task(
|
||||
self._on_final_transcript(user_id, text, user), task_name="stt_final_transcript"
|
||||
),
|
||||
)
|
||||
|
||||
@@ -421,8 +422,9 @@ class VoiceReceiverSink(voice_recv.AudioSink):
|
||||
self.interruption_audio_count.pop(user_id, None)
|
||||
|
||||
# Call interruption handler (this sets miku_speaking=False)
|
||||
asyncio.create_task(
|
||||
self.voice_manager.on_user_interruption(user_id)
|
||||
create_tracked_task(
|
||||
self.voice_manager.on_user_interruption(user_id),
|
||||
task_name="voice_user_interruption"
|
||||
)
|
||||
else:
|
||||
# Audio below RMS threshold (silence) - reset interruption tracking
|
||||
|
||||
Reference in New Issue
Block a user