0Pricing
MCP Academy · Lesson

Clean Shutdown & Resource Release

Close pools and files when the server stops.

Why Shutdown Matters

Open connections that are never closed leak resources: sockets pile up, pools stay reserved, and databases stay busy.

Teardown Lives After Yield

Everything after yield in your lifespan is shutdown code, running when the server is told to stop.

async def lifespan(server):
    db = await connect()
    yield {"db": db}
    await db.close()

All lessons in this course

  1. The Lifespan Startup Hook
  2. Share State via App Context
  3. Access the Context Object
  4. Clean Shutdown & Resource Release
← Back to MCP Academy