Skip to content
cancel-async-tasks logo

Async Task Cancellation with Cleanup

cancel-async-tasks

Guidance for implementing asyncio task cancellation with proper cleanup, especially for handling keyboard interrupts (Ctrl+C). This skill should be used when tasks involve asyncio cancellation, signal handling, or graceful shutdown of concurrent Python tasks.

SKILL.md

Full skill instructions

Async Task Cancellation with Cleanup

This skill provides guidance for implementing proper cancellation handling in Python asyncio code, with particular focus on keyboard interrupt (Ctrl+C) handling and graceful cleanup.

Core Concepts

KeyboardInterrupt vs CancelledError

Understanding the difference is critical:

  • KeyboardInterrupt: A BaseException raised in the main thread when Ctrl+C is pressed. It does NOT propagate into async coroutines directly.
  • CancelledError: An Exception raised inside coroutines when task.cancel() is called. This IS what coroutines receive during cancellation.

Critical Insight: Placing except KeyboardInterrupt inside an async function will NOT catch Ctrl+C. The interrupt is raised at the event loop level, not within coroutines.

Signal Handling in asyncio

To properly handle Ctrl+C in asyncio:

  1. Use loop.add_signal_handler() to register SIGINT/​SIGTERM handlers
  2. Or wrap asyncio.run() in a try/​except at the synchronous entry point
  3. The signal handler should trigger task cancellation, which then raises CancelledError in coroutines

Potential Approaches

Approach 1: Signal Handler Pattern

Register signal handlers at the event loop level to trigger graceful shutdown:

1. Get the event loop
2. Add signal handlers for SIGINT and SIGTERM
3. In the signal handler, cancel all running tasks
4. In coroutines, catch CancelledError to perform cleanup

Approach 2: Synchronous Wrapper Pattern

Wrap the async entry point to catch KeyboardInterrupt synchronously:

1. Define the async main logic
2. In a synchronous wrapper, call asyncio.run() inside try/​except
3. Catch KeyboardInterrupt in the synchronous code
4. Handle cleanup or re-raise as needed

Approach 3: TaskGroup with Exception Handling (Python 3.11+)

Use asyncio.TaskGroup for structured concurrency:

1. Use async with asyncio.TaskGroup() for task management
2. TaskGroup handles cancellation propagation automatically
3. Catch ExceptionGroup for handling multiple failures

Verification Strategies

Test with Actual Signals

Do NOT rely solely on timeout-based cancellation testing. Verify with actual signals:

import os
import signal

# Simulate Ctrl+C in tests
os.kill(os.getpid(), signal.SIGINT)

Verify Cleanup Execution

Create observable side effects to confirm cleanup runs:

  • Write to a file during cleanup
  • Set a flag variable
  • Log cleanup actions

Test Multiple Cancellation Scenarios

  1. Normal completion (no cancellation)
  2. Timeout-based cancellation (asyncio.timeout)
  3. Manual task.cancel() calls
  4. Actual SIGINT signal
  5. SIGTERM signal

Verify Exception Propagation

Test that exceptions from individual tasks are properly collected and reported, not silently swallowed.

Common Pitfalls

Pitfall 1: Catching KeyboardInterrupt in Async Functions

Wrong:

async def my_coroutine():
    try:
        await some_operation()
    except KeyboardInterrupt:  # Will NOT catch Ctrl+C!
        await cleanup()

Correct:

async def my_coroutine():
    try:
        await some_operation()
    except asyncio.CancelledError:  # This WILL be raised on cancellation
        await cleanup()
        raise  # Re-raise to propagate cancellation

Pitfall 2: Not Re-raising CancelledError

Swallowing CancelledError prevents proper cancellation propagation. Always re-raise after cleanup unless there's a specific reason not to.

Pitfall 3: Testing Only Happy Path Cancellation

Testing with asyncio.timeout() or asyncio.wait_for() does NOT verify keyboard interrupt handling. These trigger TimeoutError, not the same path as SIGINT.

Pitfall 4: Ignoring Cleanup Cancellation

Cleanup code itself can be cancelled if it awaits. Use asyncio.shield() for critical cleanup:

async def cleanup():
    await asyncio.shield(critical_cleanup_operation())

