[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M6FDP4J5NTQSJQ1P7SZ043QW":3,"skill-same:01M6FDP4J5NTQSJQ1P7SZ043QW":50},{"skillNo":4,"name":5,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":15,"categories":16,"keywords":17,"publishedAt":23,"linkOnly":24,"markdownContent":25,"markdownBody":25,"frontMatter":26,"refName":27,"commitSha":28,"skillDirectory":29,"forksCount":30,"repositoryHtmlUrl":31,"repositoryHomepageUrl":32,"ownerHtmlUrl":33,"ownerBlogUrl":6,"ownerDescription":34,"updatedAt":35,"install":36,"resources":49},"01M6FDP4J5NTQSJQ1P7SZ043QW","kotlin-concurrency-expert",null,"ComeOnOliver\u002Fskillshub","ComeOnOliver","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F43215937?v=4","01M6E5JNFECJAAVFE0W7SMWWD2","Haoran Shu","01M6E5JNNDQS7AJZDYFQ8TPZCK",67,"MIT","https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002Fnew-silvermoon\u002Fawesome-android-agent-skills\u002Fkotlin-concurrency-expert\u002FSKILL.md",[],[18,19,20,21,22],"concurrency","kotlin","expert","concurrency expert","kotlin concurrency expert","2026-11-10 10:12:30",false,"# Kotlin Concurrency Expert\n\n## Overview\n\nReview and fix Kotlin Coroutines issues in Android codebases by applying structured concurrency, lifecycle safety, proper scoping, and modern best practices with minimal behavior changes.\n\n## Workflow\n\n### 1. Triage the Issue\n\n- Capture the exact error, crash, or symptom (ANR, memory leak, race condition, incorrect state).\n- Check project coroutines setup: `kotlinx-coroutines-android` version, `lifecycle-runtime-ktx` version.\n- Identify the current scope context (`viewModelScope`, `lifecycleScope`, custom scope, or none).\n- Confirm whether the code is UI-bound (`Dispatchers.Main`) or intended to run off the main thread (`Dispatchers.IO`, `Dispatchers.Default`).\n- Verify Dispatcher injection patterns for testability.\n\n### 2. Apply the Smallest Safe Fix\n\nPrefer edits that preserve existing behavior while satisfying structured concurrency and lifecycle safety.\n\nCommon fixes:\n\n- **ANR \u002F Main thread blocking**: Move heavy work to `withContext(Dispatchers.IO)` or `Dispatchers.Default`; ensure suspend functions are main-safe.\n- **Memory leaks \u002F zombie coroutines**: Replace `GlobalScope` with a lifecycle-bound scope (`viewModelScope`, `lifecycleScope`, or injected `applicationScope`).\n- **Lifecycle collection issues**: Replace deprecated `launchWhenStarted` with `repeatOnLifecycle(Lifecycle.State.STARTED)`.\n- **State exposure**: Encapsulate `MutableStateFlow` \u002F `MutableSharedFlow`; expose read-only `StateFlow` or `Flow`.\n- **CancellationException swallowing**: Ensure generic `catch (e: Exception)` blocks rethrow `CancellationException`.\n- **Non-cooperative cancellation**: Add `ensureActive()` or `yield()` in tight loops for cooperative cancellation.\n- **Callback APIs**: Convert listeners to `callbackFlow` with proper `awaitClose` cleanup.\n- **Hardcoded Dispatchers**: Inject `CoroutineDispatcher` via constructor for testability.\n\n## Critical Rules\n\n### Dispatcher Injection (Testability)\n\n```kotlin\n\u002F\u002F CORRECT: Inject dispatcher\nclass UserRepository(\n    private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO\n) {\n    suspend fun fetchUser() = withContext(ioDispatcher) { ... }\n}\n\n\u002F\u002F INCORRECT: Hardcoded dispatcher\nclass UserRepository {\n    suspend fun fetchUser() = withContext(Dispatchers.IO) { ... }\n}\n```\n\n### Lifecycle-Aware Collection\n\n```kotlin\n\u002F\u002F CORRECT: Use repeatOnLifecycle\nviewLifecycleOwner.lifecycleScope.launch {\n    viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {\n        viewModel.uiState.collect { state -> updateUI(state) }\n    }\n}\n\n\u002F\u002F INCORRECT: Direct collection (unsafe, deprecated)\nlifecycleScope.launchWhenStarted {\n    viewModel.uiState.collect { state -> updateUI(state) }\n}\n```\n\n### State Encapsulation\n\n```kotlin\n\u002F\u002F CORRECT: Expose read-only StateFlow\nclass MyViewModel : ViewModel() {\n    private val _uiState = MutableStateFlow(UiState())\n    val uiState: StateFlow\u003CUiState> = _uiState.asStateFlow()\n}\n\n\u002F\u002F INCORRECT: Exposed mutable state\nclass MyViewModel : ViewModel() {\n    val uiState = MutableStateFlow(UiState()) \u002F\u002F Leaks mutability\n}\n```\n\n### Exception Handling\n\n```kotlin\n\u002F\u002F CORRECT: Rethrow CancellationException\ntry {\n    doSuspendWork()\n} catch (e: CancellationException) {\n    throw e \u002F\u002F Must rethrow!\n} catch (e: Exception) {\n    handleError(e)\n}\n\n\u002F\u002F INCORRECT: Swallows cancellation\ntry {\n    doSuspendWork()\n} catch (e: Exception) {\n    handleError(e) \u002F\u002F CancellationException swallowed!\n}\n```\n\n### Cooperative Cancellation\n\n```kotlin\n\u002F\u002F CORRECT: Check for cancellation in tight loops\nsuspend fun processLargeList(items: List\u003CItem>) {\n    items.forEach { item ->\n        ensureActive() \u002F\u002F Check cancellation\n        processItem(item)\n    }\n}\n\n\u002F\u002F INCORRECT: Non-cooperative (ignores cancellation)\nsuspend fun processLargeList(items: List\u003CItem>) {\n    items.forEach { item ->\n        processItem(item) \u002F\u002F Never checks cancellation\n    }\n}\n```\n\n### Callback Conversion\n\n```kotlin\n\u002F\u002F CORRECT: callbackFlow with awaitClose\nfun locationUpdates(): Flow\u003CLocation> = callbackFlow {\n    val listener = LocationListener { location ->\n        trySend(location)\n    }\n    locationManager.requestLocationUpdates(listener)\n    \n    awaitClose { locationManager.removeUpdates(listener) }\n}\n```\n\n## Scope Guidelines\n\n| Scope | Use When | Lifecycle |\n|-------|----------|-----------|\n| `viewModelScope` | ViewModel operations | Cleared with ViewModel |\n| `lifecycleScope` | UI operations in Activity\u002FFragment | Destroyed with lifecycle owner |\n| `repeatOnLifecycle` | Flow collection in UI | Started\u002FStopped with lifecycle state |\n| `applicationScope` (injected) | App-wide background work | Application lifetime |\n| `GlobalScope` | **NEVER USE** | Breaks structured concurrency |\n\n## Testing Pattern\n\n```kotlin\n@Test\nfun `loading data updates state`() = runTest {\n    val testDispatcher = StandardTestDispatcher(testScheduler)\n    val repository = FakeRepository()\n    val viewModel = MyViewModel(repository, testDispatcher)\n    \n    viewModel.loadData()\n    advanceUntilIdle()\n    \n    assertEquals(UiState.Success(data), viewModel.uiState.value)\n}\n```\n\n## Reference Material\n\n- [Kotlin Coroutines Best Practices](https:\u002F\u002Fdeveloper.android.com\u002Fkotlin\u002Fcoroutines\u002Fcoroutines-best-practices)\n- [StateFlow and SharedFlow](https:\u002F\u002Fdeveloper.android.com\u002Fkotlin\u002Fflow\u002Fstateflow-and-sharedflow)\n- [repeatOnLifecycle API](https:\u002F\u002Fdeveloper.android.com\u002Ftopic\u002Flibraries\u002Farchitecture\u002Fcoroutines#repeatOnLifecycle)\n",{},"main","def8531e65114c0fca8fb8551c1871ee0eed705c","skills\u002Fnew-silvermoon\u002Fawesome-android-agent-skills\u002Fkotlin-concurrency-expert",24,"https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub","https:\u002F\u002Fskillshub.wtf","https:\u002F\u002Fgithub.com\u002FComeOnOliver","AI & Blockchain Engineer\r\n","2026-06-24 22:07:19",{"source":7,"skillName":5,"commands":37,"prompt":47,"zipAvailable":48},[38,41,44],{"tool":39,"command":40},"npx","npx skills add ComeOnOliver\u002Fskillshub --skill kotlin-concurrency-expert",{"tool":42,"command":43},"bunx","bunx skills add ComeOnOliver\u002Fskillshub --skill kotlin-concurrency-expert",{"tool":45,"command":46},"pnpm","pnpm dlx skills add ComeOnOliver\u002Fskillshub --skill kotlin-concurrency-expert","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002Fnew-silvermoon\u002Fawesome-android-agent-skills\u002Fkotlin-concurrency-expert\u002FSKILL.md",true,[],[51,60,69,78,84,90],{"skillNo":52,"name":53,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":54,"categories":55,"keywords":56,"publishedAt":23},"01M6FKJN13GMG4KY57J3QD8GY5","model-hierarchy-skill","https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002Fzscole\u002Fmodel-hierarchy-skill\u002Fmodel-hierarchy-skill\u002FSKILL.md",[],[57,58,59],"model","model hierarchy","model hierarchy skill",{"skillNo":61,"name":62,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":63,"categories":64,"keywords":65,"publishedAt":23},"01M6FKJJJ6GCAMR4PPPRD12M5X","zoom-mcp","https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002Fzoom\u002Fskills\u002Fzoom-mcp\u002FSKILL.md",[],[66,67,68],"MCP","zoom","zoom mcp",{"skillNo":70,"name":71,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":72,"categories":73,"keywords":74,"publishedAt":23},"01M6FKJE3KYT3KPEZMEEHYKA6K","zoom-apps-sdk","https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002Fzoom\u002Fskills\u002Fzoom-apps-sdk\u002FSKILL.md",[],[75,76,77,67],"sdk","zoom apps sdk","apps",{"skillNo":79,"name":80,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":81,"categories":82,"keywords":83,"publishedAt":23},"01M6FKJ9EDH9Q5VWDF8HAJRSP0","windows","https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002Fzoom\u002Fskills\u002Fwindows\u002FSKILL.md",[],[80],{"skillNo":85,"name":86,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":87,"categories":88,"keywords":89,"publishedAt":23},"01M6FKJ666BEEFC3AE9GJ72Q93","whiteboard","https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002Fzoom\u002Fskills\u002Fwhiteboard\u002FSKILL.md",[],[86],{"skillNo":91,"name":92,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":93,"categories":94,"keywords":95,"publishedAt":23},"01M6FKJ21WFCDGTJQ2Q91T9DVW","websockets","https:\u002F\u002Fgithub.com\u002FComeOnOliver\u002Fskillshub\u002Fblob\u002Fmain\u002Fskills\u002Fzoom\u002Fskills\u002Fwebsockets\u002FSKILL.md",[],[92]]