wide-dress-57902
08/23/2025, 8:51 PMtall-airline-16756
08/23/2025, 8:51 PMtall-airline-16756
08/23/2025, 8:52 PMadd_shutdown_callback(), the callback can optionally take a single string parameter that tells you the reason for the shutdown.
Here’s a simple example to show you how it works:
async def entrypoint(ctx: JobContext):
async def shutdown_handler(reason: str):
if reason == "room disconnected":
# Handle unexpected disconnection
logger.warning("Room disconnected unexpectedly")
# Maybe try to reconnect or save your state here
elif reason == "Session ended":
# Handle graceful shutdown
logger.info("Session ended gracefully")
# Do your usual cleanup
else:
# Handle other shutdown reasons
logger.info(f"Shutdown with reason: {reason}")
ctx.add_shutdown_callback(shutdown_handler)
Some common shutdown reasons you might see include:
• "room disconnected" - When the room connection drops unexpectedly
• Reasons you provide yourself when calling ctx.shutdown(reason="your reason")
• Various system-generated reasons depending on how the shutdown happens
This way, you can easily tailor what happens during different shutdown situations—whether it’s a smooth exit, an unexpected disconnect, or something else.
Sources: examples/voice_agents/realtime_joke_teller.py