Pitfall 5: Empty or Invalid Input Handling

Always validate inputs:

  • Empty task lists
  • Invalid concurrency limits (max_concurrent <= 0)
  • None values

Pitfall 6: Assuming gather Handles All Exceptions

asyncio.gather(return_exceptions=True) collects exceptions but doesn't handle them. Results must be inspected for exception instances.

Edge Cases to Consider

  1. Empty task list: Return early with empty results
  2. max_concurrent <= 0: Raise ValueError or use sensible default
  3. All tasks fail: Ensure all exceptions are reported
  4. Partial completion: Track which tasks completed vs cancelled
  5. Nested cancellation: Cleanup code getting cancelled
  6. Multiple rapid signals: Debounce or ignore duplicate signals
  7. Windows compatibility: add_signal_handler doesn't work on Windows for SIGINT; use alternative approaches

Platform Considerations

Unix/​Linux/​macOS

  • loop.add_signal_handler() works for SIGINT, SIGTERM
  • Signal handlers run in the main thread

Windows

  • add_signal_handler() is limited; only works for SIGINT in some cases
  • Consider using signal.signal() before starting the event loop
  • Or rely on the synchronous wrapper pattern

More Productivity & Planning skills

brainstorming logo
Productivity & Planning

brainstorming

Structured design dialogue that validates ideas before implementation begins.

295.4K 385.7K
View
ui-ux-pro-max logo
Productivity & Planning

ui-ux-pro-max

Comprehensive design intelligence for web and mobile UI/UX across 10 technology stacks.

133.1K 383.9K
View
writing-plans logo
Productivity & Planning

writing-plans

Comprehensive implementation plans for multi-step tasks, breaking down specs into bite-sized, testable steps.

295.4K 268K
View
using-superpowers logo
Productivity & Planning

using-superpowers

Introduction to the obra skills system with mandatory skill invocation rules and best practices.

295.4K 259.8K
View
executing-plans logo
Productivity & Planning

executing-plans

Execute a written implementation plan with critical review and task checkpoints.

295.4K 229.1K
View
dispatching-parallel-agents logo
Productivity & Planning

dispatching-parallel-agents

Delegate independent tasks to specialized agents working concurrently with isolated context.

295.4K 206.3K
View
using-git-worktrees logo
Productivity & Planning

using-git-worktrees

Isolated git worktrees with smart directory selection and safety verification.

295.4K 205.2K
View
webapp-testing logo
Productivity & Planning

webapp-testing

Toolkit for interacting with and testing local web applications using Playwright. Supports verifying frontend functionality, debugging UI behavior, capturing browser screenshots, and viewing browser logs.

179.7K 170.6K
View
content-strategy logo
Productivity & Planning

content-strategy

Plan searchable and shareable content that drives traffic, builds authority, and generates leads.

53.3K 150.9K
View
repo-intake-and-plan logo
Productivity & Planning

repo-intake-and-plan

README-first repository scanner that extracts commands and classifies reproduction candidates without executing them.

497 139.6K
View
marketing-ideas logo
Productivity & Planning

marketing-ideas

Brainstorm and prioritize marketing strategies tailored to your SaaS stage, budget, and goals.

53.3K 137.2K
View
site-architecture logo
Productivity & Planning

site-architecture

Plan and optimize your website's page hierarchy, navigation, URL structure, and internal linking.

53.3K 112.8K
View

Productivity AI tools

Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View
SaveDay logo
Productivity

SaveDay

Capture, organize, and utilize your knowledge effortlessly.

Free
View
Any Summary logo
Productivity

Any Summary

Instant Summaries of Audio & Video Interviews with AnySummary

Freemium
View
M
Productivity

Map This

Transform PDFs into engaging mind maps.

Freemium
View
ChatPDF logo
Productivity

ChatPDF

Chat with any PDF instantly

Free
View
I
Productivity

intellisay

Create an optimal daily plan using your voice

Paid
View
A
Productivity

Aurora AI

A productivity platform to centralize organizational knowledge and workflows with contextual AI assistance.

Paid
View
AskYourPDF logo
Productivity

AskYourPDF

AskYourPDF Pricing Plans: Tailored to Your Needs

Free
View