安卓协程与并发缺陷审计
沿协程生命周期与共享状态追踪竞态、取消及乱序问题,输出含复现时序的并发审计报告。
完整提示词
# ANDROID COROUTINES AND CONCURRENCY AUDIT
I want you to perform a maximally deep, systematic, evidence-first, and runtime-oriented analysis of Kotlin Coroutines and concurrency behavior across the entire Android application.
Primary objective:
> Identify genuine race conditions, cancellation bugs, improper coroutine scopes, thread-safety violations, duplicate workloads, deadlock hazards, stale results, and inconsistent states that arise when multiple asynchronous operations execute concurrently or complete in an unexpected order.
This is not:
- a generic Kotlin review
- a generic list of Coroutines best practices
- advice to blindly convert everything into Flows
- a blanket recommendation to sprinkle Mutex locks everywhere
- automated dispatcher refactoring
- a superficial scan for `GlobalScope`
- an attempt to serialize every parallel operation
The focus is on actual runtime execution behavior.
Priority:
**data correctness > cancellation safety > structured concurrency > race prevention > resource safety > throughput > code elegance**
It is far better to find 6 confirmed concurrency bugs than to write 100 theoretical warnings.
---
# 1. DETERMINE COROUTINE STACK
Prior to analysis, identify:
- Kotlin version
- kotlinx.coroutines version
- Compose or Views
- ViewModel
- Lifecycle runtime
- Room
- Retrofit / Ktor
- WorkManager
- Flow
- StateFlow
- SharedFlow
- Channels
- Custom CoroutineScopes
- Custom Dispatchers
- RxJava interop if present
Scan repository-wide:
```text
launch
async
await
coroutineScope
supervisorScope
withContext
runBlocking
GlobalScope
CoroutineScope
Job
SupervisorJob
Mutex
Semaphore
Channel
StateFlow
SharedFlow
stateIn
shareIn
callbackFlow
channelFlow
flowOn
collect
collectLatest
first
single
```
Do not draw conclusions solely from the presence of an API call.
---
# 2. MAP CONCURRENCY ARCHITECTURE
For each critical feature, map the execution flow:
```text
User action
↓
ViewModel
↓
Coroutine
↓
Repository
↓
Database / Network
↓
Result
↓
Shared state
↓
UI
```
If parallelism exists:
```text
Coroutine A
↘
shared state
↗
Coroutine B
```
Identify:
- what triggers the job
- who owns its lifecycle
- what resources or state it shares with other jobs
- what happens if operations overlap
- what happens if operations complete in reverse order
---
# 3. COROUTINE OWNERSHIP
For each critical coroutine, identify its scope:
- `viewModelScope`
- `lifecycleScope`
- `viewLifecycleOwner.lifecycleScope`
- WorkManager
- Service scope
- Application scope
- Custom scope
- `GlobalScope`
Ask:
> Does the coroutine lifetime match the true lifetime of the underlying task?
---
# 4. OVERLY NARROW SCOPE
Example:
```text
critical sync
↓
Fragment lifecycleScope
↓
user leaves screen
↓
coroutine cancelled
↓
sync incomplete
```
If an operation must survive the screen, the scope is misplaced.
---
# 5. OVERLY BROAD SCOPE
The inverse scenario:
```text
screen-specific work
↓
application scope
↓
screen destroyed
↓
work continues
↓
stale result affects global state
```
Report only if an actual negative consequence exists.
---
# 6. GLOBALSCOPE
Analyze every single usage of `GlobalScope` thoroughly.
Do not report it automatically.
Ask:
- who expects the result
- what cancels the job
- what happens during user logout
- what happens across process lifecycle transitions
- can the result be delivered to an obsolete state
---
# 7. CUSTOM SCOPE
For every:
```kotlin
CoroutineScope(...)
```
determine:
- Job
- SupervisorJob
- Dispatcher
- Owner
- Cancellation strategy
If there is no clear owner, classify it as an architectural and concurrency risk.
---
# 8. STRUCTURED CONCURRENCY
Verify that child jobs belong to parent operations where business semantics require it.
Example:
```text
loadDashboard
├── loadProfile
├── loadStats
└── loadNotifications
```
If one failure should abort the entire operation, verify standard `coroutineScope`.
If isolation is needed, verify `supervisorScope`.
Do not swap one for the other without understanding business semantics.
---
# 9. `launch`
For each critical `launch`, ask:
> Who awaits the completion of this task?
If the answer is:
```text
nobody
```
determine whether it is genuinely a fire-and-forget task or an accidentally dropped dependency.
---
# 10. FIRE-AND-FORGET
Particularly dangerous for:
- database writes
- analytics mutations that alter state
- file uploads
- payment processing
- message delivery
- state cleanup
If the caller proceeds under the assumption that the operation succeeded while the coroutine has yet to execute, check for false-success scenarios.
---
# 11. `async`
Look for `async` calls that:
- are never awaited
- execute in an unnecessary scope
- are used merely as a substitute for `launch`
An un-awaited `async` can silently swallow exceptions.
---
# 12. PARALLELIZATION
If using:
```kotlin
async { loadA() }
async { loadB() }
```
verify that A and B can truly run concurrently.
If B depends on A, parallelization is a correctness defect.
---
# 13. SERIAL EXECUTION
Conversely, look for:
```kotlin
val a = loadA()
val b = loadB()
val c = loadC()
```
when all three are completely independent.
This is a performance improvement, not a concurrency bug.
Classify as P4 unless the resulting latency produces a tangible failure.
---
# 14. CANCELLATION
For each critical suspend operation, ask:
> What happens if it is cancelled at this exact point?
Trace:
```text
start
↓
partial work completed
↓
cancellation
↓
cleanup / rollback / persisted partial state
```
---
# 15. CANCELLATION PROPAGATION
Verify that child coroutines receive cancellation promptly when the parent job is no longer relevant.
---
# 16. SWALLOWED CANCELLATION
Specifically look for:
```kotlin
catch (e: Exception) {
...
}
```
inside suspend functions.
Check whether it catches `CancellationException` and suppresses cancellation.
---
# 17. IMPROPER RETRY AFTER CANCELLATION
Scenario:
```text
screen closes
↓
coroutine cancelled
↓
catch(Exception)
↓
treated as a network failure
↓
retry starts
```
A job that was meant to terminate continues running indefinitely.
---
# 18. `runCatching`
Review suspend code wrapped in:
```kotlin
runCatching { ... }
```
Ensure cancellation is not converted into a normal failure result without rethrowing `CancellationException`.
---
# 19. `NonCancellable`
Scrutinize every usage of `NonCancellable`.
Valid use cases exist, primarily for brief, critical cleanup operations.
Look for:
- network requests
- lengthy database operations
- retries
- navigation commands
inappropriately placed inside `NonCancellable`.
---
# 20. CLEANUP
If cancellation occurs during resource allocation, verify `finally` blocks.
Examples:
- file streams
- lock acquisition
- temporary files
- progress state flags
- database transaction wrappers
---
# 21. PARTIAL SIDE EFFECTS
A suspend function can perform part of its work before cancellation occurs.
Example:
```text
DB record inserted
↓
coroutine cancelled
↓
network call never dispatched
```
Can the system gracefully recover from this half-executed state?
---
# 22. NON-ATOMIC MULTI-STEP OPERATIONS
Map:
```text
step A
↓
step B
↓
step C
```
If cancellation or failure between steps leaves state inconsistent, report the defect.
---
# 23. TRANSACTIONS
If multiple database writes constitute a single business operation, verify database transaction wrapping.
The concurrency audit must confirm that other readers cannot observe partially written state.
---
# 24. THREAD CONFINEMENT
Determine which state belongs to:
- Main thread
- a single dedicated worker thread
- multi-threaded concurrent access
Do not assume thread safety merely because code is written in coroutines.
Coroutines can switch threads across suspension points.
---
# 25. SHARED MUTABLE STATE
Search repository-wide for:
```kotlin
var ...
mutableListOf
mutableMapOf
ArrayList
HashMap
```
that are accessed or modified across multiple coroutines.
---
# 26. READ-MODIFY-WRITE RACE
Classic race scenario:
```text
Coroutine A reads count = 5
Coroutine B reads count = 5
A writes 6
B writes 6
```
Expected:
```text
7
```
Actual:
```text
6
```
Look for such patterns across shared caches and state variables.
---
# 27. CHECK-THEN-ACT RACE
Example:
```text
if (!exists(id)) {
insert(id)
}
```
Two coroutines can both evaluate `false`.
Without a unique database constraint, duplicate records are created.
---
# 28. BUSINESS INVARIANT RACE
For each critical invariant, ask:
> Can two concurrent calls both pass the check before either modifies the state?
Examples:
- single active media recording
- single default account
- single room reservation
- single pending sync job
- limited inventory quantity
---
# 29. DATABASE CONSTRAINT AS LAST LINE OF DEFENSE
If an invariant must be absolute, UI checks or single-process Mutex guards may be insufficient.
Verify database and server constraints where applicable.
---
# 30. MUTEX
For each Mutex, determine:
- what resource it protects
- whether all access paths acquire the same Mutex instance
- the lifecycle scope of the Mutex
- process boundaries
---
# 31. LOCAL MUTEX FALSE SAFETY
Example:
```text
Repository instance A -> Mutex A
Repository instance B -> Mutex B
```
If dependency injection creates multiple instances, synchronization fails across consumers.
---
# 32. MULTI-PROCESS
A Mutex in one process cannot synchronize access with another Android process.
If the app utilizes multiple processes, account for this boundary.
---
# 33. SERVER CONCURRENCY
A client-side Mutex cannot prevent the same user action dispatched from a second device.
If an invariant must hold globally, the backend must be the authoritative barrier.
---
# 34. DEADLOCK
Map multi-lock acquisitions.
Scenario:
```text
Coroutine A:
lock X
↓
wait for lock Y
Coroutine B:
lock Y
↓
wait for lock X
```
If possible, document the exact deadlock sequence.
---
# 35. LOCK ORDERING
If multiple functions acquire the same set of locks in differing orders, this is a high-risk deadlock signal.
---
# 36. SUSPENDING WHILE HOLDING A LOCK
Examine long-running suspend calls held inside a Mutex.
Example:
```kotlin
mutex.withLock {
networkCall()
}
```
This serializes all callers across network latency.
Not inherently a bug, but assess contention and timeout risks.
---
# 37. BLOCKING LOCKS
Look for:
- `synchronized`
- `ReentrantLock`
on the Main thread or wrapped around suspending code.
---
# 38. `synchronized`
If a critical section is brief, non-suspending, and localized, standard synchronization can be appropriate.
Do not replace it with a Mutex merely because the project uses coroutines.
---
# 39. ATOMIC TYPES
If using:
- AtomicBoolean
- AtomicInteger
- AtomicReference
verify that a single atomic variable truly encompasses the entire invariant.
Atomic individual fields do not make a multi-field operation atomic.
---
# 40. VOLATILE
`@Volatile` guarantees memory visibility across threads, not multi-step atomicity.
Look for flawed reliance on `@Volatile` for read-modify-write patterns.
---
# 41. STATEFLOW
Map all mutable StateFlow instances.
Ask:
- who has write permissions
- how many concurrent writers exist
- does an update depend on the previous value
---
# 42. STATEFLOW VALUE RACE
Example:
```kotlin
_state.value = _state.value.copy(count = _state.value.count + 1)
```
If multiple coroutines update the state concurrently, check for lost updates.
---
# 43. `update`
If using atomic `_state.update { ... }`, verify that it resolves the specific shared-state race.
Do not recommend it without genuine concurrent writers.
---
# 44. MULTIPLE STATEFLOW WRITERS
An ideal audit signal:
Map every function that mutates the same `MutableStateFlow`.
---
# 45. PARTIAL UI STATE
If the following are updated independently:
```text
dataFlow
loadingFlow
errorFlow
```
concurrent coroutines can produce contradictory UI combinations (e.g. loading = true with error = nonNull).
---
# 46. COMPOSITE UISTATE
Assess whether a single atomic UiState model eliminates race conditions only if actual coordination defects exist.
---
# 47. SHAREDFLOW
Check:
- replay count
- extraBufferCapacity
- bufferOverflow policy
- active collector count
---
# 48. LOST EVENT
Scenario:
```text
event emitted
↓
no active collector
↓
event lost
```
If the event must be durable, an unbuffered SharedFlow is the wrong tool.
---
# 49. DUPLICATE EVENT
If replay > 0 for transient one-time events, new collectors receive stale historical events.
---
# 50. BACKPRESSURE
If a producer emits faster than a consumer processes, inspect:
- suspend
- buffer
- DROP_OLDEST
- DROP_LATEST
Business-critical events must never be silently dropped.
---
# 51. CHANNEL
For Channels, verify:
- capacity
- sender lifecycle
- receiver lifecycle
- closure handling
---
# 52. CHANNEL CLOSURE
Who closes the Channel, and what happens to pending senders after closure?
---
# 53. MULTIPLE CONSUMERS
Channels distribute items to consumers round-robin; they do not broadcast by default.
Verify whether the author mistakenly expected broadcast semantics.
---
# 54. FLOW EXCEPTIONS
Determine where exceptions originate:
```text
upstream
operator
collector
```
and where they are caught.
---
# 55. `catch`
A Flow `catch` operator does not intercept exceptions thrown in downstream collectors.
Trace the exact operator chain.
---
# 56. `retry`
For each retry operator, check:
- which exceptions trigger a retry
- maximum attempt limit
- backoff delay strategy
- cancellation responsiveness
- mutation idempotency
---
# 57. INFINITE RETRY
Look for flows that indefinitely retry fatal, non-transient errors.
---
# 58. RETRY STORM
Multiple collectors can simultaneously retry the same upstream failure, exacerbating server strain.
---
# 59. `collectLatest`
Verify that cancelling the previous collector block does not leave half-executed side effects.
---
# 60. SEARCH FLOW
Classic search scenario:
```text
query A
↓
request A dispatched
query B
↓
request A cancelled
↓
request B dispatched
```
Verify that the underlying HTTP client actually aborts the connection upon coroutine cancellation.
---
# 61. FLATMAPLATEST
When used for search or dynamic resource switching, ensure downstream side effects do not outlive cancellation.
---
# 62. FLATMAPMERGE
Concurrent inner flows can complete in arbitrary order.
Check whether output ordering carries business significance.
---
# 63. ZIP VS COMBINE
Ensure the chosen operator aligns with intended semantics:
`combine` emits whenever any source emits after all have produced at least one value.
`zip` pairs elements strictly 1-to-1.
An improper choice produces missed or unexpected UI state emissions.
---
# 64. DEBOUNCE
Analyze debounce operators on search and autosave alongside cancellation and final flush logic.
---
# 65. AUTOSAVE
Scenario:
```text
edit A
↓
debounce
↓
save A starts
↓
edit B
↓
save B starts
↓
B completes
↓
A completes late
```
Can the older save A overwrite newer save B?
---
# 66. REQUEST ORDERING
For any resource fetched by a dynamic ID, test:
```text
request A starts
request B starts
B completes
A completes
```
Can the older response A become the final displayed state?
---
# 67. NETWORK CANCELLATION
Verify Retrofit, OkHttp, and Ktor cancellation behavior against the actual versions used.
Do not assume full cancellation support without verification.
---
# 68. CANCELLATION DOES NOT EQUAL ROLLBACK
If an HTTP request reaches the server, cancelling the client coroutine does not mean the server aborted the mutation.
Particularly critical for write operations.
---
# 69. UNKNOWN MUTATION OUTCOME
Scenario:
```text
POST request sent
↓
server creates record
↓
network connection drops before response arrives
↓
client receives timeout
```
The client cannot know if the mutation succeeded.
A naive retry without idempotency keys creates duplicate records.
---
# 70. IDEMPOTENCY
For critical mutations, verify:
- client-generated operation IDs
- idempotency headers
- database unique constraints
- server-side deduplication
where necessary.
---
# 71. DOUBLE TAP
Simulate two concurrent coroutines triggered by rapid double-tapping a button.
Do not rely solely on UI button disabling if the operation must guarantee exactly-once execution.
---
# 72. RAPID STATE CHANGES
Test rapid user interactions:
- rapid toggle on / off
- multi-item selection changes
- drag-and-drop reordering
- status changes
Look for out-of-order persistence writes.
---
# 73. OPTIMISTIC UPDATES
Map:
```text
old local state
↓
optimistic state applied
↓
remote mutation dispatched
↓
success / failure
↓
reconciliation
```
If two concurrent optimistic mutations occur, verify rollback integrity.
---
# 74. ROLLBACK RACE
Mutations A and B touch the same entity.
If A fails after B succeeds, a naive rollback of A can overwrite B's valid changes.
---
# 75. VERSIONED STATE
For complex concurrent updates, consider entity versioning or revision tokens only when the business flow justifies it.
---
# 76. ROOM CONCURRENCY
Room manages internal multi-threading safely, but higher-level business logic can introduce race conditions:
- multi-query operations without transactions
- insert vs update race conditions
- missing unique index constraints
- unsynchronized read-modify-write sequences
---
# 77. TRANSACTION BOUNDARY
If individual DAO methods are annotated with `@Transaction`, verify whether the entire business sequence is wrapped in a single unified transaction.
---
# 78. DAO FLOW + WRITES
A Room Flow can emit intermediate states between separate sequential write operations.
If the UI must not observe partial state, wrap the writes in a transaction.
---
# 79. DATABASE + NETWORK DUAL WRITE
Scenario:
```text
local DB write
↓
network write
```
or vice versa.
Examine failure handling between the two operations.
---
# 80. OFFLINE-FIRST CONCURRENCY
When the local database acts as the single source of truth:
- foreground UI refreshes
- background periodic sync
- user local mutations
can concurrently modify the same data rows.
Map potential conflicts.
---
# 81. SYNC VS USER EDIT
Scenario:
```text
sync worker downloads older server entity
↓
user locally edits the entity with newer data
↓
sync worker writes downloaded response to DB
↓
user local changes are overwritten
```
Check timestamp or revision conflict resolution models.
---
# 82. WORKMANAGER + FOREGROUND
A background Worker and a foreground Repository can execute the exact same synchronization task concurrently.
---
# 83. UNIQUE WORK
Verify whether `ExistingWorkPolicy` resolves only WorkManager internal duplication, rather than concurrency with active foreground calls.
---
# 84. MULTIPLE WORKERS
If workers with different unique names mutate the same underlying database tables, they can conflict.
---
# 85. WORKER RETRY
WorkManager can reschedule and re-execute jobs.
Every worker must be idempotent and resilient to re-execution.
---
# 86. PROCESS RESTART
In-memory mutexes and singleton flags disappear upon process death.
Do not rely on them for durable background deduplication.
---
# 87. MULTIPLE APP INSTANCES / PROCESSES
If the backend can be invoked from multiple devices or separate Android processes, client-side in-memory locking offers no protection.
---
# 88. SHARED PREFERENCES
Concurrent writes to SharedPreferences can encounter race and ordering issues.
Verify whether state synchronized through preferences or DataStore maintains consistent semantics.
---
# 89. DATASTORE
DataStore `updateData` and `edit` provide atomic read-modify-write semantics.
If code reads state outside the edit block and writes it later, a race condition exists.
---
# 90. FILE WRITES
Concurrent file write operations can:
- truncate data
- overwrite modifications
- corrupt file contents
Verify locking and atomic replacement strategies.
---
# 91. TEMP FILE + RENAME
For critical files, writing to a temporary file and atomically renaming is a standard pattern.
Do not mandate it without a concrete corruption risk.
---
# 92. CACHE CONCURRENCY
For in-memory LRU or custom caches:
- read
- write
- eviction
verify thread-safe access.
---
# 93. SINGLETON REPOSITORY
A singleton repository is not inherently thread-safe.
Verify internal synchronization.
---
# 94. LAZY INITIALIZATION
Double initialization races can occur if custom lazy loading patterns are not thread-safe.
---
# 95. TOKEN REFRESH RACE
Among the most critical network concurrency flows:
```text
Request A -> 401 Unauthorized
Request B -> 401 Unauthorized
Request C -> 401 Unauthorized
↓
three concurrent refresh attempts dispatched
```
Check:
- single-flight token refresh
- queuing waiting requests
- refresh token rotation handling
- retry count boundaries
---
# 96. REFRESH TOKEN ROTATION
If the auth provider rotates refresh tokens on use, concurrent refresh requests will invalidate the session and log the user out.
---
# 97. AUTH STATE
Logout can execute concurrently with:
- token refresh
- active API retries
- background synchronization
Trace execution ordering.
---
# 98. LOGOUT VS ACTIVE REQUEST
Scenario:
```text
User A request dispatched
↓
user logs out
↓
User B logs in
↓
User A request completes late
```
Can User A's response be written into User B's state, cache, or database?
This represents a potential P0 or P1 finding.
---
# 99. SESSION GENERATION
In complex multi-user apps, verify whether asynchronous callbacks validate that their response matches the active session generation.
Do not mandate this mechanism if account switching is not supported.
---
# 100. CACHE USER ISOLATION
If concurrent logout and login leave prior requests running, verify data isolation across users.
---
# 101. PAGINATION
Concurrent:
- pull-to-refresh
- scroll-to-append
- filter adjustments
can produce duplicate items or gaps in the list.
---
# 102. PAGING 3
If Paging 3 is utilized, inspect:
- invalidation handling
- RemoteMediator implementation
- paging keys
- transaction boundaries
- refresh race conditions
---
# 103. REMOTEMEDIATOR
Specifically verify that:
```text
remote keys
+
cached entities
```
are updated within a single unified database transaction.
---
# 104. MEDIA
Player commands can arrive concurrently from:
- UI controls
- media notification actions
- headset buttons
- MediaSession callbacks
across multiple threads.
Verify command serialization and player state machine integrity.
---
# 105. RECORDING
Start / stop recording races carry high failure risks.
Scenario:
```text
start recording
↓
stop recording
↓
start recording
```
in rapid succession.
Verify hardware resource ownership and terminal state transitions.
---
# 106. CAMERA
Camera bind / unbind operations and lifecycle callbacks can race against each other.
If camera features exist, analyze its state machine.
---
# 107. DOWNLOAD
Concurrent:
- pause
- cancel
- complete
actions can arrive almost simultaneously.
Verify which terminal state takes precedence.
---
# 108. UPLOAD
Network retry loops and explicit user cancellation can race.
Ask:
> Can a cancelled upload still result in a success state?
---
# 109. STATE MACHINE
For complex asynchronous features, map explicit states:
Example:
```text
IDLE
STARTING
RUNNING
STOPPING
COMPLETED
FAILED
CANCELLED
```
Verify allowed transitions.
---
# 110. INVALID TRANSITIONS
Look for:
```text
STOPPING -> STARTING
```
or conflicting terminal transitions executing concurrently.
---
# 111. COMPARE-AND-SET
If a state machine requires atomic transitions, verify the implementation.
Do not mandate CAS if single-thread confinement already guarantees ordering.
---
# 112. ACTOR MODEL
If using Channel or actor-like serialization patterns, check:
- message queue growth limits
- channel closure
- error handling propagation
Do not suggest actors merely because concurrency exists.
---
# 113. MAIN THREAD SERIALIZATION
Certain UI state modifications are naturally serialized on `Dispatchers.Main`.
Do not report a race if all writers are guaranteed to execute sequentially on the Main thread without intervening suspension points.
---
# 114. SUSPENSION POINTS
Crucial distinction:
Two coroutines executing on the Main dispatcher can interleave whenever a suspension point is reached.
Trace suspension points.
---
# 115. CHECK + SUSPEND + ACT
Scenario:
```text
if (!busy) {
networkCall() // suspend point
busy = true
}
```
A second coroutine passes the `!busy` check while the first is suspended.
---
# 116. FLAG GUARDS
A boolean flag like `isLoading` or `isSaving` is not an automatic thread-safe guard.
Check:
- when it is toggled
- whether suspension occurs prior to setting the flag
- how many concurrent writers modify it
---
# 117. EARLY GUARD
Scenario:
```text
if (saving) return
saving = true
try {
...
} finally {
saving = false
}
```
This pattern can be completely sufficient if executed sequentially on `Dispatchers.Main`.
Do not overcomplicate without cause.
---
# 118. `withContext`
For every critical `withContext`, verify:
- target Dispatcher
- return values
- cancellation behavior
- nesting overhead
---
# 119. `Dispatchers.IO`
Do not treat `Dispatchers.IO` as a mandatory wrapper around all background operations.
Many libraries (Room, Retrofit) internally manage I/O dispatching.
An extra context switch is usually a performance optimization question, not a bug.
---
# 120. `Dispatchers.Default`
CPU-intensive computational work should be executed on `Dispatchers.Default`.
Check for excessive parallel CPU tasks starving the pool.
---
# 121. LIMITED PARALLELISM
If using `limitedParallelism`, check the rationale and the underlying resource it constrains.
---
# 122. CUSTOM EXECUTOR
If converting an Executor to a `CoroutineDispatcher`, verify lifecycle and shutdown procedures.
---
# 123. EXECUTOR LEAK
A custom thread pool executor that is never shut down can leak resources if not intentionally process-scoped.
---
# 124. `runBlocking`
Analyze every invocation of `runBlocking` on the Android runtime path.
Pay special attention to the Main thread.
---
# 125. DEADLOCK WITH `runBlocking`
If `runBlocking` on the Main thread awaits a coroutine that requires `Dispatchers.Main` to finish, a deadlock occurs.
Prove the execution flow.
---
# 126. BLOCKING API IN COROUTINE
Running a coroutine does not make a blocking Java API non-blocking.
Calling blocking I/O on `Dispatchers.Main` remains an ANR hazard.
---
# 127. THREAD AFFINITY
Certain platform APIs require invocation from a specific thread (e.g. specific OpenGL, camera, or UI components).
Verify library contracts before filing a finding.
---
# 128. CALLBACK THREAD
Legacy SDK callbacks may not return on the Main thread.
If modifying UI or state requiring Main thread confinement, verify context switching.
---
# 129. CALLBACK MULTIPLE INVOCATIONS
Do not assume a callback fires exactly once unless guaranteed by the SDK contract.
---
# 130. CALLBACK AFTER CANCELLATION
If a coroutine bridge cancels the operation, the underlying callback can still fire.
Check race conditions with the continuation state.
---
# 131. `suspendCancellableCoroutine`
For each usage, verify:
- immediate synchronous callback
- asynchronous delayed callback
- `invokeOnCancellation` listener cleanup
- double-resume prevention
- exception handling
---
# 132. RESUME AFTER CANCELLATION
The continuation may already be cancelled when the callback fires.
Ensure the implementation conforms to the API contract.
---
# 133. CALLBACKFLOW
Verify `awaitClose { ... }`.
If the listener is not removed inside `awaitClose`, a memory and listener leak occurs.
---
# 134. CALLBACKFLOW CHANNEL CLOSURE
If an external API emits after the channel closes, verify `trySend()` result handling.
---
# 135. CONFLATION
When using `conflate()`, verify that dropped intermediate values are not business-critical.
---
# 136. DISTINCT UNTIL CHANGED
If the equality check or data class `equals` is flawed, valid state updates can be suppressed.
---
# 137. MAPLATEST
Like `collectLatest`, verify that cancelled transformations do not leave partial side effects behind.
---
# 138. FLOW BUFFER
Adding a buffer changes timing and backpressure dynamics.
Ask whether the producer is allowed to outpace the consumer.
---
# 139. FLOWON
`flowOn` modifies the upstream execution context, not the downstream collector context.
Look for flawed mental models.
---
# 140. UI STATE ORDERING
If multiple independent Flows control a single screen, combining them can produce transient invalid state combinations.
Ensure the UI handles intermediate states safely.
---
# 141. TRANSACTIONAL SNAPSHOT
If a screen requires multiple values that must represent the exact same point in time, independent streams can lead to inconsistencies.
---
# 142. CACHE INVALIDATION RACE
Scenario:
```text
mutation dispatched
↓
cache invalidated
↓
refetch triggered
```
If an older request is already in-flight, can its response arrive after the refetch, overwriting new data?
---
# 143. REQUEST DEDUPLICATION
Ensure multiple screens or components do not dispatch identical expensive requests in parallel when a shared single-flight repository pattern is needed.
---
# 144. SINGLE-FLIGHT
Single-flight is not always necessary.
It is particularly relevant for:
- auth token refreshes
- heavy startup initializations
- static configuration fetches
---
# 145. INITIALIZATION RACE
Scenario:
```text
Screen A -> ensureInitialized()
Screen B -> ensureInitialized()
```
Both can trigger duplicate initialization logic simultaneously.
---
# 146. LAZY SINGLETON INITIALIZATION
If initialization involves side effects, verify thread safety.
---
# 147. CONFIG REFRESH
Refreshing remote configuration concurrently with feature evaluations can produce inconsistent feature states if updates are not atomic.
---
# 148. FEATURE FLAGS
If multiple flags form a cohesive feature configuration, ensure they update together as a single atomic snapshot.
---
# 149. FILE IMPORT
File import routines and routine CRUD operations can modify the database concurrently.
Check isolation levels.
---
# 150. BACKUP
Generating a backup snapshot during active database write operations can produce corrupt or inconsistent backup data without proper snapshot mechanisms.
---
# 151. RESTORE
Data restoration and background worker sync tasks must not mutate the database concurrently without explicit coordination.
---
# 152. DESTRUCTIVE RESET
Data reset operations must coordinate with:
- background workers
- repositories
- open database handles
- file I/O operations
Look for post-reset resurrection of wiped data.
---
# 153. DELETE VS ACTIVE OPERATION
Scenario:
```text
resource operation in-flight
↓
user deletes the resource
↓
operation completes
↓
resource is recreated or stale state is saved
```
---
# 154. ACCOUNT DELETION
Apply the same verification to account deletion and logout.
An in-flight background request must not repopulate deleted private user data.
---
# 155. APP STARTUP CONCURRENCY
During application launch, tasks often run in parallel:
- auth token verification
- database migrations
- remote configuration fetches
- local cache priming
- background sync scheduling
Map cross-task dependencies.
---
# 156. STARTUP RACE
Scenario:
```text
navigation decision executed
↓
auth session still loading
```
or:
```text
UI queries database
↓
migration / initial sync still altering tables
```
---
# 157. DATASTORE INITIALIZATION
If preferences govern routing, theme, or authentication, handle the initial unknown state explicitly rather than falling back prematurely to defaults.
---
# 158. ERROR STATE RACE
Request A failure can overwrite Request B success.
Scenario:
```text
Request A starts
Request B starts
Request B succeeds
Request A fails late
```
The final UI can erroneously display an error despite Request B's success.
---
# 159. LOADING STATE RACE
A shared boolean across multiple concurrent requests:
```text
Request A starts -> loading = true
Request B starts -> loading = true
Request A completes -> loading = false
Request B still running
```
Look for this pattern.
---
# 160. ACTIVE REQUEST COUNTER
Do not mandate an integer counter everywhere, but consider an appropriate reference-counting model when multiple concurrent requests share a single loading indicator.
---
# 161. REQUEST ID / GENERATION TOKEN
For "latest wins" features, check whether:
- cancellation
- generation tokens
- target ID verification
prevent older responses from overwriting newer state.
---
# 162. LATEST-WINS VS ALL-MUST-COMPLETE
Classify each feature:
Search queries generally require latest-wins.
Batch file uploads generally require all-must-complete.
Do not apply the same concurrency model to both.
---
# 163. FIRST-WINS
Some features may require the first valid response among multiple competing sources.
Verify prompt cancellation of the remaining tasks.
---
# 164. FAN-OUT / FAN-IN
For parallel requests, check:
- partial failure handling
- cancellation propagation
- result aggregation
- timeout handling
---
# 165. `awaitAll`
If one asynchronous task fails, verify what happens to sibling tasks and confirm it matches business requirements.
---
# 166. SUPERVISOR SCOPE
If partial success is acceptable, a supervisor scope is appropriate.
However, explicit error handling must be defined for each child task.
---
# 167. TIMEOUT
When using `withTimeout`, verify what cancellation implies for underlying side effects.
---
# 168. `withTimeoutOrNull`
Returning `null` can erase the distinction between:
- an actual timeout occurrence
- a valid, legitimate null response
if the domain allows null.
---
# 169. NETWORK TIMEOUT VS COROUTINE TIMEOUT
Two timeout layers (HTTP client timeout vs coroutine timeout) can have differing thresholds and error types.
Verify consistency.
---
# 170. RETRY + TIMEOUT
Calculate worst-case elapsed duration where possible:
```text
timeout
x
max attempts
+
exponential backoff
```
Do not invent numbers if configurations are unknown.
---
# 171. CANCELLATION-SAFE UI STATE
A `finally` block resetting:
```kotlin
_loading.value = false
```
can be appropriate.
However, ensure another concurrent operation is not actively relying on that same loading state.
---
# 172. PROGRESS
If multiple concurrent tasks update a unified progress indicator, verify atomic progress aggregation.
---
# 173. PERCENTAGE
Ensure a late progress callback cannot regress total progress from 90% back down to 30% due to an out-of-order event.
---
# 174. DOWNLOAD QUEUE
If a download queue is maintained, check:
- concurrency limit
- task cancellation
- resumption logic
- duplicate ID rejection
- terminal state resolution
---
# 175. SEMAPHORE
If using a Semaphore to limit concurrency, ensure permits are released reliably in failure and cancellation scenarios.
---
# 176. PERMIT LEAK
A permit that is not released inside a `finally` block can permanently stall the queue.
---
# 177. STARVATION
Unfair queueing or priority inversions can starve tasks.
Report only if the implementation permits practical starvation.
---
# 178. ORDERING
If users expect FIFO task execution, verify that the concurrency layer does not execute tasks in arbitrary order.
---
# 179. BATCH OPERATIONS
For batch operations, verify:
- partial success handling
- retry boundaries
- rollback strategies
- duplicate item prevention
---
# 180. TRANSACTION + REMOTE CALLS
A database transaction cannot span across remote network calls in a distributed client-server environment.
Never keep a local database transaction open across slow network requests without exceptional justification.
---
# 181. OUTBOX PATTERN
For reliable client-to-server data synchronization, consider an outbox pattern only when architectural requirements justify it.
Do not recommend enterprise patterns for simple features without need.
---
# 182. INBOX / DEDUPLICATION
For server push events or WebSocket synchronization, check duplicate delivery handling.
---
# 183. WEBSOCKET
Concurrent:
- socket reconnects
- old connection messages
- new connection messages
can produce stale event races.
---
# 184. CONNECTION GENERATION
When a WebSocket reconnects, verify that callbacks from the previous connection instance cannot mutate current state.
---
# 185. POLLING
A polling request can overlap with the next interval if the request takes longer than the interval duration.
---
# 186. FIXED DELAY VS FIXED RATE
If polling uses a sequential loop:
```text
fetch()
delay(interval)
```
requests cannot overlap, unlike independent timer tasks.
Verify the actual implementation.
---
# 187. APP BACKGROUND
Polling or Flows can continue running in the background if the parent scope remains active.
Assess whether background execution is intentional.
---
# 188. TESTING
Review concurrency test coverage.
Look for suites that test only happy-path sequential execution.
---
# 189. `runTest`
Verify proper usage of the `kotlinx.coroutines.test` API.
---
# 190. STANDARDTESTDISPATCHER
Virtual time makes execution deterministic, but tests must advance the virtual scheduler explicitly (`advanceUntilIdle()`).
---
# 191. UNCONFINEDTESTDISPATCHER
Can mask ordering and interleaving bugs due to eager execution.
Do not classify it as bad by definition, but recognize its limitations.
---
# 192. REAL DISPATCHERS IN TESTS
Hardcoding `Dispatchers.IO`, `Main`, or `Default` degrades testability.
A finding depends on whether tests become flaky, non-deterministic, or difficult to isolate.
---
# 193. RACE TESTING
For each serious race finding, propose a deterministic test.
Avoid relying on:
```text
Thread.sleep(100)
```
when you can control schedulers or deferred responses.
---
# 194. CONTROLLED DEFERRED
An effective race test:
```text
start task A
start task B
complete task B
complete task A
assert final state
```
This directly validates out-of-order execution behavior.
---
# 195. STRESS TESTING
Repeated concurrent stress tests can help detect shared mutable state defects.
However, stress testing is not a substitute for deterministic reproduction.
---
# 196. TURBINE
If the project uses Turbine or similar Flow test utilities, verify emission sequence assertions and cancellation checks.
Do not demand a specific library purely for fashion.
---
# 197. WORKMANAGER TESTS
For background worker concurrency, test retries, duplicate scheduling, and unique work constraints.
---
# 198. ROOM CONCURRENCY TESTS
Use an in-memory or real Room database test harness where necessary to validate unique constraints and transactions.
---
# 199. NETWORK MOCKS
MockWebServer or equivalent can simulate:
- latency delays
- out-of-order response sequences
- socket disconnects
- connection timeouts
if supported by the project stack.
---
# 200. FINDING FORMAT
For every serious finding:
```text
ID:
Severity:
Category:
Confidence:
Status:
Feature:
Module:
File:
Function/Class:
Coroutine owner:
Scope:
Dispatcher:
Shared resource/state:
Problem:
Evidence:
Concurrency Timeline:
T0:
T1:
T2:
T3:
Expected result:
Actual possible result:
Cancellation behavior:
Data impact:
User impact:
Root cause:
Recommended remediation:
Regression test:
Runtime verification:
Complexity:
XS / S / M / L / XL
```
---
# 201. CONCURRENCY TIMELINE
For any race condition finding, a precise timeline is mandatory.
Example:
```text
T0 - request A starts for query "cat"
T1 - request B starts for query "dog"
T2 - B completes and writes dog results
T3 - A completes and overwrites state with cat results
```
Without a demonstrable sequence of events, do not designate an issue as a race condition.
---
# 202. SEVERITY
Use:
## P0 - CRITICAL
- cross-user data exposure
- catastrophic data corruption
- duplicate financial or irreversible business actions
- concurrency flaw that breaks a core security invariant
## P1 - HIGH
- data loss
- severe duplicate mutations
- session corruption
- deadlock in a primary user flow
- major stale overwrite
- concurrency defect that consistently disrupts a critical feature
## P2 - MEDIUM
- reproducible race or cancellation bug of contained scope
- inconsistent UI or data state with an available workaround
## P3 - LOW
- minor edge-case concurrency issue
- low-impact resource contention
## P4 - IMPROVEMENT
- architectural concurrency improvement without an active bug
---
# 203. CONFIDENCE
Use:
```text
HIGH
MEDIUM
LOW
```
HIGH:
the race condition is directly provable via code flow or reproducible test.
MEDIUM:
multiple asynchronous paths can compete, but device reproduction is missing.
LOW:
scenario depends on unverified vendor threading or third-party library behavior.
---
# 204. STATUS
Use:
```text
CONFIRMED
LIKELY
THEORETICAL
NOT VERIFIED
```
---
# 205. DO NOT REPORT "POSSIBLE RACE" WITHOUT A TIMELINE
If you cannot demonstrate:
```text
Task A
Task B
Interleaving sequence
Incorrect resulting state
```
the finding is not a confirmed race condition.
---
# 206. DO NOT USE MUTEX AS A UNIVERSAL FIX
Recommended remediation must consider:
- single-thread confinement
- atomic updates (`_state.update`)
- database unique constraints
- database transactions
- idempotency keys
- cancellation
- explicit state machines
- request generation tokens
Choose the simplest solution that directly resolves the root cause.
---
# 207. DO NOT SERIALIZE EVERYTHING
Excessive locking can:
- increase latency
- introduce deadlock hazards
- block unrelated workloads
Constrain concurrency only where a shared invariant or resource truly demands it.
---
# 208. DO NOT MODIFY CODE
During the audit:
- do not add Mutexes
- do not change scopes
- do not swap dispatchers
- do not rewrite Flows
- do not introduce Channels
- do not add retries
- do not modify WorkManager policies
Complete the audit first.
---
# 209. OUTPUT - ANDROID_COROUTINES_CONCURRENCY_AUDIT.md
Structure the final audit report:
## 1. Executive Summary
- coroutine architecture overview
- scope hierarchy
- highest race risks
- cancellation safety status
- shared-state management health
## 2. Coroutine Scope Map
| Scope | Owner | Work | Lifetime | Risk |
|---|---|---|---|---|
## 3. Dispatcher Map
## 4. Shared Mutable State Inventory
## 5. StateFlow / SharedFlow Audit
## 6. Flow Operator Audit
## 7. Cancellation Audit
## 8. Structured Concurrency Audit
## 9. Race Condition Audit
## 10. Mutex / Lock Audit
## 11. Deadlock Audit
## 12. Network Concurrency
## 13. Token Refresh Concurrency
## 14. Room / Database Concurrency
## 15. Offline / Sync Concurrency
## 16. WorkManager Concurrency
## 17. Resource State Machines
## 18. Logout / Account Switching Concurrency
## 19. Retry / Idempotency Audit
## 20. Test Coverage
## 21. Findings Summary
| ID | Severity | Category | Shared resource | Problem | Confidence | Status |
|---|---|---|---|---|---|---|
## 22. P0 Findings
## 23. P1 Findings
## 24. P2 Findings
## 25. P3 Findings
## 26. P4 Improvements
## 27. Things Done Well
## 28. Unknown / Not Verified
## 29. Remediation Roadmap
---
# 210. RACE CONDITION MATRIX
For critical features:
| Feature | Operation A | Operation B | Shared state/resource | Safe? | Evidence |
|---|---|---|---|---|---|
Examples:
- refresh vs refresh
- refresh vs edit
- save vs save
- sync vs user edit
- logout vs in-flight request
- start vs stop
- delete vs background sync
---
# 211. CANCELLATION MATRIX
| Operation | Owner | Cancellation trigger | Partial side effects | Safe? |
|---|---|---|---|---|
---
# 212. IDEMPOTENCY MATRIX
| Mutation | Retry possible | Duplicate consequence | Protection | Status |
|---|---|---|---|---|
---
# 213. STATE MACHINE MATRIX
For complex asynchronous features:
| From | Event | To | Allowed | Atomic |
|---|---|---|---|---|
Look for concurrent competing transitions from the same initial state.
---
# 214. SECOND PASS - OUT-OF-ORDER ATTACK
For every asynchronous function, assume:
> Responses return in the worst possible order.
Simulate:
```text
A starts
B starts
C starts
C finishes
B finishes
A finishes
```
Ask:
> Does the final state represent the user's latest intent?
---
# 215. SECOND PASS - DOUBLE ACTION
For each write operation, simulate:
```text
tap
tap
tap
```
in rapid succession.
Check:
- local database
- remote API
- queue
- notifications
- final resulting state
---
# 216. SECOND PASS - CANCELLATION
For each suspend function, mentally insert cancellation after each major step:
```text
step 1
CANCEL
step 1
step 2
CANCEL
step 1
step 2
step 3
CANCEL
```
Ask:
> Does the system remain in a consistent state?
---
# 217. SECOND PASS - LOGOUT
The most critical account race:
```text
User A request dispatched
↓
user logs out
↓
User B logs in
↓
User A request finishes
```
Re-check:
- StateFlow
- Room database
- In-memory cache
- Local files
- System notifications
- Navigation back stack
---
# 218. SECOND PASS - PROCESS DEATH
Ask:
> Which concurrency protections vanish when the process is killed?
Examples:
- Mutex
- in-memory boolean flag
- singleton task queue
If protection is required for a durable or global invariant, it may reside on the wrong layer.
---
# 219. SECOND PASS - BACKGROUND WORK
Simulate:
```text
foreground operation running
↓
app sent to background
↓
WorkManager starts a similar synchronization operation
```
Check shared state conflicts.
---
# 220. SECOND PASS - SLOW SERVER
Assume every network operation takes 30 seconds.
This widens the execution window for:
- double-tap interactions
- logout transitions
- navigation away
- periodic refreshes
- retry loops
- process backgrounding
Re-evaluate critical user flows under high latency.
---
# 221. SECOND PASS - FAILURE AFTER SIDE EFFECT
The classic distributed systems scenario:
```text
server side effect succeeds
↓
network response drops
↓
client receives connection error
```
Ask:
> What does the client retry logic do?
---
# 222. FINAL QUALITY GATE
Before submitting the final report, verify:
- every race condition includes a precise event timeline
- you did not report a race simply because two coroutines exist
- cancellation is not confused with failure
- `CancellationException` handling has been verified
- every Mutex has verified scope and granularity semantics
- client-side locking is not misrepresented as a global or server-level guarantee
- retry logic for write mutations verifies idempotency
- StateFlow multi-writer behavior is properly analyzed
- loading and error state races are investigated
- logout and account switching concurrency is verified
- Room transactions and database constraints are examined
- WorkManager task re-execution is accounted for
- process termination removing in-memory guards is considered
- performance optimization is not labeled as a correctness defect
- deadlock findings demonstrate a concrete conflicting lock order
- remediation does not introduce unnecessary serialization
---
# FINAL RULE
I do not want a generic report stating:
> Use structured concurrency, Mutex, and Dispatchers.IO.
That is not a concurrency audit.
I am looking for concrete issues such as:
```text
Request A starts for account A
↓
user logs out
↓
account B logs in
↓
request A completes
↓
repository writes response into global user cache
↓
account B sees data belonging to account A
```
or:
```text
save version 1 starts
↓
save version 2 starts
↓
version 2 reaches server first
↓
version 1 reaches server second
↓
older state overwrites newer state
```
or:
```text
three API requests receive 401 Unauthorized
↓
three token refresh coroutines start
↓
first refresh rotates the token
↓
other two refresh requests use invalidated old token
↓
session is logged out erroneously
```
or:
```text
worker reads "not synced"
↓
foreground code also reads "not synced"
↓
both send create requests concurrently
↓
server creates duplicate records
```
or:
```text
coroutine catches generic Exception
↓
CancellationException is swallowed
↓
screen is destroyed
↓
coroutine continues retry loop
↓
old screen operation keeps executing in background
```
or:
```text
Request A starts
↓
Request B starts
↓
B succeeds
↓
UI displays correct new data
↓
A fails later
↓
shared error state updates to failure
↓
UI replaces valid B result with stale error
```
These are the concrete concurrency defects you must uncover.
Think through:
- ownership
- interleaving
- ordering
- shared state
- cancellation
- retries
- idempotency
- atomicity
- transaction boundaries
- process boundaries
- account boundaries
For every serious finding, answer:
> Which two or more operations can overlap?
> What state or resource do they share?
> In what execution order must events occur for the defect to manifest?
> What mechanism currently prevents or fails to prevent this scenario?
If there is no concrete answer:
**NOT VERIFIED.**
If the issue is merely a theoretical concurrency enhancement without an active failure:
**P4 - IMPROVEMENT.**
It is far better to find 5 genuine race conditions with precise timelines than 50 generic warnings about thread safety.
The objective is a forensically sound concurrency audit from which every serious finding can directly translate into:
- deterministic reproduction
- concurrency regression test
- atomicity and idempotency fix
- runtime verification
- production-safe concurrency model分类与标签
编程开发、安卓开发、协程取消、竞态条件、共享状态