coroutines-cancellation-exceptions

Enforce Kotlin coroutine cancellation and exception handling patterns with runCatching and ensureActive.

Updated Apr 8, 2026
One-click install
npx skills add https://github.com/ClankerGuru/opsx --skill coroutines-cancellation-exceptions
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: coroutines-cancellation-exceptions
Source: https://github.com/ClankerGuru/opsx/tree/main/cli/src/main/resources/content/skills/coroutines-cancellation-exceptions
Command: npx skills add https://github.com/ClankerGuru/opsx --skill coroutines-cancellation-exceptions

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Coroutines cancellation and exception handling are easy to mishandle, leading to leaked work, stuck jobs, or silent cancellation.

Core Features & Use Cases

  • Enforces safe cancellation patterns in suspend functions using runCatching and currentCoroutineContext().ensureActive() to rethrow CancellationException.
  • Supports structured concurrency decisions, whether to use supervisorScope, and how to propagate failures without killing siblings.
  • Guides proper cleanup using NonCancellable regions to perform suspending operations after cancellation.

Quick Start

Apply the coroutine cancellation guidelines by wrapping suspending work in runCatching and ensuring active state in failure handlers.

Frequently Asked Questions about coroutines-cancellation-exceptions

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

FAQPage Schema
Why does my Kotlin coroutine cancellation leak work or get stuck?

Kotlin coroutine cancellation leaks or stuck jobs usually happen when suspending functions swallow CancellationException. Enforcing safe patterns with runCatching and ensureActive prevents silent cancellation and ensures proper job termination.

How do I handle exceptions in Kotlin coroutines without killing sibling jobs?

To handle exceptions in Kotlin coroutines without killing siblings, use supervisorScope to propagate failures independently. This structured concurrency boundary prevents a single child's exception from canceling other parallel tasks.

How do I perform suspending cleanup after a coroutine is cancelled?

You perform suspending cleanup after a coroutine is cancelled by wrapping the cleanup logic in a NonCancellable region. This allows suspending operations to complete safely without being interrupted by the active cancellation state.

When should I use runCatching in Kotlin suspend functions?

You should use runCatching in Kotlin suspend functions to safely wrap suspending work and catch exceptions. You must pair it with currentCoroutineContext().ensureActive() in failure handlers to rethrow CancellationException and avoid masking cancellation.

Does structured concurrency in Kotlin require manual cancellation checks?

Structured concurrency in Kotlin relies on cooperative cancellation, meaning suspending functions must check for cancellation manually. Using ensureActive verifies the active state and rethrows CancellationException to stop execution immediately when cancelled.

What is the best way to rethrow CancellationException in Kotlin coroutines?

The best way to rethrow CancellationException in Kotlin coroutines is calling currentCoroutineContext().ensureActive() inside your catch or runCatching failure blocks. This enforces safe cancellation patterns and prevents the exception from being swallowed.