Hi folks, there is any CoroutinesContextElement/Ke...
# coroutines
s
Hi folks, there is any CoroutinesContextElement/Key that says when is running inside a
runBlocking
?
d
You would have to check if
ContinuationInterceptor
is an
EventLoop
, however that is an internal class.
e
you could do a horrible hack like
Copy code
runBlocking {
    coroutineContext[ContinuationInterceptor.Key]!!::class
        .allSuperclasses
    	.any { it.qualifiedName == "kotlinx.coroutines.EventLoop" }
}
but that is fragile. it'll yield false within
runBlocking { withContext(Dispatchers.Unconfined) { } }
but you're still effectively
runBlocking
, for example. what are you trying to do?
s
I'm working with a API that is low latency dependent and there is a Server Loop, we have a couple functions that use Mutex and in some cases we need to use runBlocking, launch a coroutines is not a choose in this cases, we also have a dispatcher that dispatches to this thread in the next Server Loop, the problem is when: we are on the Main Thread and we are using a runBlocking and some code tries to switch to this Main Thread Dispatcher, then, is causes a deadlock, this is predictable, what I want to do is have a log for when this cases happen because this cases usually happen. So by knowing that we are using runBlocking and we are trying to dispatch to this Main Thread I could get the stacktrace and print warning of the deadlock.
e
how about checking Thread.currentThread() then?
(or Looper on Android)