async-error-handling

Implement async context managers and FastAPI lifespan error handling patterns.

1|Updated Jan 25, 2026
One-click install
npx skills add https://github.com/kellyson520/TG-ONE --skill async-error-handling-kellyson520
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: async-error-handling
Source: https://github.com/kellyson520/TG-ONE/tree/main/.agent/skills/async-error-handling
Command: npx skills add https://github.com/kellyson520/TG-ONE --skill async-error-handling-kellyson520

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) components.

What problem does it solve?

Async code often suffers from resource leaks, failed shutdowns, and unpredictable behavior when CancelledError and other asyncio exceptions are mishandled. This Skill provides expert guidance on Python async/await error handling patterns, context managers, and FastAPI lifespan management to ensure exceptions propagate correctly and resources are cleaned up.

Core Features & Use Cases

  • Standardized templates for asynchronous context management and FastAPI lifespan handling.
  • Automated checks, templates, and guidance to fix common pitfalls and improve reliability across services.
  • Real-world examples and references to production-grade patterns for robust asynchronous workflows.

Quick Start

Implement a basic async context manager following the standard pattern to ensure proper cancellation handling and cleanup.

Frequently Asked Questions about async-error-handling

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I handle CancelledError in Python asyncio to prevent resource leaks?

To handle CancelledError in Python asyncio, you must catch the exception, execute your cleanup logic in a finally block, and safely re-raise it to ensure the cancellation propagates correctly and resources are cleaned up without breaking the event loop.

What is the best way to manage FastAPI lifespan for asynchronous resource cleanup?

The best way to manage FastAPI lifespan is using asynccontextmanager to handle startup and shutdown events, ensuring asynchronous resources are properly initialized and cleaned up even when unexpected exceptions or cancellations occur during the application lifecycle.

Why does my async context manager fail during event loop shutdown?

Async context managers fail during shutdown when CancelledError is swallowed or not re-raised. You must explicitly track CancelledError within your finally cleanup blocks to maintain predictable async behavior and ensure resources are fully released.

Can I use standard asynccontextmanager patterns for production-grade FastAPI services?

Yes, you can apply asynccontextmanager patterns to production-grade FastAPI services. Standardized templates provide robust asynchronous workflows with explicit exception handling and safe re-raising suitable for small utilities and large-scale services.

How do I safely re-raise asyncio exceptions without breaking the event loop?

You safely re-raise asyncio exceptions by catching CancelledError specifically, performing your necessary cleanup operations in a finally block, and then re-raising the exception immediately to preserve the cancellation state expected by the asyncio event loop.