ANR: CommandQueueJNIBridge.cppDelete() blocks main thread for 5+ seconds during RiveFile.close() ...
Description
When using rememberRiveFile in the Compose API, the RiveFile cleanup via cppDelete() is executed synchronously on the main thread, causing ANRs. The disposal should be asynchronous, consistent with the documented threading model.
Source Code Analysis
Location of the bug: kotlin/src/main/kotlin/app/rive/core/CommandQueue.kt lines 120-131
private fun dispose(cppPointer: Long) {
bridge.cppDelete(cppPointer) // ← SYNCHRONOUS JNI CALL - CAUSES ANR
listeners.close()
renderContext.close()
// ...
}
Call path triggering the issue:
Composable leaves composition
produceState.awaitDispose → RiveFile.close()
CloseOnce.close() → riveWorker.release()
RCPointer.release() → onDispose(cppPointer)
CommandQueue.dispose() → bridge.cppDelete(cppPointer) ← BLOCKS MAIN THREAD
Thread context: awaitDispose runs on AndroidUiDispatcher (main thread), so the entire dispose() chain runs on main thread.
Library Version & Environment
PropertyValuerive-android11.1.1 (also tested 11.1.2)compileSdk36targetSdk35minSdk26Compose BOM2025.02.00
Stack Trace (Production)
ANR: Main thread blocked for 5007ms
at app.rive.core.CommandQueueJNIBridge.cppDelete(CommandQueueBridge.kt)
at app.rive.core.CommandQueue.dispose(CommandQueue.kt:131)
at app.rive.core.CommandQueue.access$dispose(CommandQueue.kt)
at app.rive.core.CommandQueue$cppPointer$1.invoke(CommandQueue.kt:121)
at app.rive.core.RCPointer.release(RCPointer.kt:115)
at app.rive.core.CommandQueue.release(CommandQueue.kt:145)
at app.rive.RiveFile$1.invoke(RiveFile.kt:45)
at app.rive.core.CloseOnce.close(CloseOnce.kt:34)
at app.rive.RiveFile.close(RiveFile.kt:2)
at app.rive.RiveFileKt$rememberRiveFile$1$1.invoke(RiveFile.kt:199)
at androidx.compose.runtime.ProduceStateScopeImpl.awaitDispose(ProduceState.kt:51)
...
at android.os.Looper.loop(Looper.java:357)
at android.app.ActivityThread.main(ActivityThread.java:8088)
Suggested Fix
Use the existing cppRunOnCommandServer to dispatch cppDelete to the worker thread:
Option A: Dispatch cleanup to command server thread
// In CommandQueue.kt
private fun dispose(cppPointer: Long) {
// Dispatch native cleanup to command server thread
bridge.cppRunOnCommandServer(cppPointer) {
bridge.cppDelete(cppPointer)
}
listeners.close()
renderContext.close()
// ...
}
Option B: Dispatch disposal to IO thread with coroutines
private fun dispose(cppPointer: Long) {
CoroutineScope(Dispatchers.IO).launch {
bridge.cppDelete(cppPointer)
}
listeners.close()
renderContext.close()
// ...
}
Option C: Make RiveFile.close() a suspend function or accept a CoroutineScope
This would allow the caller to control the disposal context, but requires API changes.
Reproduction
Use rememberRiveFile with any Rive animation in a Compose screen
Navigate away from the screen (triggering awaitDispose)
Main thread blocks for 5+ seconds during cppDelete
Relevant Source Files
FileLineDescriptionkotlin/src/main/kotlin/app/rive/RiveFile.kt 190-199 /rememberRiveFile calls awaitDispose { close() }kotlin/src/main/kotlin/app/rive/RiveFile.kt 38-43 RiveFile implements AutoCloseable via CloseOncekotlin/src/main/kotlin/app/rive/core/CommandQueue.kt 120-131 dispose() calls bridge.cppDelete() synchronouslykotlin/src/main/kotlin/app/rive/core/RCPointer.kt 98-108 release() calls onDispose() when refCount reaches 0kotlin/src/main/kotlin/app/rive/core/CommandQueueBridge.kt 351 cppDelete is an external JNI function
Impact
ANR dialogs in production
UI freezes during screen transitions
Affects all apps using rememberRiveFile with screen navigation
Reported by multiple production apps