kotlin-coroutines-structured-concurrency

Refactor stored CoroutineScope usage into suspending APIs with lifecycle-bound scopes.

908|42|Updated May 12, 2026
One-click install
npx skills add https://github.com/chrisbanes/skills --skill kotlin-coroutines-structured-concurrency-chrisbanes
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-coroutines-structured-concurrency
Source: https://github.com/chrisbanes/skills/tree/main/skills/kotlin-coroutines-structured-concurrency
Command: npx skills add https://github.com/chrisbanes/skills --skill kotlin-coroutines-structured-concurrency-chrisbanes

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Kotlin coroutines are easy to misuse when a class stores a CoroutineScope or when non-suspending APIs launch work. This Skill teaches guiding the code to structure concurrency correctly so lifecycle ownership stays with the caller and work is expressed through suspending APIs.

Core Features & Use Cases

  • Prevents stored CoroutineScope as a property; replaces with suspending APIs.
  • Moves coroutine launches out of init blocks and DI-bound singletons; ensures cancellation and error semantics are explicit.
  • Provides guidance for UI state holders and their coroutine boundaries, and outlines patterns for explicit launch sites and lifecycle-bound scopes.

Quick Start

Refactor a repository that stores a CoroutineScope to expose suspend functions and delegate lifecycle ownership to the caller.

Frequently Asked Questions about kotlin-coroutines-structured-concurrency

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

FAQPage Schema
How do I refactor a stored CoroutineScope in Kotlin to use suspending APIs?

To refactor a stored CoroutineScope, remove the property and expose suspend functions instead, delegating lifecycle ownership to the caller. This ensures work is expressed through suspending APIs rather than non-suspending launches.

Why should I move coroutine launches out of init blocks in Kotlin?

Moving coroutine launches out of init blocks ensures cancellation and error semantics are explicit. It enforces lifecycle ownership by the caller, requiring mapping to lifecycle-scoped contexts like viewModelScope.

What is structured concurrency in Kotlin coroutines?

Structured concurrency in Kotlin coroutines means lifecycle ownership stays with the caller and work is expressed through suspending APIs. It prevents misuse like storing CoroutineScope or using fire-and-forget calls.

How do I test Kotlin coroutines after converting to suspending APIs?

You validate changes by testing with runTest. This confirms that removing stored CoroutineScope and converting to suspending APIs maintains correct cancellation and error semantics across repositories and UI layers.

Can I use this structured concurrency refactor for UI state holders and repositories?

Yes, this refactor applies to codebases with UI state holders and repositories. It provides guidance for coroutine boundaries, outlining patterns for explicit launch sites and lifecycle-bound scopes across data sources and UI layers.

How do I fix fire-and-forget coroutine calls and broad-catch around suspend functions?

Fix fire-and-forget calls by removing stored CoroutineScope and converting to suspending APIs. This enforces lifecycle ownership by the caller and requires mapping to lifecycle-scoped contexts like viewModelScope.