enough-pilot-49245
09/14/2025, 4:13 PMtall-airline-16756
09/14/2025, 4:13 PMtall-airline-16756
09/14/2025, 4:14 PMasyncio.wait()
to await your tasks and cancel any that are still pending when you’re tearing down.
2. Use Reliable Task Cancellation: Tools like utils.aio.cancel_and_wait()
can be really helpful to safely cancel and await tasks, which prevents that "destroyed but pending" issue.
3. Keep Track and Cancel Long-Lived Tasks: It’s a good idea to hold references to background tasks created with asyncio.create_task()
and cancel them all together when shutting down.
4. Implement a Graceful Shutdown: Catch termination signals and run cleanup routines to ensure all tasks are cancelled before the event loop closes.
5. Detect Leaked Tasks Early: Using test fixtures to spot any uncancelled tasks after execution can save you headaches later on.
Remember to pair every asyncio.create_task()
call with proper cancellation logic, and consider using structured cleanup patterns in finally
blocks or dedicated shutdown routines. If you’d like, I’m here to help you through any of these steps or answer any questions you have!
Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | README.md | stream_audio.py