Run Blocking In Suspend

ID

kotlin.run_blocking_in_suspend

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Kotlin

Tags

concurrency, reliability

Description

Reports calls to runBlocking { …​ } made from inside a suspend function.

Rationale

suspend fun fetch() {
    runBlocking { delay(100) } // FLAW
}

A suspend function is already running on a coroutine dispatcher. Calling runBlocking from inside it blocks the underlying OS thread until the child coroutine completes, starving the dispatcher’s thread pool. On small pools (such as the default Android UI dispatcher) this can deadlock the entire application.

Remediation

suspend fun fetch() {
    withContext(Dispatchers.IO) { delay(100) }
}

Replace runBlocking { } with withContext(Dispatchers.X) { } to switch dispatchers without blocking, or simply call the suspending API directly.