JetBrains/intellij-community / .agents/skills/compose-ui
compose-ui
VerifiedWrite or audit Compose Desktop UI state, effects, and boundaries.
audit/references/effects/infinite-loop-patterns.md
View on GitHub# Infinite Loop Patterns
Infinite recomposition loops are the most severe effect misuse. They freeze the UI and peg CPU.
## Pattern 1: Effect key changes on every write
```kotlin
// BUG — LaunchedEffect key = count; count++ changes key → effect restarts → count++ again
var count by remember { mutableIntStateOf(0) }
LaunchedEffect(count) {
count++ // count changes → recomposition → new key → LaunchedEffect restarts
}
// Note: LaunchedEffect(Unit) would NOT loop — it runs exactly once regardless of recomposition.
```
**Fix:** Use a stable key and add suspension to avoid tight-loop state writes:
```kotlin
// FIXED — Unit key runs once; delay prevents tight-loop recomposition
LaunchedEffect(Unit) {
while (isActive) {
delay(1000)
count++
}
}
```
## Pattern 2: derivedStateOf writes to state it reads
```kotlin
// BUG — derivedStateOf is read-only; writing inside it is undefined behaviour
val filtered by remember {
derivedStateOf {
if (items.value.isEmpty()) {
showEmptyState.value = true // DO NOT DO THIS
}
items.value.filter { it.active }
}
}
```
**Fix:** Move the side effect out of `derivedStateOf`:
```kotlin
val filtered by remember { derivedStateOf { items.value.filter { it.active } } }
LaunchedEffect(filtered) {
showEmptyState.value = filtered.isEmpty()
}
```
## Pattern 3: snapshotFlow collects into state that feeds the flow
```kotlin
// BUG — scrollPosition drives itself
val scrollPosition = mutableIntStateOf(0)
LaunchedEffect(Unit) {
snapshotFlow { scrollPosition.intValue }
.collect { scrollPosition.intValue = it + 1 }
}
```
**Fix:** Do not write to the same state you are observing. Use a different state target or a
`Flow` operator.
## Pattern 4: Unstable key causes restart storm
```kotlin
// BUG — lambda is a new object every recomposition
LaunchedEffect(onRefresh) { onRefresh() }
// FIX — stabilise the key or use rememberUpdatedState
val currentOnRefresh by rememberUpdatedState(onRefresh)
LaunchedEffect(Unit) { currentOnRefresh() }
```
## Detection
- UI freezes or becomes unresponsive.
- CPU usage spikes to 100% of one core.
- Logs from the effect print continuously.
- Compose debug builds may throw an exception after too many recompositions.
## Fix strategy
1. Add `delay(1)` or `withFrameNanos` inside the effect to break the synchronous cycle.
2. Identify which state write triggers the recomposition.
3. Move the write to a different coroutine, a different phase, or a different composable.
4. Ensure effect keys are stable primitives or use `rememberUpdatedState`.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |