could you annotate a method that should not be cal...
# coroutines
r
could you annotate a method that should not be called within a coroutine so that you get a compile-time error?
s
How would that work? Eg what if you call a regular (non-suspend) function and that function then calls that 'forbidden' method? Curious: What use-case do you have to need such a check?
it should have been annotated. I missed the javadoc
but yes, I guess since calling a function that calls a function would be a common issue there no way to check if you are in a coroutine context
s
Ah.. ThreadLocal vs Coroutine Context Elements magic 🙂 Still, I'm not sure how you can tell the compiler when a function is called as part of a stack that has its root in a Coroutine, since regular functions can be called from Coroutines, which in turn can call your forbidden function....
r
ok, but moving to runtime, how would you warn?
e
kotlinx-coroutines-debug has integration with BlockHound which you might be able to repurpose or copy
o
Late to the party, but... indeed, tweaking BlockHound could accomplish this, although it would complain about a blocking call, not a message tailored to the special case above. If you'd like to give it a try, you could create a
BlockHoundIntegration
like this (untested, but hopefully works)
Copy code
import reactor.blockhound.integration.BlockHoundIntegration
import reactor.blockhound.BlockHound.Builder

class MyBlockHoundIntegration : BlockHoundIntegration {
    override fun applyTo(builder: Builder): Unit = with(builder) {
        markAsBlocking(
            "io.opentelemetry.context.ImplicitContextKeyed", "makeCurrent", "()Lio/opentelemetry/context/Scope;"
        )
    }
}
along with a service provider configuration file.
Added caveat: The above would only work with coroutine threads marked as non-blocking. So threads from
Dispatchers.Default
work, while those from
<http://Dispatchers.IO|Dispatchers.IO>
would not detect calls.