HealthConnectClient.permissionController.getGrantedPermissions() freeze the process when called inside async coroutine with await()
This bug doesn't occure if an Intent using PermissionController.createRequestPermissionResultContract() is launch before.
A simple code like this reproduces the bug :
private val PERMISSIONS = setOf(HealthPermission.getReadPermission(StepsRecord::class))
private fun syncCheckPermissions(healthConnectClient:HealthConnectClient): Boolean {
val granted: Boolean = runBlocking {
try {
withTimeout(5000) {
GlobalScope.async {
val granted = healthConnectClient.permissionController.getGrantedPermissions()
granted.containsAll(PERMISSIONS)
}.await()
}
} catch (e: TimeoutCancellationException) {
return@runBlocking true
}
}
return granted
}
When getGrantedPermissions() is replaced by any mock function, there is no timeout triggered.
I'm not sure if the bug come from the lib or my code. Is there another way to get non-suspend result from suspend function ?
Edit : I know runBlocking block the process until await done and there is the bug, await never return a result and in the end I got an ANR from the system.
Inside the runBlocking block, you call the await() function.
The runBlocking block - Runs a new coroutine and blocks the current thread until its completion.
This is the reason why your syncCheckPermissions function blocks the thread and waits for the result from await().
So syncCheckPermissions isn't async because runBlocking, you can declare syncCheckPermissions as a suspend function or use callback approach.
I guess there you can find more detailed explanation